]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/string_view
libstdc++: allow string_view insertion to work with <iosfwd> (PR 94051)
[thirdparty/gcc.git] / libstdc++-v3 / include / std / string_view
1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
2
3 // Copyright (C) 2013-2020 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file string_view
26 * This is a Standard C++ Library header.
27 */
28
29 //
30 // N3762 basic_string_view library
31 //
32
33 #ifndef _GLIBCXX_STRING_VIEW
34 #define _GLIBCXX_STRING_VIEW 1
35
36 #pragma GCC system_header
37
38 #if __cplusplus >= 201703L
39
40 #include <iosfwd>
41 #include <bits/char_traits.h>
42 #include <bits/functional_hash.h>
43 #include <bits/int_limits.h>
44 #include <bits/range_access.h>
45 #include <bits/ostream_insert.h>
46
47 namespace std _GLIBCXX_VISIBILITY(default)
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50
51 #define __cpp_lib_string_view 201803
52
53 // Helper for basic_string and basic_string_view members.
54 constexpr size_t
55 __sv_check(size_t __size, size_t __pos, const char* __s)
56 {
57 if (__pos > __size)
58 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > __size "
59 "(which is %zu)"), __s, __pos, __size);
60 return __pos;
61 }
62
63 // Helper for basic_string members.
64 // NB: __sv_limit doesn't check for a bad __pos value.
65 constexpr size_t
66 __sv_limit(size_t __size, size_t __pos, size_t __off) noexcept
67 {
68 const bool __testoff = __off < __size - __pos;
69 return __testoff ? __off : __size - __pos;
70 }
71
72 /**
73 * @class basic_string_view <string_view>
74 * @brief A non-owning reference to a string.
75 *
76 * @ingroup strings
77 * @ingroup sequences
78 *
79 * @tparam _CharT Type of character
80 * @tparam _Traits Traits for character type, defaults to
81 * char_traits<_CharT>.
82 *
83 * A basic_string_view looks like this:
84 *
85 * @code
86 * _CharT* _M_str
87 * size_t _M_len
88 * @endcode
89 */
90 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
91 class basic_string_view
92 {
93 static_assert(!is_array_v<_CharT>);
94 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
95 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
96
97 public:
98
99 // types
100 using traits_type = _Traits;
101 using value_type = _CharT;
102 using pointer = value_type*;
103 using const_pointer = const value_type*;
104 using reference = value_type&;
105 using const_reference = const value_type&;
106 using const_iterator = const value_type*;
107 using iterator = const_iterator;
108 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
109 using reverse_iterator = const_reverse_iterator;
110 using size_type = size_t;
111 using difference_type = ptrdiff_t;
112 static constexpr size_type npos = size_type(-1);
113
114 // [string.view.cons], construction and assignment
115
116 constexpr
117 basic_string_view() noexcept
118 : _M_len{0}, _M_str{nullptr}
119 { }
120
121 constexpr basic_string_view(const basic_string_view&) noexcept = default;
122
123 __attribute__((__nonnull__)) constexpr
124 basic_string_view(const _CharT* __str) noexcept
125 : _M_len{traits_type::length(__str)},
126 _M_str{__str}
127 { }
128
129 constexpr
130 basic_string_view(const _CharT* __str, size_type __len) noexcept
131 : _M_len{__len}, _M_str{__str}
132 { }
133
134 #if __cplusplus > 201703L && __cpp_lib_concepts
135 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
136 requires same_as<iter_value_t<_It>, _CharT>
137 && (!convertible_to<_End, size_type>)
138 constexpr
139 basic_string_view(_It __first, _End __last)
140 : _M_len(__last - __first), _M_str(std::to_address(__first))
141 { }
142 #endif
143
144 constexpr basic_string_view&
145 operator=(const basic_string_view&) noexcept = default;
146
147 // [string.view.iterators], iterator support
148
149 constexpr const_iterator
150 begin() const noexcept
151 { return this->_M_str; }
152
153 constexpr const_iterator
154 end() const noexcept
155 { return this->_M_str + this->_M_len; }
156
157 constexpr const_iterator
158 cbegin() const noexcept
159 { return this->_M_str; }
160
161 constexpr const_iterator
162 cend() const noexcept
163 { return this->_M_str + this->_M_len; }
164
165 constexpr const_reverse_iterator
166 rbegin() const noexcept
167 { return const_reverse_iterator(this->end()); }
168
169 constexpr const_reverse_iterator
170 rend() const noexcept
171 { return const_reverse_iterator(this->begin()); }
172
173 constexpr const_reverse_iterator
174 crbegin() const noexcept
175 { return const_reverse_iterator(this->end()); }
176
177 constexpr const_reverse_iterator
178 crend() const noexcept
179 { return const_reverse_iterator(this->begin()); }
180
181 // [string.view.capacity], capacity
182
183 constexpr size_type
184 size() const noexcept
185 { return this->_M_len; }
186
187 constexpr size_type
188 length() const noexcept
189 { return _M_len; }
190
191 constexpr size_type
192 max_size() const noexcept
193 {
194 return (npos - sizeof(size_type) - sizeof(void*))
195 / sizeof(value_type) / 4;
196 }
197
198 [[nodiscard]] constexpr bool
199 empty() const noexcept
200 { return this->_M_len == 0; }
201
202 // [string.view.access], element access
203
204 constexpr const_reference
205 operator[](size_type __pos) const noexcept
206 {
207 // TODO: Assert to restore in a way compatible with the constexpr.
208 // __glibcxx_assert(__pos < this->_M_len);
209 return *(this->_M_str + __pos);
210 }
211
212 constexpr const_reference
213 at(size_type __pos) const
214 {
215 if (__pos >= _M_len)
216 __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
217 "(which is %zu) >= this->size() "
218 "(which is %zu)"), __pos, this->size());
219 return *(this->_M_str + __pos);
220 }
221
222 constexpr const_reference
223 front() const noexcept
224 {
225 // TODO: Assert to restore in a way compatible with the constexpr.
226 // __glibcxx_assert(this->_M_len > 0);
227 return *this->_M_str;
228 }
229
230 constexpr const_reference
231 back() const noexcept
232 {
233 // TODO: Assert to restore in a way compatible with the constexpr.
234 // __glibcxx_assert(this->_M_len > 0);
235 return *(this->_M_str + this->_M_len - 1);
236 }
237
238 constexpr const_pointer
239 data() const noexcept
240 { return this->_M_str; }
241
242 // [string.view.modifiers], modifiers:
243
244 constexpr void
245 remove_prefix(size_type __n) noexcept
246 {
247 __glibcxx_assert(this->_M_len >= __n);
248 this->_M_str += __n;
249 this->_M_len -= __n;
250 }
251
252 constexpr void
253 remove_suffix(size_type __n) noexcept
254 { this->_M_len -= __n; }
255
256 constexpr void
257 swap(basic_string_view& __sv) noexcept
258 {
259 auto __tmp = *this;
260 *this = __sv;
261 __sv = __tmp;
262 }
263
264 // [string.view.ops], string operations:
265
266 size_type
267 copy(_CharT* __str, size_type __n, size_type __pos = 0) const
268 {
269 __glibcxx_requires_string_len(__str, __n);
270 __pos = std::__sv_check(size(), __pos, "basic_string_view::copy");
271 const size_type __rlen = std::min(__n, _M_len - __pos);
272 // _GLIBCXX_RESOLVE_LIB_DEFECTS
273 // 2777. basic_string_view::copy should use char_traits::copy
274 traits_type::copy(__str, data() + __pos, __rlen);
275 return __rlen;
276 }
277
278 constexpr basic_string_view
279 substr(size_type __pos = 0, size_type __n = npos) const noexcept(false)
280 {
281 __pos = std::__sv_check(size(), __pos, "basic_string_view::substr");
282 const size_type __rlen = std::min(__n, _M_len - __pos);
283 return basic_string_view{_M_str + __pos, __rlen};
284 }
285
286 constexpr int
287 compare(basic_string_view __str) const noexcept
288 {
289 const size_type __rlen = std::min(this->_M_len, __str._M_len);
290 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
291 if (__ret == 0)
292 __ret = _S_compare(this->_M_len, __str._M_len);
293 return __ret;
294 }
295
296 constexpr int
297 compare(size_type __pos1, size_type __n1, basic_string_view __str) const
298 { return this->substr(__pos1, __n1).compare(__str); }
299
300 constexpr int
301 compare(size_type __pos1, size_type __n1,
302 basic_string_view __str, size_type __pos2, size_type __n2) const
303 {
304 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
305 }
306
307 __attribute__((__nonnull__)) constexpr int
308 compare(const _CharT* __str) const noexcept
309 { return this->compare(basic_string_view{__str}); }
310
311 __attribute__((__nonnull__)) constexpr int
312 compare(size_type __pos1, size_type __n1, const _CharT* __str) const
313 { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
314
315 constexpr int
316 compare(size_type __pos1, size_type __n1,
317 const _CharT* __str, size_type __n2) const noexcept(false)
318 {
319 return this->substr(__pos1, __n1)
320 .compare(basic_string_view(__str, __n2));
321 }
322
323 #if __cplusplus > 201703L
324 constexpr bool
325 starts_with(basic_string_view __x) const noexcept
326 { return this->substr(0, __x.size()) == __x; }
327
328 constexpr bool
329 starts_with(_CharT __x) const noexcept
330 { return !this->empty() && traits_type::eq(this->front(), __x); }
331
332 constexpr bool
333 starts_with(const _CharT* __x) const noexcept
334 { return this->starts_with(basic_string_view(__x)); }
335
336 constexpr bool
337 ends_with(basic_string_view __x) const noexcept
338 {
339 return this->size() >= __x.size()
340 && this->compare(this->size() - __x.size(), npos, __x) == 0;
341 }
342
343 constexpr bool
344 ends_with(_CharT __x) const noexcept
345 { return !this->empty() && traits_type::eq(this->back(), __x); }
346
347 constexpr bool
348 ends_with(const _CharT* __x) const noexcept
349 { return this->ends_with(basic_string_view(__x)); }
350 #endif // C++20
351
352 // [string.view.find], searching
353
354 constexpr size_type
355 find(basic_string_view __str, size_type __pos = 0) const noexcept
356 { return this->find(__str._M_str, __pos, __str._M_len); }
357
358 constexpr size_type
359 find(_CharT __c, size_type __pos = 0) const noexcept;
360
361 constexpr size_type
362 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
363
364 __attribute__((__nonnull__)) constexpr size_type
365 find(const _CharT* __str, size_type __pos = 0) const noexcept
366 { return this->find(__str, __pos, traits_type::length(__str)); }
367
368 constexpr size_type
369 rfind(basic_string_view __str, size_type __pos = npos) const noexcept
370 { return this->rfind(__str._M_str, __pos, __str._M_len); }
371
372 constexpr size_type
373 rfind(_CharT __c, size_type __pos = npos) const noexcept;
374
375 constexpr size_type
376 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
377
378 __attribute__((__nonnull__)) constexpr size_type
379 rfind(const _CharT* __str, size_type __pos = npos) const noexcept
380 { return this->rfind(__str, __pos, traits_type::length(__str)); }
381
382 constexpr size_type
383 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
384 { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
385
386 constexpr size_type
387 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
388 { return this->find(__c, __pos); }
389
390 constexpr size_type
391 find_first_of(const _CharT* __str, size_type __pos,
392 size_type __n) const noexcept;
393
394 __attribute__((__nonnull__)) constexpr size_type
395 find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
396 { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
397
398 constexpr size_type
399 find_last_of(basic_string_view __str,
400 size_type __pos = npos) const noexcept
401 { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
402
403 constexpr size_type
404 find_last_of(_CharT __c, size_type __pos=npos) const noexcept
405 { return this->rfind(__c, __pos); }
406
407 constexpr size_type
408 find_last_of(const _CharT* __str, size_type __pos,
409 size_type __n) const noexcept;
410
411 __attribute__((__nonnull__)) constexpr size_type
412 find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
413 { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
414
415 constexpr size_type
416 find_first_not_of(basic_string_view __str,
417 size_type __pos = 0) const noexcept
418 { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
419
420 constexpr size_type
421 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
422
423 constexpr size_type
424 find_first_not_of(const _CharT* __str,
425 size_type __pos, size_type __n) const noexcept;
426
427 __attribute__((__nonnull__)) constexpr size_type
428 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
429 {
430 return this->find_first_not_of(__str, __pos,
431 traits_type::length(__str));
432 }
433
434 constexpr size_type
435 find_last_not_of(basic_string_view __str,
436 size_type __pos = npos) const noexcept
437 { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
438
439 constexpr size_type
440 find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
441
442 constexpr size_type
443 find_last_not_of(const _CharT* __str,
444 size_type __pos, size_type __n) const noexcept;
445
446 __attribute__((__nonnull__)) constexpr size_type
447 find_last_not_of(const _CharT* __str,
448 size_type __pos = npos) const noexcept
449 {
450 return this->find_last_not_of(__str, __pos,
451 traits_type::length(__str));
452 }
453
454 private:
455
456 static constexpr int
457 _S_compare(size_type __n1, size_type __n2) noexcept
458 {
459 const difference_type __diff = __n1 - __n2;
460 if (__diff > __detail::__int_limits<int>::max())
461 return __detail::__int_limits<int>::max();
462 if (__diff < __detail::__int_limits<int>::min())
463 return __detail::__int_limits<int>::min();
464 return static_cast<int>(__diff);
465 }
466
467 size_t _M_len;
468 const _CharT* _M_str;
469 };
470
471 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
472 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
473 basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
474 #endif
475
476 // [string.view.comparison], non-member basic_string_view comparison function
477
478 // Several of these functions use type_identity_t to create a non-deduced
479 // context, so that only one argument participates in template argument
480 // deduction and the other argument gets implicitly converted to the deduced
481 // type (see N3766).
482
483 template<typename _CharT, typename _Traits>
484 constexpr bool
485 operator==(basic_string_view<_CharT, _Traits> __x,
486 basic_string_view<_CharT, _Traits> __y) noexcept
487 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
488
489 template<typename _CharT, typename _Traits>
490 constexpr bool
491 operator==(basic_string_view<_CharT, _Traits> __x,
492 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
493 noexcept
494 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
495
496 template<typename _CharT, typename _Traits>
497 constexpr bool
498 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
499 basic_string_view<_CharT, _Traits> __y) noexcept
500 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
501
502 template<typename _CharT, typename _Traits>
503 constexpr bool
504 operator!=(basic_string_view<_CharT, _Traits> __x,
505 basic_string_view<_CharT, _Traits> __y) noexcept
506 { return !(__x == __y); }
507
508 template<typename _CharT, typename _Traits>
509 constexpr bool
510 operator!=(basic_string_view<_CharT, _Traits> __x,
511 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
512 noexcept
513 { return !(__x == __y); }
514
515 template<typename _CharT, typename _Traits>
516 constexpr bool
517 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
518 basic_string_view<_CharT, _Traits> __y) noexcept
519 { return !(__x == __y); }
520
521 template<typename _CharT, typename _Traits>
522 constexpr bool
523 operator< (basic_string_view<_CharT, _Traits> __x,
524 basic_string_view<_CharT, _Traits> __y) noexcept
525 { return __x.compare(__y) < 0; }
526
527 template<typename _CharT, typename _Traits>
528 constexpr bool
529 operator< (basic_string_view<_CharT, _Traits> __x,
530 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
531 noexcept
532 { return __x.compare(__y) < 0; }
533
534 template<typename _CharT, typename _Traits>
535 constexpr bool
536 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
537 basic_string_view<_CharT, _Traits> __y) noexcept
538 { return __x.compare(__y) < 0; }
539
540 template<typename _CharT, typename _Traits>
541 constexpr bool
542 operator> (basic_string_view<_CharT, _Traits> __x,
543 basic_string_view<_CharT, _Traits> __y) noexcept
544 { return __x.compare(__y) > 0; }
545
546 template<typename _CharT, typename _Traits>
547 constexpr bool
548 operator> (basic_string_view<_CharT, _Traits> __x,
549 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
550 noexcept
551 { return __x.compare(__y) > 0; }
552
553 template<typename _CharT, typename _Traits>
554 constexpr bool
555 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
556 basic_string_view<_CharT, _Traits> __y) noexcept
557 { return __x.compare(__y) > 0; }
558
559 template<typename _CharT, typename _Traits>
560 constexpr bool
561 operator<=(basic_string_view<_CharT, _Traits> __x,
562 basic_string_view<_CharT, _Traits> __y) noexcept
563 { return __x.compare(__y) <= 0; }
564
565 template<typename _CharT, typename _Traits>
566 constexpr bool
567 operator<=(basic_string_view<_CharT, _Traits> __x,
568 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
569 noexcept
570 { return __x.compare(__y) <= 0; }
571
572 template<typename _CharT, typename _Traits>
573 constexpr bool
574 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
575 basic_string_view<_CharT, _Traits> __y) noexcept
576 { return __x.compare(__y) <= 0; }
577
578 template<typename _CharT, typename _Traits>
579 constexpr bool
580 operator>=(basic_string_view<_CharT, _Traits> __x,
581 basic_string_view<_CharT, _Traits> __y) noexcept
582 { return __x.compare(__y) >= 0; }
583
584 template<typename _CharT, typename _Traits>
585 constexpr bool
586 operator>=(basic_string_view<_CharT, _Traits> __x,
587 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
588 noexcept
589 { return __x.compare(__y) >= 0; }
590
591 template<typename _CharT, typename _Traits>
592 constexpr bool
593 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
594 basic_string_view<_CharT, _Traits> __y) noexcept
595 { return __x.compare(__y) >= 0; }
596
597 // [string.view.io], Inserters and extractors
598 template<typename _CharT, typename _Traits>
599 inline basic_ostream<_CharT, _Traits>&
600 operator<<(basic_ostream<_CharT, _Traits>& __os,
601 basic_string_view<_CharT,_Traits> __str)
602 { return __ostream_insert(__os, __str.data(), __str.size()); }
603
604
605 // basic_string_view typedef names
606
607 using string_view = basic_string_view<char>;
608 #ifdef _GLIBCXX_USE_WCHAR_T
609 using wstring_view = basic_string_view<wchar_t>;
610 #endif
611 #ifdef _GLIBCXX_USE_CHAR8_T
612 using u8string_view = basic_string_view<char8_t>;
613 #endif
614 using u16string_view = basic_string_view<char16_t>;
615 using u32string_view = basic_string_view<char32_t>;
616
617 // [string.view.hash], hash support:
618
619 template<typename _Tp>
620 struct hash;
621
622 template<>
623 struct hash<string_view>
624 : public __hash_base<size_t, string_view>
625 {
626 size_t
627 operator()(const string_view& __str) const noexcept
628 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
629 };
630
631 template<>
632 struct __is_fast_hash<hash<string_view>> : std::false_type
633 { };
634
635 #ifdef _GLIBCXX_USE_WCHAR_T
636 template<>
637 struct hash<wstring_view>
638 : public __hash_base<size_t, wstring_view>
639 {
640 size_t
641 operator()(const wstring_view& __s) const noexcept
642 { return std::_Hash_impl::hash(__s.data(),
643 __s.length() * sizeof(wchar_t)); }
644 };
645
646 template<>
647 struct __is_fast_hash<hash<wstring_view>> : std::false_type
648 { };
649 #endif
650
651 #ifdef _GLIBCXX_USE_CHAR8_T
652 template<>
653 struct hash<u8string_view>
654 : public __hash_base<size_t, u8string_view>
655 {
656 size_t
657 operator()(const u8string_view& __str) const noexcept
658 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
659 };
660
661 template<>
662 struct __is_fast_hash<hash<u8string_view>> : std::false_type
663 { };
664 #endif
665
666 template<>
667 struct hash<u16string_view>
668 : public __hash_base<size_t, u16string_view>
669 {
670 size_t
671 operator()(const u16string_view& __s) const noexcept
672 { return std::_Hash_impl::hash(__s.data(),
673 __s.length() * sizeof(char16_t)); }
674 };
675
676 template<>
677 struct __is_fast_hash<hash<u16string_view>> : std::false_type
678 { };
679
680 template<>
681 struct hash<u32string_view>
682 : public __hash_base<size_t, u32string_view>
683 {
684 size_t
685 operator()(const u32string_view& __s) const noexcept
686 { return std::_Hash_impl::hash(__s.data(),
687 __s.length() * sizeof(char32_t)); }
688 };
689
690 template<>
691 struct __is_fast_hash<hash<u32string_view>> : std::false_type
692 { };
693
694 inline namespace literals
695 {
696 inline namespace string_view_literals
697 {
698 #pragma GCC diagnostic push
699 #pragma GCC diagnostic ignored "-Wliteral-suffix"
700 inline constexpr basic_string_view<char>
701 operator""sv(const char* __str, size_t __len) noexcept
702 { return basic_string_view<char>{__str, __len}; }
703
704 #ifdef _GLIBCXX_USE_WCHAR_T
705 inline constexpr basic_string_view<wchar_t>
706 operator""sv(const wchar_t* __str, size_t __len) noexcept
707 { return basic_string_view<wchar_t>{__str, __len}; }
708 #endif
709
710 #ifdef _GLIBCXX_USE_CHAR8_T
711 inline constexpr basic_string_view<char8_t>
712 operator""sv(const char8_t* __str, size_t __len) noexcept
713 { return basic_string_view<char8_t>{__str, __len}; }
714 #endif
715
716 inline constexpr basic_string_view<char16_t>
717 operator""sv(const char16_t* __str, size_t __len) noexcept
718 { return basic_string_view<char16_t>{__str, __len}; }
719
720 inline constexpr basic_string_view<char32_t>
721 operator""sv(const char32_t* __str, size_t __len) noexcept
722 { return basic_string_view<char32_t>{__str, __len}; }
723
724 #pragma GCC diagnostic pop
725 } // namespace string_literals
726 } // namespace literals
727
728 #if __cpp_lib_concepts
729 namespace ranges
730 {
731 // Opt-in to borrowed_range concept
732 template<typename _CharT, typename _Traits>
733 inline constexpr bool
734 enable_borrowed_range<basic_string_view<_CharT, _Traits>> = true;
735
736 // Opt-in to view concept
737 template<typename _CharT, typename _Traits>
738 inline constexpr bool
739 enable_view<basic_string_view<_CharT, _Traits>> = true;
740 }
741 #endif
742 _GLIBCXX_END_NAMESPACE_VERSION
743 } // namespace std
744
745 #include <bits/string_view.tcc>
746
747 #endif // __cplusplus <= 201402L
748
749 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW