From 5934a281244126e4728d3519a1f690f1c836eea2 Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Fri, 6 Mar 2020 22:26:06 -0500 Subject: [PATCH] Fix operator+ for non-full arrays --- include/boost/static_string/static_string.hpp | 16 ++++++-------- test/static_string.cpp | 22 +++++++++++++++++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/include/boost/static_string/static_string.hpp b/include/boost/static_string/static_string.hpp index 47641ef..09cd0ce 100644 --- a/include/boost/static_string/static_string.hpp +++ b/include/boost/static_string/static_string.hpp @@ -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 operator+( const basic_static_string& 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(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 operator+( const CharT(&lhs)[N], const basic_static_string& 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(rhs).insert( + std::size_t(0), +lhs); } //------------------------------------------------------------------------------ diff --git a/test/static_string.cpp b/test/static_string.cpp index 119b9ef..f4e8ce5 100644 --- a/test/static_string.cpp +++ b/test/static_string.cpp @@ -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); } }