diff --git a/include/boost/static_string/static_string.hpp b/include/boost/static_string/static_string.hpp index f8a1ec1..4b6ce64 100644 --- a/include/boost/static_string/static_string.hpp +++ b/include/boost/static_string/static_string.hpp @@ -4456,33 +4456,41 @@ operator+( CharT lhs, const basic_static_string& rhs) { - return basic_static_string(rhs).insert(0, lhs); + // The cast to std::size_t is needed here since 0 is a null pointer constant + return basic_static_string(rhs).insert( + std::size_t(0), 1, lhs); } +// 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 +basic_static_string<(N + M) - 1, CharT, Traits> operator+( const basic_static_string& lhs, const CharT(&rhs)[M]) { - return basic_static_string(lhs).append(+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); } +// 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 +basic_static_string<(N + M) - 1, CharT, Traits> operator+( const CharT(&lhs)[N], const basic_static_string& rhs) { - return basic_static_string(rhs).insert(0, +rhs, N); + // 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); } //------------------------------------------------------------------------------ diff --git a/test/static_string.cpp b/test/static_string.cpp index 4f556d7..c01a183 100644 --- a/test/static_string.cpp +++ b/test/static_string.cpp @@ -7009,6 +7009,53 @@ testStream() BOOST_TEST(a.str() == b.subview()); } +void +testOperatorPlus() +{ + static_string<10> s1 = "hello"; + static_string<10> s2 = "world"; + + // operator+(static_string, static_string) + { + auto res = s1 + s2; + BOOST_TEST(res == "helloworld"); + BOOST_TEST(res.capacity() == 20); + BOOST_TEST(res.size() == 10); + } + + // operator+(static_string, CharT) + { + auto res = s1 + '!'; + BOOST_TEST(res == "hello!"); + BOOST_TEST(res.capacity() == 11); + BOOST_TEST(res.size() == 6); + } + + // operator+(CharT, static_string) + { + auto res = '!' + s1; + BOOST_TEST(res == "!hello"); + BOOST_TEST(res.capacity() == 11); + BOOST_TEST(res.size() == 6); + } + + // operator+(static_string, CharT(&)[N]) + { + auto res = s1 + "world"; + BOOST_TEST(res == "helloworld"); + BOOST_TEST(res.capacity() == 15); + BOOST_TEST(res.size() == 10); + } + + // operator+(CharT(&)[N], static_string) + { + auto res = "hello" + s2; + BOOST_TEST(res == "helloworld"); + BOOST_TEST(res.capacity() == 15); + BOOST_TEST(res.size() == 10); + } +} + int runTests() { @@ -7047,6 +7094,7 @@ runTests() testHash(); testEmpty(); testStream(); + testOperatorPlus(); return report_errors(); }