1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013 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 Standard 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 <= 201103L
39 # include <bits/c++14_warning.h>
42 #include <debug/debug.h>
46 namespace std //_GLIBCXX_VISIBILITY(default)
48 namespace experimental
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 * @class basic_string_view <string_view>
54 * @brief A non-owning reference to a string.
59 * @tparam _CharT Type of character
60 * @tparam _Traits Traits for character type, defaults to
61 * char_traits<_CharT>.
63 * A basic_string_view looks like this:
70 template<typename _CharT, typename _Traits = char_traits<_CharT>>
71 class basic_string_view
77 using traits_type = _Traits;
78 using value_type = _CharT;
79 using pointer = const _CharT*;
80 using const_pointer = const _CharT*;
81 using reference = const _CharT&;
82 using const_reference = const _CharT&;
83 using const_iterator = const _CharT*;
84 using iterator = const_iterator;
85 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
86 using reverse_iterator = const_reverse_iterator;
87 using size_type = size_t;
88 using difference_type = ptrdiff_t;
89 static constexpr size_type npos = size_type(-1);
91 // [string.view.cons], construct/copy
94 basic_string_view() noexcept
95 : _M_len{0}, _M_str{nullptr}
98 constexpr basic_string_view(const basic_string_view&) noexcept = default;
100 template<typename _Allocator>
101 basic_string_view(const basic_string<_CharT, _Traits,
102 _Allocator>& __str) noexcept
103 : _M_len{__str.length()}, _M_str{__str.data()}
106 constexpr basic_string_view(const _CharT* __str)
107 : _M_len{__str == nullptr ? 0 : traits_type::length(__str)}, _M_str{__str}
110 constexpr basic_string_view(const _CharT* __str, size_type __len)
111 : _M_len{__len}, _M_str{__str}
115 operator=(const basic_string_view&) noexcept = default;
117 // [string.view.iterators], iterators
119 constexpr const_iterator
120 begin() const noexcept
121 { return this->_M_str; }
123 constexpr const_iterator
125 { return this->_M_str + this->_M_len; }
127 constexpr const_iterator
128 cbegin() const noexcept
129 { return this->_M_str; }
131 constexpr const_iterator
132 cend() const noexcept
133 { return this->_M_str + this->_M_len; }
135 const_reverse_iterator
136 rbegin() const noexcept
137 { return std::reverse_iterator<const_iterator>(this->end()); }
139 const_reverse_iterator
140 rend() const noexcept
141 { return std::reverse_iterator<const_iterator>(this->begin()); }
143 const_reverse_iterator
144 crbegin() const noexcept
145 { return std::reverse_iterator<const_iterator>(this->end()); }
147 const_reverse_iterator
148 crend() const noexcept
149 { return std::reverse_iterator<const_iterator>(this->begin()); }
151 // [string.view.capacity], capacity
154 size() const noexcept
155 { return this->_M_len; }
158 length() const noexcept
162 max_size() const noexcept
163 { return ((npos - sizeof(size_type) - sizeof(void*))
164 / sizeof(value_type) / 4); }
167 empty() const noexcept
168 { return this->_M_len == 0; }
170 // [string.view.access], element access
172 constexpr const _CharT&
173 operator[](size_type __pos) const
175 _GLIBCXX_DEBUG_ASSERT(__pos <= this->_M_len);
176 return *(this->_M_str + __pos);
179 constexpr const _CharT&
180 at(size_type __pos) const
182 return __pos < this->_M_len
183 ? *(this->_M_str + __pos)
184 : (__throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
185 "(which is %zu) >= this->size() "
187 __pos, this->size()),
188 *static_cast<pointer>(nullptr));
191 constexpr const _CharT&
194 _GLIBCXX_DEBUG_ASSERT(this->_M_len > 0);
195 return *this->_M_str;
198 constexpr const _CharT&
201 _GLIBCXX_DEBUG_ASSERT(this->_M_len > 0);
202 return *(this->_M_str + this->_M_len - 1);
205 constexpr const _CharT*
206 data() const noexcept
207 { return this->_M_str; }
209 // [string.view.modifiers], modifiers:
214 this->_M_str = nullptr;
218 remove_prefix(size_type __n)
220 _GLIBCXX_DEBUG_ASSERT(this->_M_len >= __n);
226 remove_suffix(size_type __n)
227 { this->_M_len -= __n; }
230 swap(basic_string_view& __sv) noexcept
232 std::swap(this->_M_len, __sv._M_len);
233 std::swap(this->_M_str, __sv._M_str);
237 // [string.view.ops], string operations:
239 template<typename _Allocator>
240 explicit operator basic_string<_CharT, _Traits, _Allocator>() const
242 return basic_string<_CharT, _Traits, _Allocator>
243 (this->_M_len, this->_M_str);
247 copy(_CharT* __str, size_type __n, size_type __pos = 0) const
249 __glibcxx_requires_string_len(__str, __n);
250 if (__pos >= this->_M_len)
251 __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
252 "(which is %zu) >= this->size() "
254 __pos, this->size());
255 size_type __rlen{std::min(__n, size_type{this->_M_len - __pos})};
256 for (auto __begin = this->_M_str + __pos,
257 __end = this->_M_str + __rlen; __begin != __end;)
258 *__str++ = *__begin++;
263 // [string.view.ops], string operations:
265 constexpr basic_string_view
266 substr(size_type __pos, size_type __n=npos) const
268 return __pos < this->_M_len
269 ? basic_string_view{this->_M_str + __pos,
270 std::min(__n, size_type{this->_M_len - __pos})}
271 : (__throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
272 "(which is %zu) >= this->size() "
274 __pos, this->size()), basic_string_view{});
278 compare(basic_string_view __str) const noexcept
280 int __ret = traits_type::compare(this->_M_str, __str._M_str,
281 std::min(this->_M_len, __str._M_len));
283 __ret = _S_compare(this->_M_len, __str._M_len);
288 compare(size_type __pos1, size_type __n1, basic_string_view __str) const
289 { return this->substr(__pos1, __n1).compare(__str); }
292 compare(size_type __pos1, size_type __n1,
293 basic_string_view __str, size_type __pos2, size_type __n2) const
294 { return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
297 compare(const _CharT* __str) const noexcept
298 { return this->compare(basic_string_view{__str}); }
301 compare(size_type __pos1, size_type __n1, const _CharT* __str) const
302 { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
305 compare(size_type __pos1, size_type __n1,
306 const _CharT* __str, size_type __n2) const
308 return this->substr(__pos1, __n1)
309 .compare(basic_string_view(__str, __n2));
313 find(basic_string_view __str, size_type __pos = 0) const noexcept
314 { return this->find(__str._M_str, __pos, __str._M_len); }
317 find(_CharT __c, size_type __pos=0) const noexcept;
320 find(const _CharT* __str, size_type __pos, size_type __n) const;
323 find(const _CharT* __str, size_type __pos=0) const noexcept
324 { return this->find(__str, __pos, traits_type::length(__str)); }
327 rfind(basic_string_view __str, size_type __pos = npos) const noexcept
328 { return this->rfind(__str._M_str, __pos, __str._M_len); }
331 rfind(_CharT __c, size_type __pos = npos) const noexcept;
334 rfind(const _CharT* __str, size_type __pos, size_type __n) const;
337 rfind(const _CharT* __str, size_type __pos = npos) const noexcept
338 { return this->rfind(__str, __pos, traits_type::length(__str)); }
341 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
342 { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
345 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
346 { return this->find(__c, __pos); }
349 find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
352 find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
353 { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
356 find_last_of(basic_string_view __str,
357 size_type __pos = npos) const noexcept
358 { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
361 find_last_of(_CharT __c, size_type __pos=npos) const noexcept
362 { return this->rfind(__c, __pos); }
365 find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
368 find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
369 { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
372 find_first_not_of(basic_string_view __str,
373 size_type __pos = 0) const noexcept
374 { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
377 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
380 find_first_not_of(const _CharT* __str,
381 size_type __pos, size_type __n) const;
384 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
386 return this->find_first_not_of(__str, __pos,
387 traits_type::length(__str));
391 find_last_not_of(basic_string_view __str,
392 size_type __pos = npos) const noexcept
393 { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
396 find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
399 find_last_not_of(const _CharT* __str,
400 size_type __pos, size_type __n) const;
403 find_last_not_of(const _CharT* __str,
404 size_type __pos = npos) const noexcept
406 return this->find_last_not_of(__str, __pos,
407 traits_type::length(__str));
411 static constexpr const int
412 _S_compare(size_type __n1, size_type __n2) noexcept
414 return difference_type{__n1 - __n2} > std::numeric_limits<int>::max()
415 ? std::numeric_limits<int>::max()
416 : difference_type{__n1 - __n2} < std::numeric_limits<int>::min()
417 ? std::numeric_limits<int>::min()
418 : static_cast<int>(difference_type{__n1 - __n2});
422 const _CharT* _M_str;
426 // [string.view.comparison], non-member basic_string_view comparison functions
430 // Identity transform to make ADL work with just one argument.
432 template<typename _Tp = void>
434 { typedef _Tp type; };
437 struct __identity<void>;
439 template<typename _Tp>
440 using __idt = typename __identity<_Tp>::type;
443 template<typename _CharT, typename _Traits>
445 operator==(basic_string_view<_CharT, _Traits> __x,
446 basic_string_view<_CharT, _Traits> __y) noexcept
447 { return __x.compare(__y) == 0; }
449 template<typename _CharT, typename _Traits>
451 operator==(basic_string_view<_CharT, _Traits> __x,
452 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
453 { return __x.compare(__y) == 0; }
455 template<typename _CharT, typename _Traits>
457 operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
458 basic_string_view<_CharT, _Traits> __y) noexcept
459 { return __x.compare(__y) == 0; }
461 template<typename _CharT, typename _Traits>
463 operator!=(basic_string_view<_CharT, _Traits> __x,
464 basic_string_view<_CharT, _Traits> __y) noexcept
465 { return !(__x == __y); }
467 template<typename _CharT, typename _Traits>
469 operator!=(basic_string_view<_CharT, _Traits> __x,
470 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
471 { return !(__x == __y); }
473 template<typename _CharT, typename _Traits>
475 operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
476 basic_string_view<_CharT, _Traits> __y) noexcept
477 { return !(__x == __y); }
479 template<typename _CharT, typename _Traits>
481 operator< (basic_string_view<_CharT, _Traits> __x,
482 basic_string_view<_CharT, _Traits> __y) noexcept
483 { return __x.compare(__y) < 0; }
485 template<typename _CharT, typename _Traits>
487 operator< (basic_string_view<_CharT, _Traits> __x,
488 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
489 { return __x.compare(__y) < 0; }
491 template<typename _CharT, typename _Traits>
493 operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
494 basic_string_view<_CharT, _Traits> __y) noexcept
495 { return __x.compare(__y) < 0; }
497 template<typename _CharT, typename _Traits>
499 operator> (basic_string_view<_CharT, _Traits> __x,
500 basic_string_view<_CharT, _Traits> __y) noexcept
501 { return __x.compare(__y) > 0; }
503 template<typename _CharT, typename _Traits>
505 operator> (basic_string_view<_CharT, _Traits> __x,
506 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
507 { return __x.compare(__y) > 0; }
509 template<typename _CharT, typename _Traits>
511 operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
512 basic_string_view<_CharT, _Traits> __y) noexcept
513 { return __x.compare(__y) > 0; }
515 template<typename _CharT, typename _Traits>
517 operator<=(basic_string_view<_CharT, _Traits> __x,
518 basic_string_view<_CharT, _Traits> __y) noexcept
519 { return __x.compare(__y) <= 0; }
521 template<typename _CharT, typename _Traits>
523 operator<=(basic_string_view<_CharT, _Traits> __x,
524 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
525 { return __x.compare(__y) <= 0; }
527 template<typename _CharT, typename _Traits>
529 operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
530 basic_string_view<_CharT, _Traits> __y) noexcept
531 { return __x.compare(__y) <= 0; }
533 template<typename _CharT, typename _Traits>
535 operator>=(basic_string_view<_CharT, _Traits> __x,
536 basic_string_view<_CharT, _Traits> __y) noexcept
537 { return __x.compare(__y) >= 0; }
539 template<typename _CharT, typename _Traits>
541 operator>=(basic_string_view<_CharT, _Traits> __x,
542 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
543 { return __x.compare(__y) >= 0; }
545 template<typename _CharT, typename _Traits>
547 operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
548 basic_string_view<_CharT, _Traits> __y) noexcept
549 { return __x.compare(__y) >= 0; }
551 // [string.view.comparison], sufficient additional overloads of comparison functions
553 // [string.view.nonmem], other non-member basic_string_view functions
554 template<typename _CharT, typename _Traits = char_traits<_CharT>,
555 typename _Allocator = allocator<_CharT>>
556 basic_string<_CharT, _Traits, _Allocator>
557 to_string(basic_string_view<_CharT, _Traits> __str,
558 const _Allocator& __alloc = _Allocator())
560 return basic_string<_CharT, _Traits, _Allocator>
561 (__str.begin(), __str.end(), __alloc);
564 template<typename _CharT, typename _Traits>
565 basic_ostream<_CharT, _Traits>&
566 operator<<(basic_ostream<_CharT, _Traits>& __os,
567 basic_string_view<_CharT,_Traits> __str)
568 { return __ostream_insert(__os, __str.data(), __str.size()); }
571 // basic_string_view typedef names
573 using string_view = basic_string_view<char>;
574 #ifdef _GLIBCXX_USE_WCHAR_T
575 using wstring_view = basic_string_view<wchar_t>;
577 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
578 using u16string_view = basic_string_view<char16_t>;
579 using u32string_view = basic_string_view<char32_t>;
582 _GLIBCXX_END_NAMESPACE_VERSION
583 } // namespace experimental
586 // [string.view.hash], hash support:
588 _GLIBCXX_BEGIN_NAMESPACE_VERSION
589 template<typename _Tp>
593 struct hash<experimental::string_view>
594 : public __hash_base<size_t, experimental::string_view>
597 operator()(const experimental::string_view& __str) const noexcept
598 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
602 struct __is_fast_hash<hash<experimental::string_view>> : std::false_type
605 #ifdef _GLIBCXX_USE_WCHAR_T
607 struct hash<experimental::wstring_view>
608 : public __hash_base<size_t, wstring>
611 operator()(const experimental::wstring_view& __s) const noexcept
612 { return std::_Hash_impl::hash(__s.data(),
613 __s.length() * sizeof(wchar_t)); }
617 struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type
621 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
623 struct hash<experimental::u16string_view>
624 : public __hash_base<size_t, experimental::u16string_view>
627 operator()(const experimental::u16string_view& __s) const noexcept
628 { return std::_Hash_impl::hash(__s.data(),
629 __s.length() * sizeof(char16_t)); }
633 struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type
637 struct hash<experimental::u32string_view>
638 : public __hash_base<size_t, experimental::u32string_view>
641 operator()(const experimental::u32string_view& __s) const noexcept
642 { return std::_Hash_impl::hash(__s.data(),
643 __s.length() * sizeof(char32_t)); }
647 struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type
650 _GLIBCXX_END_NAMESPACE_VERSION
652 namespace experimental
654 _GLIBCXX_BEGIN_NAMESPACE_VERSION
656 // I added these EMSR.
657 inline namespace literals
659 inline namespace string_view_literals
662 inline basic_string_view<char>
663 operator""sv(const char* __str, size_t __len)
664 { return basic_string_view<char>{__str, __len}; }
666 #ifdef _GLIBCXX_USE_WCHAR_T
667 inline basic_string_view<wchar_t>
668 operator""sv(const wchar_t* __str, size_t __len)
669 { return basic_string_view<wchar_t>{__str, __len}; }
672 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
673 inline basic_string_view<char16_t>
674 operator""sv(const char16_t* __str, size_t __len)
675 { return basic_string_view<char16_t>{__str, __len}; }
677 inline basic_string_view<char32_t>
678 operator""sv(const char32_t* __str, size_t __len)
679 { return basic_string_view<char32_t>{__str, __len}; }
685 _GLIBCXX_END_NAMESPACE_VERSION
686 } // namespace experimental
689 #include <experimental/string_view.tcc>
691 #endif // __cplusplus <= 201103L
693 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW