File tree 2 files changed +27
-0
lines changed
test/std/strings/basic.string/string.access 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -1198,11 +1198,17 @@ public:
1198
1198
1199
1199
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator [](size_type __pos) const _NOEXCEPT {
1200
1200
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS (__pos <= size (), " string index out of bounds" );
1201
+ if (__builtin_constant_p (__pos) && !__fits_in_sso (__pos)) {
1202
+ return *(__get_long_pointer () + __pos);
1203
+ }
1201
1204
return *(data () + __pos);
1202
1205
}
1203
1206
1204
1207
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator [](size_type __pos) _NOEXCEPT {
1205
1208
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS (__pos <= size (), " string index out of bounds" );
1209
+ if (__builtin_constant_p (__pos) && !__fits_in_sso (__pos)) {
1210
+ return *(__get_long_pointer () + __pos);
1211
+ }
1206
1212
return *(__get_pointer () + __pos);
1207
1213
}
1208
1214
Original file line number Diff line number Diff line change @@ -34,10 +34,31 @@ TEST_CONSTEXPR_CXX20 void test_string() {
34
34
assert (s2[0 ] == ' \0 ' );
35
35
}
36
36
37
+ // Same, but for the string that doesn't fit into SSO.
38
+ template <class S >
39
+ TEST_CONSTEXPR_CXX20 void test_string_long () {
40
+ S s (" 0123456789012345678901234567890123456789" );
41
+ const S& cs = s;
42
+ ASSERT_SAME_TYPE (decltype (s[0 ]), typename S::reference);
43
+ ASSERT_SAME_TYPE (decltype (cs[0 ]), typename S::const_reference);
44
+ LIBCPP_ASSERT_NOEXCEPT (s[0 ]);
45
+ LIBCPP_ASSERT_NOEXCEPT (cs[0 ]);
46
+ for (typename S::size_type i = 0 ; i < cs.size (); ++i) {
47
+ assert (s[i] == static_cast <char >(' 0' + (i % 10 )));
48
+ assert (cs[i] == s[i]);
49
+ }
50
+ assert (s[33 ] == static_cast <char >(' 0' + (33 % 10 )));
51
+ assert (cs[34 ] == s[34 ]);
52
+ assert (cs[cs.size ()] == ' \0 ' );
53
+ const S s2 = S ();
54
+ assert (s2[0 ] == ' \0 ' );
55
+ }
56
+
37
57
TEST_CONSTEXPR_CXX20 bool test () {
38
58
test_string<std::string>();
39
59
#if TEST_STD_VER >= 11
40
60
test_string<std::basic_string<char , std::char_traits<char >, min_allocator<char >>>();
61
+ test_string_long<std::basic_string<char , std::char_traits<char >, min_allocator<char >>>();
41
62
#endif
42
63
43
64
return true ;
You can’t perform that action at this time.
0 commit comments