mirror of
https://github.com/boostorg/static_string.git
synced 2025-06-25 03:41:33 +02:00
Fix operator+ for non-full arrays
This commit is contained in:
@ -5544,36 +5544,34 @@ operator+(
|
||||
std::size_t(0), 1, lhs);
|
||||
}
|
||||
|
||||
// Add a null-terminated character array to a string.
|
||||
// Add a null terminated character array to a string.
|
||||
template<
|
||||
std::size_t N, std::size_t M,
|
||||
typename CharT, typename Traits>
|
||||
BOOST_STATIC_STRING_CPP14_CONSTEXPR
|
||||
inline
|
||||
basic_static_string<(N + M) - 1, CharT, Traits>
|
||||
basic_static_string<N + M, CharT, Traits>
|
||||
operator+(
|
||||
const basic_static_string<N, CharT, Traits>& lhs,
|
||||
const CharT(&rhs)[M])
|
||||
{
|
||||
// Subtract 1 to account for the null terminator, as "hello" is a char[6]
|
||||
return basic_static_string<(N + M) - 1, CharT, Traits>(lhs).append(+rhs, M - 1);
|
||||
return basic_static_string<N + M, CharT, Traits>(lhs).append(+rhs);
|
||||
}
|
||||
|
||||
// Add a string to a null-terminated character array.
|
||||
// Add a string to a null terminated character array.
|
||||
template<
|
||||
std::size_t N, std::size_t M,
|
||||
typename CharT, typename Traits>
|
||||
BOOST_STATIC_STRING_CPP14_CONSTEXPR
|
||||
inline
|
||||
basic_static_string<(N + M) - 1, CharT, Traits>
|
||||
basic_static_string<N + M, CharT, Traits>
|
||||
operator+(
|
||||
const CharT(&lhs)[N],
|
||||
const basic_static_string<M, CharT, Traits>& rhs)
|
||||
{
|
||||
// Subtract 1 to account for the null terminator, as "hello" is a char[6]
|
||||
// The cast to std::size_t is needed here since 0 is a null pointer constant
|
||||
return basic_static_string<(N + M) - 1, CharT, Traits>(rhs).insert(
|
||||
std::size_t(0), +lhs, N - 1);
|
||||
return basic_static_string<N + M, CharT, Traits>(rhs).insert(
|
||||
std::size_t(0), +lhs);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -7096,7 +7096,7 @@ testOperatorPlus()
|
||||
{
|
||||
auto res = s1 + "world";
|
||||
BOOST_TEST(res == "helloworld");
|
||||
BOOST_TEST(res.capacity() == 15);
|
||||
BOOST_TEST(res.capacity() == 16);
|
||||
BOOST_TEST(res.size() == 10);
|
||||
}
|
||||
|
||||
@ -7104,7 +7104,25 @@ testOperatorPlus()
|
||||
{
|
||||
auto res = "hello" + s2;
|
||||
BOOST_TEST(res == "helloworld");
|
||||
BOOST_TEST(res.capacity() == 15);
|
||||
BOOST_TEST(res.capacity() == 16);
|
||||
BOOST_TEST(res.size() == 10);
|
||||
}
|
||||
|
||||
// operator+(static_string, CharT(&)[N]), no null
|
||||
{
|
||||
char arr[10] = {'w', 'o', 'r', 'l', 'd'};
|
||||
auto res = s1 + arr;
|
||||
BOOST_TEST(res == "helloworld");
|
||||
BOOST_TEST(res.capacity() == 20);
|
||||
BOOST_TEST(res.size() == 10);
|
||||
}
|
||||
|
||||
// operator+(CharT(&)[N], static_string), no null
|
||||
{
|
||||
char arr[10] = {'h', 'e', 'l', 'l', 'o'};
|
||||
auto res = arr + s2;
|
||||
BOOST_TEST(res == "helloworld");
|
||||
BOOST_TEST(res.capacity() == 20);
|
||||
BOOST_TEST(res.size() == 10);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user