1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file experimental/string_view
26 * This is a TS C++ Library header.
30 // N3762 basic_string_view library
33 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW
34 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201402L
42 #include <experimental/bits/lfts_config.h>
44 namespace std _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 namespace experimental
50 inline namespace fundamentals_v1
52 #define __cpp_lib_experimental_string_view 201411
55 * @class basic_string_view <experimental/string_view>
56 * @brief A non-owning reference to a string.
60 * @ingroup experimental
62 * @tparam _CharT Type of character
63 * @tparam _Traits Traits for character type, defaults to
64 * char_traits<_CharT>.
66 * A basic_string_view looks like this:
73 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
74 class basic_string_view
79 using traits_type = _Traits;
80 using value_type = _CharT;
81 using pointer = const _CharT*;
82 using const_pointer = const _CharT*;
83 using reference = const _CharT&;
84 using const_reference = const _CharT&;
85 using const_iterator = const _CharT*;
86 using iterator = const_iterator;
87 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
88 using reverse_iterator = const_reverse_iterator;
89 using size_type = size_t;
90 using difference_type = ptrdiff_t;
91 static constexpr size_type npos = size_type(-1);
93 // [string.view.cons], construct/copy
96 basic_string_view() noexcept
97 : _M_len{0}, _M_str{nullptr}
100 constexpr basic_string_view(const basic_string_view&) noexcept = default;
102 template<typename _Allocator>
103 basic_string_view(const basic_string<_CharT, _Traits,
104 _Allocator>& __str) noexcept
105 : _M_len{__str.length()}, _M_str{__str.data()}
108 constexpr basic_string_view(const _CharT* __str)
109 : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
113 constexpr basic_string_view(const _CharT* __str, size_type __len)
119 operator=(const basic_string_view&) noexcept = default;
121 // [string.view.iterators], iterators
123 constexpr const_iterator
124 begin() const noexcept
125 { return this->_M_str; }
127 constexpr const_iterator
129 { return this->_M_str + this->_M_len; }
131 constexpr const_iterator
132 cbegin() const noexcept
133 { return this->_M_str; }
135 constexpr const_iterator
136 cend() const noexcept
137 { return this->_M_str + this->_M_len; }
139 const_reverse_iterator
140 rbegin() const noexcept
141 { return const_reverse_iterator(this->end()); }
143 const_reverse_iterator
144 rend() const noexcept
145 { return const_reverse_iterator(this->begin()); }
147 const_reverse_iterator
148 crbegin() const noexcept
149 { return const_reverse_iterator(this->end()); }
151 const_reverse_iterator
152 crend() const noexcept
153 { return const_reverse_iterator(this->begin()); }
155 // [string.view.capacity], capacity
158 size() const noexcept
159 { return this->_M_len; }
162 length() const noexcept
166 max_size() const noexcept
168 return (npos - sizeof(size_type) - sizeof(void*))
169 / sizeof(value_type) / 4;
173 empty() const noexcept
174 { return this->_M_len == 0; }
176 // [string.view.access], element access
178 constexpr const _CharT&
179 operator[](size_type __pos) const
181 __glibcxx_assert(__pos < this->_M_len);
182 return *(this->_M_str + __pos);
185 constexpr const _CharT&
186 at(size_type __pos) const
188 return __pos < this->_M_len
189 ? *(this->_M_str + __pos)
190 : (__throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
191 "(which is %zu) >= this->size() "
193 __pos, this->size()),
197 constexpr const _CharT&
200 __glibcxx_assert(this->_M_len > 0);
201 return *this->_M_str;
204 constexpr const _CharT&
207 __glibcxx_assert(this->_M_len > 0);
208 return *(this->_M_str + this->_M_len - 1);
211 constexpr const _CharT*
212 data() const noexcept
213 { return this->_M_str; }
215 // [string.view.modifiers], modifiers:
218 remove_prefix(size_type __n)
220 __glibcxx_assert(this->_M_len >= __n);
226 remove_suffix(size_type __n)
227 { this->_M_len -= __n; }
230 swap(basic_string_view& __sv) noexcept
238 // [string.view.ops], string operations:
240 template<typename _Allocator>
241 explicit operator basic_string<_CharT, _Traits, _Allocator>() const
243 return { this->_M_str, this->_M_len };
246 template<typename _Allocator = std::allocator<_CharT>>
247 basic_string<_CharT, _Traits, _Allocator>
248 to_string(const _Allocator& __alloc = _Allocator()) const
250 return { this->_M_str, this->_M_len, __alloc };
254 copy(_CharT* __str, size_type __n, size_type __pos = 0) const
256 __glibcxx_requires_string_len(__str, __n);
257 if (__pos > this->_M_len)
258 __throw_out_of_range_fmt(__N("basic_string_view::copy: __pos "
259 "(which is %zu) > this->size() "
261 __pos, this->size());
262 size_type __rlen{std::min(__n, size_type{this->_M_len - __pos})};
263 for (auto __begin = this->_M_str + __pos,
264 __end = __begin + __rlen; __begin != __end;)
265 *__str++ = *__begin++;
270 // [string.view.ops], string operations:
272 constexpr basic_string_view
273 substr(size_type __pos = 0, size_type __n = npos) const
275 return __pos <= this->_M_len
276 ? basic_string_view{this->_M_str + __pos,
277 std::min(__n, size_type{this->_M_len - __pos})}
278 : (__throw_out_of_range_fmt(__N("basic_string_view::substr: __pos "
279 "(which is %zu) > this->size() "
281 __pos, this->size()), basic_string_view{});
285 compare(basic_string_view __str) const noexcept
287 int __ret = traits_type::compare(this->_M_str, __str._M_str,
288 std::min(this->_M_len, __str._M_len));
290 __ret = _S_compare(this->_M_len, __str._M_len);
295 compare(size_type __pos1, size_type __n1, basic_string_view __str) const
296 { return this->substr(__pos1, __n1).compare(__str); }
299 compare(size_type __pos1, size_type __n1,
300 basic_string_view __str, size_type __pos2, size_type __n2) const
301 { return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
304 compare(const _CharT* __str) const noexcept
305 { return this->compare(basic_string_view{__str}); }
308 compare(size_type __pos1, size_type __n1, const _CharT* __str) const
309 { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
312 compare(size_type __pos1, size_type __n1,
313 const _CharT* __str, size_type __n2) const
315 return this->substr(__pos1, __n1)
316 .compare(basic_string_view(__str, __n2));
320 find(basic_string_view __str, size_type __pos = 0) const noexcept
321 { return this->find(__str._M_str, __pos, __str._M_len); }
324 find(_CharT __c, size_type __pos=0) const noexcept;
327 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
330 find(const _CharT* __str, size_type __pos=0) const noexcept
331 { return this->find(__str, __pos, traits_type::length(__str)); }
334 rfind(basic_string_view __str, size_type __pos = npos) const noexcept
335 { return this->rfind(__str._M_str, __pos, __str._M_len); }
338 rfind(_CharT __c, size_type __pos = npos) const noexcept;
341 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
344 rfind(const _CharT* __str, size_type __pos = npos) const noexcept
345 { return this->rfind(__str, __pos, traits_type::length(__str)); }
348 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
349 { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
352 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
353 { return this->find(__c, __pos); }
356 find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
359 find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
360 { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
363 find_last_of(basic_string_view __str,
364 size_type __pos = npos) const noexcept
365 { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
368 find_last_of(_CharT __c, size_type __pos=npos) const noexcept
369 { return this->rfind(__c, __pos); }
372 find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
375 find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
376 { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
379 find_first_not_of(basic_string_view __str,
380 size_type __pos = 0) const noexcept
381 { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
384 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
387 find_first_not_of(const _CharT* __str,
388 size_type __pos, size_type __n) const;
391 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
393 return this->find_first_not_of(__str, __pos,
394 traits_type::length(__str));
398 find_last_not_of(basic_string_view __str,
399 size_type __pos = npos) const noexcept
400 { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
403 find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
406 find_last_not_of(const _CharT* __str,
407 size_type __pos, size_type __n) const;
410 find_last_not_of(const _CharT* __str,
411 size_type __pos = npos) const noexcept
413 return this->find_last_not_of(__str, __pos,
414 traits_type::length(__str));
420 _S_compare(size_type __n1, size_type __n2) noexcept
422 return difference_type(__n1 - __n2) > std::numeric_limits<int>::max()
423 ? std::numeric_limits<int>::max()
424 : difference_type(__n1 - __n2) < std::numeric_limits<int>::min()
425 ? std::numeric_limits<int>::min()
426 : static_cast<int>(difference_type(__n1 - __n2));
430 const _CharT* _M_str;
433 // [string.view.comparison], non-member basic_string_view comparison functions
437 // Identity transform to create a non-deduced context, so that only one
438 // argument participates in template argument deduction and the other
439 // argument gets implicitly converted to the deduced type. See n3766.html.
440 template<typename _Tp>
441 using __idt = common_type_t<_Tp>;
444 template<typename _CharT, typename _Traits>
446 operator==(basic_string_view<_CharT, _Traits> __x,
447 basic_string_view<_CharT, _Traits> __y) noexcept
448 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
450 template<typename _CharT, typename _Traits>
452 operator==(basic_string_view<_CharT, _Traits> __x,
453 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
454 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
456 template<typename _CharT, typename _Traits>
458 operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
459 basic_string_view<_CharT, _Traits> __y) noexcept
460 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
462 template<typename _CharT, typename _Traits>
464 operator!=(basic_string_view<_CharT, _Traits> __x,
465 basic_string_view<_CharT, _Traits> __y) noexcept
466 { return !(__x == __y); }
468 template<typename _CharT, typename _Traits>
470 operator!=(basic_string_view<_CharT, _Traits> __x,
471 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
472 { return !(__x == __y); }
474 template<typename _CharT, typename _Traits>
476 operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
477 basic_string_view<_CharT, _Traits> __y) noexcept
478 { return !(__x == __y); }
480 template<typename _CharT, typename _Traits>
482 operator< (basic_string_view<_CharT, _Traits> __x,
483 basic_string_view<_CharT, _Traits> __y) noexcept
484 { return __x.compare(__y) < 0; }
486 template<typename _CharT, typename _Traits>
488 operator< (basic_string_view<_CharT, _Traits> __x,
489 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
490 { return __x.compare(__y) < 0; }
492 template<typename _CharT, typename _Traits>
494 operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
495 basic_string_view<_CharT, _Traits> __y) noexcept
496 { return __x.compare(__y) < 0; }
498 template<typename _CharT, typename _Traits>
500 operator> (basic_string_view<_CharT, _Traits> __x,
501 basic_string_view<_CharT, _Traits> __y) noexcept
502 { return __x.compare(__y) > 0; }
504 template<typename _CharT, typename _Traits>
506 operator> (basic_string_view<_CharT, _Traits> __x,
507 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
508 { return __x.compare(__y) > 0; }
510 template<typename _CharT, typename _Traits>
512 operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
513 basic_string_view<_CharT, _Traits> __y) noexcept
514 { return __x.compare(__y) > 0; }
516 template<typename _CharT, typename _Traits>
518 operator<=(basic_string_view<_CharT, _Traits> __x,
519 basic_string_view<_CharT, _Traits> __y) noexcept
520 { return __x.compare(__y) <= 0; }
522 template<typename _CharT, typename _Traits>
524 operator<=(basic_string_view<_CharT, _Traits> __x,
525 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
526 { return __x.compare(__y) <= 0; }
528 template<typename _CharT, typename _Traits>
530 operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
531 basic_string_view<_CharT, _Traits> __y) noexcept
532 { return __x.compare(__y) <= 0; }
534 template<typename _CharT, typename _Traits>
536 operator>=(basic_string_view<_CharT, _Traits> __x,
537 basic_string_view<_CharT, _Traits> __y) noexcept
538 { return __x.compare(__y) >= 0; }
540 template<typename _CharT, typename _Traits>
542 operator>=(basic_string_view<_CharT, _Traits> __x,
543 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
544 { return __x.compare(__y) >= 0; }
546 template<typename _CharT, typename _Traits>
548 operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
549 basic_string_view<_CharT, _Traits> __y) noexcept
550 { return __x.compare(__y) >= 0; }
552 // [string.view.io], Inserters and extractors
553 template<typename _CharT, typename _Traits>
554 inline basic_ostream<_CharT, _Traits>&
555 operator<<(basic_ostream<_CharT, _Traits>& __os,
556 basic_string_view<_CharT,_Traits> __str)
557 { return __ostream_insert(__os, __str.data(), __str.size()); }
560 // basic_string_view typedef names
562 using string_view = basic_string_view<char>;
563 #ifdef _GLIBCXX_USE_WCHAR_T
564 using wstring_view = basic_string_view<wchar_t>;
566 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
567 using u16string_view = basic_string_view<char16_t>;
568 using u32string_view = basic_string_view<char32_t>;
570 } // namespace fundamentals_v1
571 } // namespace experimental
574 // [string.view.hash], hash support:
575 template<typename _Tp>
579 struct hash<experimental::string_view>
580 : public __hash_base<size_t, experimental::string_view>
583 operator()(const experimental::string_view& __str) const noexcept
584 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
588 struct __is_fast_hash<hash<experimental::string_view>> : std::false_type
591 #ifdef _GLIBCXX_USE_WCHAR_T
593 struct hash<experimental::wstring_view>
594 : public __hash_base<size_t, wstring>
597 operator()(const experimental::wstring_view& __s) const noexcept
598 { return std::_Hash_impl::hash(__s.data(),
599 __s.length() * sizeof(wchar_t)); }
603 struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type
607 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
609 struct hash<experimental::u16string_view>
610 : public __hash_base<size_t, experimental::u16string_view>
613 operator()(const experimental::u16string_view& __s) const noexcept
614 { return std::_Hash_impl::hash(__s.data(),
615 __s.length() * sizeof(char16_t)); }
619 struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type
623 struct hash<experimental::u32string_view>
624 : public __hash_base<size_t, experimental::u32string_view>
627 operator()(const experimental::u32string_view& __s) const noexcept
628 { return std::_Hash_impl::hash(__s.data(),
629 __s.length() * sizeof(char32_t)); }
633 struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type
637 namespace experimental
639 // I added these EMSR.
640 inline namespace literals
642 inline namespace string_view_literals
644 #pragma GCC diagnostic push
645 #pragma GCC diagnostic ignored "-Wliteral-suffix"
646 inline constexpr basic_string_view<char>
647 operator""sv(const char* __str, size_t __len) noexcept
648 { return basic_string_view<char>{__str, __len}; }
650 #ifdef _GLIBCXX_USE_WCHAR_T
651 inline constexpr basic_string_view<wchar_t>
652 operator""sv(const wchar_t* __str, size_t __len) noexcept
653 { return basic_string_view<wchar_t>{__str, __len}; }
656 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
657 inline constexpr basic_string_view<char16_t>
658 operator""sv(const char16_t* __str, size_t __len) noexcept
659 { return basic_string_view<char16_t>{__str, __len}; }
661 inline constexpr basic_string_view<char32_t>
662 operator""sv(const char32_t* __str, size_t __len) noexcept
663 { return basic_string_view<char32_t>{__str, __len}; }
665 #pragma GCC diagnostic pop
666 } // namespace string_literals
667 } // namespace literals
668 } // namespace experimental
670 _GLIBCXX_END_NAMESPACE_VERSION
673 #include <experimental/bits/string_view.tcc>
675 #endif // __cplusplus <= 201103L
677 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW