diff --git a/libcxx/include/string b/libcxx/include/string --- a/libcxx/include/string +++ b/libcxx/include/string @@ -2522,7 +2522,7 @@ basic_string<_CharT, _Traits, _Allocator>& basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str) { - if (this != &__str) { + if (this != std::addressof(__str)) { __copy_assign_alloc(__str); if (!__is_long()) { if (!__str.__is_long()) { @@ -3982,7 +3982,7 @@ return false; if (data() == nullptr) return false; - if (data()[size()] != value_type()) + if (!_Traits::eq(data()[size()], value_type())) return false; return true; } diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp @@ -17,25 +17,35 @@ #include "test_macros.h" #include "min_allocator.h" +#include "nasty_string.h" + +template +TEST_CONSTEXPR_CXX20 void test() { + using CharT = typename S::value_type; + + S s(CONVERT_TO_CSTRING(CharT, "123")); + s.append({CharT('a'), CharT('b'), CharT('c')}); + assert(s == CONVERT_TO_CSTRING(CharT, "123abc")); +} TEST_CONSTEXPR_CXX20 bool test() { - { - std::string s("123"); - s.append({'a', 'b', 'c'}); - assert(s == "123abc"); - } - { - typedef std::basic_string, min_allocator> S; - S s("123"); - s.append({'a', 'b', 'c'}); - assert(s == "123abc"); - } + test(); +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + test(); +#endif +#if TEST_STD_VER >= 20 + test(); +#endif + test(); + test(); + + test, min_allocator>>(); + test(); return true; } -int main(int, char**) -{ +int main(int, char**) { test(); #if TEST_STD_VER > 17 static_assert(test()); diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp @@ -15,16 +15,15 @@ #include #include "test_macros.h" +#include "nasty_string.h" #include "min_allocator.h" #include "test_allocator.h" template -TEST_CONSTEXPR_CXX20 void -test(S s, S str, S expected) -{ - s.assign(str); - LIBCPP_ASSERT(s.__invariants()); - assert(s == expected); +TEST_CONSTEXPR_CXX20 void test(S s, S str) { + s.assign(str); + LIBCPP_ASSERT(s.__invariants()); + assert(s == str); } template @@ -37,69 +36,66 @@ assert(s.get_allocator() == a); } +template +TEST_CONSTEXPR_CXX20 void test_assign() { + using CharT = typename S::value_type; + + test(S(), S()); + test(S(), S(CONVERT_TO_CSTRING(CharT, "12345"))); + test(S(), S(CONVERT_TO_CSTRING(CharT, "1234567890"))); + test(S(), S(CONVERT_TO_CSTRING(CharT, "12345678901234567890"))); + + test(S(CONVERT_TO_CSTRING(CharT, "12345")), S()); + test(S(CONVERT_TO_CSTRING(CharT, "12345")), S(CONVERT_TO_CSTRING(CharT, "12345"))); + test(S(CONVERT_TO_CSTRING(CharT, "12345")), S(CONVERT_TO_CSTRING(CharT, "1234567890"))); + test(S(CONVERT_TO_CSTRING(CharT, "12345")), S(CONVERT_TO_CSTRING(CharT, "12345678901234567890"))); + + test(S(CONVERT_TO_CSTRING(CharT, "1234567890")), S()); + test(S(CONVERT_TO_CSTRING(CharT, "1234567890")), S(CONVERT_TO_CSTRING(CharT, "12345"))); + test(S(CONVERT_TO_CSTRING(CharT, "1234567890")), S(CONVERT_TO_CSTRING(CharT, "1234567890"))); + test(S(CONVERT_TO_CSTRING(CharT, "1234567890")), S(CONVERT_TO_CSTRING(CharT, "12345678901234567890"))); + + test(S(CONVERT_TO_CSTRING(CharT, "12345678901234567890")), S()); + test(S(CONVERT_TO_CSTRING(CharT, "12345678901234567890")), S(CONVERT_TO_CSTRING(CharT, "12345"))); + test(S(CONVERT_TO_CSTRING(CharT, "12345678901234567890")), S(CONVERT_TO_CSTRING(CharT, "1234567890"))); + test(S(CONVERT_TO_CSTRING(CharT, "12345678901234567890")), S(CONVERT_TO_CSTRING(CharT, "12345678901234567890"))); +} + TEST_CONSTEXPR_CXX20 bool test() { - { - typedef std::string S; - test(S(), S(), S()); - test(S(), S("12345"), S("12345")); - test(S(), S("1234567890"), S("1234567890")); - test(S(), S("12345678901234567890"), S("12345678901234567890")); - - test(S("12345"), S(), S()); - test(S("12345"), S("12345"), S("12345")); - test(S("12345"), S("1234567890"), S("1234567890")); - test(S("12345"), S("12345678901234567890"), S("12345678901234567890")); - - test(S("1234567890"), S(), S()); - test(S("1234567890"), S("12345"), S("12345")); - test(S("1234567890"), S("1234567890"), S("1234567890")); - test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890")); - - test(S("12345678901234567890"), S(), S()); - test(S("12345678901234567890"), S("12345"), S("12345")); - test(S("12345678901234567890"), S("1234567890"), S("1234567890")); - test(S("12345678901234567890"), S("12345678901234567890"), - S("12345678901234567890")); - - testAlloc(S(), S(), std::allocator()); - testAlloc(S(), S("12345"), std::allocator()); - testAlloc(S(), S("1234567890"), std::allocator()); - testAlloc(S(), S("12345678901234567890"), std::allocator()); - } + test_assign(); +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + test_assign(); +#endif +#if TEST_STD_VER >= 20 + test_assign(); +#endif + test_assign(); + test_assign(); +#if TEST_STD_VER >= 20 + test_assign(); +#endif - { // LWG#5579 make sure assign takes the allocators where appropriate - typedef other_allocator A; // has POCCA --> true - typedef std::basic_string, A> S; - testAlloc(S(A(5)), S(A(3)), A(3)); - testAlloc(S(A(5)), S("1"), A()); - testAlloc(S(A(5)), S("1", A(7)), A(7)); - testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7)); - } + { + typedef std::string S; + testAlloc(S(), S(), std::allocator()); + testAlloc(S(), S("12345"), std::allocator()); + testAlloc(S(), S("1234567890"), std::allocator()); + testAlloc(S(), S("12345678901234567890"), std::allocator()); + } + + { // LWG#5579 make sure assign takes the allocators where appropriate + typedef other_allocator A; // has POCCA --> true + typedef std::basic_string, A> S; + testAlloc(S(A(5)), S(A(3)), A(3)); + testAlloc(S(A(5)), S("1"), A()); + testAlloc(S(A(5)), S("1", A(7)), A(7)); + testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7)); + } #if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; - test(S(), S(), S()); - test(S(), S("12345"), S("12345")); - test(S(), S("1234567890"), S("1234567890")); - test(S(), S("12345678901234567890"), S("12345678901234567890")); - - test(S("12345"), S(), S()); - test(S("12345"), S("12345"), S("12345")); - test(S("12345"), S("1234567890"), S("1234567890")); - test(S("12345"), S("12345678901234567890"), S("12345678901234567890")); - - test(S("1234567890"), S(), S()); - test(S("1234567890"), S("12345"), S("12345")); - test(S("1234567890"), S("1234567890"), S("1234567890")); - test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890")); - - test(S("12345678901234567890"), S(), S()); - test(S("12345678901234567890"), S("12345"), S("12345")); - test(S("12345678901234567890"), S("1234567890"), S("1234567890")); - test(S("12345678901234567890"), S("12345678901234567890"), - S("12345678901234567890")); - + test_assign(); testAlloc(S(), S(), min_allocator()); testAlloc(S(), S("12345"), min_allocator()); testAlloc(S(), S("1234567890"), min_allocator());