11
11
// <string_view>
12
12
13
13
// constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
14
- // constexpr basic_string_view subview(size_type pos = 0,
15
- // size_type n = npos) const; // freestanding-deleted
16
-
17
- // subview is alternative name of substr
18
14
19
15
// Throws: out_of_range if pos > size().
20
16
// Effects: Determines the effective length rlen of the string to reference as the smaller of n and size() - pos.
21
17
// Returns: basic_string_view(data()+pos, rlen).
22
18
23
- #if 0
24
- # include <algorithm>
25
- # include <cassert>
26
- # include <string_view>
27
-
28
- # include "make_string.h"
29
- # include "test_macros.h"
19
+ #include < algorithm>
20
+ #include < cassert>
21
+ #include < cstddef>
22
+ #include < stdexcept>
23
+ #include < string_view>
30
24
31
- # define CS(S) MAKE_CSTRING(CharT, S)
25
+ #include " test_macros.h "
32
26
33
27
template <typename CharT>
34
- struct Test {
35
- typedef std::basic_string_view<CharT> (std::basic_string_view<CharT>::*Sub)(
36
- typename std::basic_string_view<CharT>::size_type = 0, typename std::basic_string_view<CharT>::size_type = npos) const;
37
- };
38
-
39
-
40
- template <typename CharT, typename Test<CharT>::Sub TestSub>
41
- TEST_CONSTEXPR_CXX14 void testDetail(std::basic_string_view<CharT> sv, std::size_t n, size_t pos) {
28
+ void test1 (std::basic_string_view<CharT> sv, std::size_t n, size_t pos) {
42
29
std::basic_string_view<CharT> sv1;
43
- # ifdef TEST_HAS_NO_EXCEPTIONS
30
+ #ifdef TEST_HAS_NO_EXCEPTIONS
44
31
if (pos > sv.size ())
45
32
return ; // would throw if exceptions were enabled
46
- sv1 = (sv.*TestSub)(pos, n);
47
- # else
48
- if (!TEST_IS_CONSTANT_EVALUATED) {
49
- try {
50
- sv1 = (sv.*TestSub)(pos, n);
51
- assert(pos <= sv.size());
52
- } catch (const std::out_of_range&) {
53
- assert(pos > sv.size());
54
- return;
55
- }
33
+ sv1 = sv.substr (pos, n);
34
+ #else
35
+ try {
36
+ sv1 = sv.substr (pos, n);
37
+ assert (pos <= sv.size ());
38
+ } catch (const std::out_of_range&) {
39
+ assert (pos > sv.size ());
40
+ return ;
56
41
}
57
- # endif
42
+ #endif
58
43
const std::size_t rlen = std::min (n, sv.size () - pos);
59
44
assert (sv1.size () == rlen);
60
45
for (std::size_t i = 0 ; i < rlen; ++i)
61
46
assert (sv[pos + i] == sv1[i]);
62
47
}
63
48
64
- template <typename CharT, typename Test<CharT>::Sub TestSub >
65
- TEST_CONSTEXPR_CXX14 void testCases (const CharT* s) {
66
- std::basic_string_view<CharT> sv(s) ;
49
+ template <typename CharT>
50
+ void test (const CharT* s) {
51
+ typedef std::basic_string_view<CharT> string_view_t ;
67
52
68
- testDetail<CharT, TestSub>(sv, 0, 0);
69
- testDetail<CharT, TestSub>(sv, 1, 0);
70
- testDetail<CharT, TestSub>(sv, 20, 0);
71
- testDetail<CharT, TestSub>(sv, sv.size(), 0);
53
+ string_view_t sv1 (s);
72
54
73
- testDetail<CharT, TestSub>(sv, 100, 3);
55
+ test1 (sv1, 0 , 0 );
56
+ test1 (sv1, 1 , 0 );
57
+ test1 (sv1, 20 , 0 );
58
+ test1 (sv1, sv1.size (), 0 );
74
59
75
- testDetail<CharT, TestSub>(sv , 0, std::basic_string_view<CharT>::npos );
76
- testDetail<CharT, TestSub>(sv , 2, std::basic_string_view<CharT>::npos );
77
- testDetail<CharT, TestSub>(sv, sv.size(), std::basic_string_view<CharT>::npos );
60
+ test1 (sv1 , 0 , 3 );
61
+ test1 (sv1 , 2 , 3 );
62
+ test1 (sv1, 100 , 3 );
78
63
79
- // Test if exceptions are thrown correctly.
80
- testDetail<CharT, TestSub>(sv, sv.size() + 1, 0);
81
- testDetail<CharT, TestSub>(sv, sv.size() + 1, 1);
82
- testDetail<CharT, TestSub>(sv, sv.size() + 1, std::basic_string_view<CharT>::npos);
83
- }
64
+ test1 (sv1, 0 , string_view_t ::npos);
65
+ test1 (sv1, 2 , string_view_t ::npos);
66
+ test1 (sv1, sv1.size (), string_view_t ::npos);
84
67
85
- template <typename CharT>
86
- TEST_CONSTEXPR_CXX14 void testSubs(const CharT* s) {
87
- testCases<CharT, &std::basic_string_view<CharT>::substr>(s);
88
- # if TEST_STD_VER >= 26
89
- testCases<CharT, &std::basic_string_view<CharT>::subview>(s);
90
- # endif
91
- }
92
-
93
- # define CS(S) MAKE_CSTRING(CharT, S)
94
-
95
- template <typename CharT>
96
- TEST_CONSTEXPR_CXX14 void test() {
97
- testSubs(
98
- CS("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE"));
99
- testSubs(CS("ABCDE"));
100
- testSubs(CS("a"));
101
- testSubs(CS(""));
102
- }
103
-
104
- TEST_CONSTEXPR_CXX14 bool test() {
105
- test<char>();
106
- # ifndef TEST_HAS_NO_WIDE_CHARACTERS
107
- test<wchar_t>();
108
- # endif
109
- # if TEST_STD_VER >= 11
110
- # ifndef TEST_HAS_NO_CHAR8_T
111
- test<char8_t>();
112
- # endif
113
- test<char16_t>();
114
- test<char32_t>();
115
- # endif
116
-
117
- return true;
68
+ test1 (sv1, sv1.size () + 1 , 0 );
69
+ test1 (sv1, sv1.size () + 1 , 1 );
70
+ test1 (sv1, sv1.size () + 1 , string_view_t ::npos);
118
71
}
119
72
120
73
int main (int , char **) {
121
- test();
122
- # if TEST_STD_VER >= 14
123
- // static_assert( test() );
124
- # endif
74
+ test (" ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE " );
75
+ test ( " ABCDE " );
76
+ test (" a " );
77
+ test ( " " );
125
78
126
- return 0;
127
- }
79
+ #ifndef TEST_HAS_NO_WIDE_CHARACTERS
80
+ test (L" ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
81
+ test (L" ABCDE" );
82
+ test (L" a" );
83
+ test (L" " );
128
84
#endif
129
85
130
- #include < cassert>
131
- #include < string>
132
- #include < utility>
133
-
134
- #include " constexpr_char_traits.h"
135
- #include " make_string.h"
136
- #include " test_macros.h"
86
+ #if TEST_STD_VER >= 11
87
+ test (u" ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
88
+ test (u" ABCDE" );
89
+ test (u" a" );
90
+ test (u" " );
137
91
138
- #define CS (S ) MAKE_CSTRING(CharT, S)
139
-
140
- template <typename CharT, typename TraitsT>
141
- struct Test {
142
- typedef std::basic_string_view<CharT, TraitsT> (std::basic_string_view<CharT, TraitsT>::*Sub)(
143
- typename std::basic_string_view<CharT, TraitsT>::size_type,
144
- typename std::basic_string_view<CharT, TraitsT>::size_type) const ;
145
- };
146
-
147
- template <typename CharT, typename TraitsT, typename Test<CharT, TraitsT>::Sub TestSub>
148
- TEST_CONSTEXPR_CXX14 void test () {
149
- const std::basic_string_view<CharT, TraitsT> sv = CS (" Hello cruel world!" );
150
-
151
- // With a default position and a character length.
152
- assert ((sv.*TestSub)(0 , std::basic_string_view<CharT, TraitsT>::npos) == CS (" Hello cruel world!" ));
153
-
154
- // With a explict position and a character length.
155
- assert ((sv.*TestSub)(6 , 5 ) == CS (" cruel" ));
156
-
157
- // From the beginning of the string with a explicit character length.
158
- assert ((sv.*TestSub)(0 , 5 ) == CS (" Hello" ));
159
-
160
- // To the end of string with the default character length.
161
- assert ((sv.*TestSub)(12 , std::basic_string_view<CharT, TraitsT>::npos) == CS (" world!" ));
92
+ test (U" ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
93
+ test (U" ABCDE" );
94
+ test (U" a" );
95
+ test (U" " );
96
+ #endif
162
97
163
- // From the beginning to the end of the string with explicit values.
164
- assert ((sv.*TestSub)(0 , sv.size ()) == CS (" Hello cruel world!" ));
98
+ #if TEST_STD_VER > 11
99
+ {
100
+ constexpr std::string_view sv1{" ABCDE" , 5 };
165
101
166
- // Test if exceptions are thrown correctly.
167
- #ifndef TEST_HAS_NO_EXCEPTIONS
168
- if (!TEST_IS_CONSTANT_EVALUATED) {
169
- { // With a position that is out of range.
170
- try {
171
- (sv.*TestSub)(sv.size () + 1 , std::basic_string_view<CharT, TraitsT>::npos);
172
- assert (false && " Expected std::out_of_range exception" );
173
- } catch (const std::out_of_range&) {
174
- // Expected exception
175
- }
102
+ {
103
+ constexpr std::string_view sv2 = sv1.substr (0 , 3 );
104
+ static_assert (sv2.size () == 3 , " " );
105
+ static_assert (sv2[0 ] == ' A' , " " );
106
+ static_assert (sv2[1 ] == ' B' , " " );
107
+ static_assert (sv2[2 ] == ' C' , " " );
176
108
}
177
109
178
- { // With a position that is out of range and a 0 character length.
179
- try {
180
- (sv.*TestSub)(sv.size () + 1 , 0 );
181
- assert (false && " Expected std::out_of_range exception" );
182
- } catch (const std::out_of_range&) {
183
- // Expected exception
184
- }
110
+ {
111
+ constexpr std::string_view sv2 = sv1.substr (3 , 0 );
112
+ static_assert (sv2.size () == 0 , " " );
185
113
}
186
114
187
- { // With a position that is out of range and a some character length.
188
- try {
189
- (sv.*TestSub)(sv.size () + 1 , 1 );
190
- assert (false && " Expected std::out_of_range exception" );
191
- } catch (const std::out_of_range&) {
192
- // Expected exception
193
- }
115
+ {
116
+ constexpr std::string_view sv2 = sv1.substr (3 , 3 );
117
+ static_assert (sv2.size () == 2 , " " );
118
+ static_assert (sv2[0 ] == ' D' , " " );
119
+ static_assert (sv2[1 ] == ' E' , " " );
194
120
}
195
121
}
196
122
#endif
197
- }
198
-
199
- template <typename CharT>
200
- TEST_CONSTEXPR_CXX14 void test () {
201
- ASSERT_SAME_TYPE (std::basic_string_view<CharT>, decltype (std::declval<std::basic_string_view<CharT> >().substr ()));
202
- test<CharT, std::char_traits<CharT>, &std::basic_string_view<CharT, std::char_traits<CharT> >::substr>();
203
- test<CharT, constexpr_char_traits<CharT>, &std::basic_string_view<CharT, constexpr_char_traits<CharT> >::substr>();
204
- #if TEST_STD_VER >= 26
205
- ASSERT_SAME_TYPE (std::basic_string_view<CharT>, decltype (std::declval<std::basic_string_view<CharT> >().subview ()));
206
- test<CharT, std::char_traits<CharT>, &std::basic_string_view<CharT, std::char_traits<CharT>>::subview>();
207
- test<CharT, constexpr_char_traits<CharT>, &std::basic_string_view<CharT, constexpr_char_traits<CharT> >::subview>();
208
- #endif
209
- }
210
-
211
- TEST_CONSTEXPR_CXX14 bool test () {
212
- test<char >();
213
- #ifndef TEST_HAS_NO_WIDE_CHARACTERS
214
- test<wchar_t >();
215
- #endif
216
- #ifndef TEST_HAS_NO_CHAR8_T
217
- test<char8_t >();
218
- #endif
219
- test<char16_t >();
220
- test<char32_t >();
221
-
222
- return true ;
223
- }
224
-
225
- int main (int , char **) {
226
- test ();
227
- #if TEST_STD_VER >= 14
228
- static_assert (test ());
229
- #endif
230
123
231
124
return 0 ;
232
- }
125
+ }
0 commit comments