* include/experimental/string_view: Fix inconsistent exception specs.
[official-gcc.git] / libstdc++-v3 / include / experimental / string_view
blob6b6588b97708c364d759abead3b8d9e4f88a03ba
1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013-2014 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.
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.
27  */
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>
40 #else
42 #include <debug/debug.h>
43 #include <string>
44 #include <limits>
46 namespace std _GLIBCXX_VISIBILITY(default)
48 namespace experimental
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52   /**
53    *  @class basic_string_view <string_view>
54    *  @brief  A non-owning reference to a string.
55    *
56    *  @ingroup strings
57    *  @ingroup sequences
58    *
59    *  @tparam _CharT  Type of character
60    *  @tparam _Traits  Traits for character type, defaults to
61    *                   char_traits<_CharT>.
62    *
63    *  A basic_string_view looks like this:
64    *
65    *  @code
66    *    _CharT*    _M_str
67    *    size_t     _M_len
68    *  @endcode
69    *
70    *  A basic_string_view represents an empty string with a static constexpr
71    *  length one string:
72    *
73    *  @code
74    *    static constexpr value_type _S_empty_str[1]{0};
75    *  @endcode
76    */
77   template<typename _CharT, typename _Traits = char_traits<_CharT>>
78     class basic_string_view
79     {
81     public:
83       // types
84       using traits_type = _Traits;
85       using value_type = _CharT;
86       using pointer = const _CharT*;
87       using const_pointer = const _CharT*;
88       using reference = const _CharT&;
89       using const_reference = const _CharT&;
90       using const_iterator = const _CharT*;
91       using iterator = const_iterator;
92       using const_reverse_iterator = std::reverse_iterator<const_iterator>;
93       using reverse_iterator = const_reverse_iterator;
94       using size_type = size_t;
95       using difference_type = ptrdiff_t;
96       static constexpr size_type npos = size_type(-1);
98       // [string.view.cons], construct/copy
100       constexpr
101       basic_string_view() noexcept
102       : _M_len{0}, _M_str{_S_empty_str}
103       { }
105       constexpr basic_string_view(const basic_string_view&) noexcept = default;
107       template<typename _Allocator>
108         basic_string_view(const basic_string<_CharT, _Traits,
109                           _Allocator>& __str) noexcept
110         : _M_len{__str.length()}, _M_str{__str.data()}
111         { }
113       constexpr basic_string_view(const _CharT* __str)
114       : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
115         _M_str{__str == nullptr ? _S_empty_str : __str}
116       { }
118       constexpr basic_string_view(const _CharT* __str, size_type __len)
119       : _M_len{__str == nullptr ? 0 :__len},
120         _M_str{__str == nullptr ? _S_empty_str : __str}
121       { }
123       basic_string_view&
124       operator=(const basic_string_view&) noexcept = default;
126       // [string.view.iterators], iterators
128       constexpr const_iterator
129       begin() const noexcept
130       { return this->_M_str; }
132       constexpr const_iterator
133       end() const noexcept
134       { return this->_M_str + this->_M_len; }
136       constexpr const_iterator
137       cbegin() const noexcept
138       { return this->_M_str; }
140       constexpr const_iterator
141       cend() const noexcept
142       { return this->_M_str + this->_M_len; }
144       const_reverse_iterator
145       rbegin() const noexcept
146       { return std::reverse_iterator<const_iterator>(this->end()); }
148       const_reverse_iterator
149       rend() const noexcept
150       { return std::reverse_iterator<const_iterator>(this->begin()); }
152       const_reverse_iterator
153       crbegin() const noexcept
154       { return std::reverse_iterator<const_iterator>(this->end()); }
156       const_reverse_iterator
157       crend() const noexcept
158       { return std::reverse_iterator<const_iterator>(this->begin()); }
160       // [string.view.capacity], capacity
162       constexpr size_type
163       size() const noexcept
164       { return this->_M_len; }
166       constexpr size_type
167       length() const noexcept
168       { return _M_len; }
170       constexpr size_type
171       max_size() const noexcept
172       { return ((npos - sizeof(size_type) - sizeof(void*))
173                 / sizeof(value_type) / 4); }
175       constexpr bool
176       empty() const noexcept
177       { return this->_M_len == 0; }
179       // [string.view.access], element access
181       constexpr const _CharT&
182       operator[](size_type __pos) const
183       {
184         // TODO: Assert to restore in a way compatible with the constexpr.
185         // _GLIBCXX_DEBUG_ASSERT(__pos < this->_M_len);
186         return *(this->_M_str + __pos);
187       }
189       constexpr const _CharT&
190       at(size_type __pos) const
191       {
192         return __pos < this->_M_len
193              ? *(this->_M_str + __pos)
194              : (__throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
195                                              "(which is %zu) >= this->size() "
196                                              "(which is %zu)"),
197                                          __pos, this->size()),
198                 _S_empty_str[0]);
199       }
201       constexpr const _CharT&
202       front() const
203       {
204         // TODO: Assert to restore in a way compatible with the constexpr.
205         // _GLIBCXX_DEBUG_ASSERT(this->_M_len > 0);
206         return *this->_M_str;
207       }
209       constexpr const _CharT&
210       back() const
211       {
212         // TODO: Assert to restore in a way compatible with the constexpr.
213         // _GLIBCXX_DEBUG_ASSERT(this->_M_len > 0);
214         return *(this->_M_str + this->_M_len - 1);
215       }
217       constexpr const _CharT*
218       data() const noexcept
219       { return this->_M_str; }
221       // [string.view.modifiers], modifiers:
222       void
223       clear() noexcept
224       {
225         this->_M_len = 0;
226         this->_M_str = _S_empty_str;
227       }
229       void
230       remove_prefix(size_type __n)
231       {
232         _GLIBCXX_DEBUG_ASSERT(this->_M_len >= __n);
233         this->_M_str += __n;
234         this->_M_len -= __n;
235       }
237       void
238       remove_suffix(size_type __n)
239       { this->_M_len -= __n; }
241       void
242       swap(basic_string_view& __sv) noexcept
243       {
244         std::swap(this->_M_len, __sv._M_len);
245         std::swap(this->_M_str, __sv._M_str);
246       }
249       // [string.view.ops], string operations:
251       template<typename _Allocator>
252         explicit operator basic_string<_CharT, _Traits, _Allocator>() const
253         {
254           return basic_string<_CharT, _Traits, _Allocator>
255                                         (this->_M_len, this->_M_str);
256         }
258       size_type
259       copy(_CharT* __str, size_type __n, size_type __pos = 0) const
260       {
261         __glibcxx_requires_string_len(__str, __n);
262         if (__pos > this->_M_len)
263           __throw_out_of_range_fmt(__N("basic_string_view::copy: __pos "
264                                        "(which is %zu) > this->size() "
265                                        "(which is %zu)"),
266                                    __pos, this->size());
267         size_type __rlen{std::min(__n, size_type{this->_M_len  - __pos})};
268         for (auto __begin = this->_M_str + __pos,
269              __end = __begin + __rlen; __begin != __end;)
270           *__str++ = *__begin++;
271         return __rlen;
272       }
275       // [string.view.ops], string operations:
277       constexpr basic_string_view
278       substr(size_type __pos, size_type __n=npos) const
279       {
280         return __pos <= this->_M_len
281              ? basic_string_view{this->_M_str + __pos,
282                                 std::min(__n, size_type{this->_M_len  - __pos})}
283              : (__throw_out_of_range_fmt(__N("basic_string_view::substr: __pos "
284                                              "(which is %zu) > this->size() "
285                                              "(which is %zu)"),
286                                      __pos, this->size()), basic_string_view{});
287       }
289       int
290       compare(basic_string_view __str) const noexcept
291       {
292         int __ret = traits_type::compare(this->_M_str, __str._M_str,
293                                          std::min(this->_M_len, __str._M_len));
294         if (__ret == 0)
295           __ret = _S_compare(this->_M_len, __str._M_len);
296         return __ret;
297       }
299       int
300       compare(size_type __pos1, size_type __n1, basic_string_view __str) const
301       { return this->substr(__pos1, __n1).compare(__str); }
303       int
304       compare(size_type __pos1, size_type __n1,
305               basic_string_view __str, size_type __pos2, size_type __n2) const
306       { return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
308       int
309       compare(const _CharT* __str) const noexcept
310       { return this->compare(basic_string_view{__str}); }
312       int
313       compare(size_type __pos1, size_type __n1, const _CharT* __str) const
314       { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
316       int
317       compare(size_type __pos1, size_type __n1,
318               const _CharT* __str, size_type __n2) const
319       {
320         return this->substr(__pos1, __n1)
321                    .compare(basic_string_view(__str, __n2));
322       }
324       size_type
325       find(basic_string_view __str, size_type __pos = 0) const noexcept
326       { return this->find(__str._M_str, __pos, __str._M_len); }
328       size_type
329       find(_CharT __c, size_type __pos=0) const noexcept;
331       size_type
332       find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
334       size_type
335       find(const _CharT* __str, size_type __pos=0) const noexcept
336       { return this->find(__str, __pos, traits_type::length(__str)); }
338       size_type
339       rfind(basic_string_view __str, size_type __pos = npos) const noexcept
340       { return this->rfind(__str._M_str, __pos, __str._M_len); }
342       size_type
343       rfind(_CharT __c, size_type __pos = npos) const noexcept;
345       size_type
346       rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
348       size_type
349       rfind(const _CharT* __str, size_type __pos = npos) const noexcept
350       { return this->rfind(__str, __pos, traits_type::length(__str)); }
352       size_type
353       find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
354       { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
356       size_type
357       find_first_of(_CharT __c, size_type __pos = 0) const noexcept
358       { return this->find(__c, __pos); }
360       size_type
361       find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
363       size_type
364       find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
365       { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
367       size_type
368       find_last_of(basic_string_view __str,
369                    size_type __pos = npos) const noexcept
370       { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
372       size_type
373       find_last_of(_CharT __c, size_type __pos=npos) const noexcept
374       { return this->rfind(__c, __pos); }
376       size_type
377       find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
379       size_type
380       find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
381       { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
383       size_type
384       find_first_not_of(basic_string_view __str,
385                         size_type __pos = 0) const noexcept
386       { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
388       size_type
389       find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
391       size_type
392       find_first_not_of(const _CharT* __str,
393                         size_type __pos, size_type __n) const;
395       size_type
396       find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
397       {
398         return this->find_first_not_of(__str, __pos,
399                                        traits_type::length(__str));
400       }
402       size_type
403       find_last_not_of(basic_string_view __str,
404                        size_type __pos = npos) const noexcept
405       { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
407       size_type
408       find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
410       size_type
411       find_last_not_of(const _CharT* __str,
412                        size_type __pos, size_type __n) const;
414       size_type
415       find_last_not_of(const _CharT* __str,
416                        size_type __pos = npos) const noexcept
417       {
418         return this->find_last_not_of(__str, __pos,
419                                       traits_type::length(__str));
420       }
422     private:
424       static constexpr const int
425       _S_compare(size_type __n1, size_type __n2) noexcept
426       {
427         return difference_type{__n1 - __n2} > std::numeric_limits<int>::max()
428              ? std::numeric_limits<int>::max()
429              : difference_type{__n1 - __n2} < std::numeric_limits<int>::min()
430              ? std::numeric_limits<int>::min()
431              : static_cast<int>(difference_type{__n1 - __n2});
432       }
434       static constexpr value_type _S_empty_str[1]{};
436       size_t        _M_len;
437       const _CharT* _M_str;
438     };
441   // [string.view.comparison], non-member basic_string_view comparison functions
443   namespace __detail
444   {
445     //  Identity transform to make ADL work with just one argument.
446     //  See n3766.html.
447     template<typename _Tp = void>
448       struct __identity
449       { typedef _Tp type; };
451     template<>
452       struct __identity<void>;
454     template<typename _Tp>
455       using __idt = typename __identity<_Tp>::type;
456   }
458   template<typename _CharT, typename _Traits>
459     bool
460     operator==(basic_string_view<_CharT, _Traits> __x,
461                basic_string_view<_CharT, _Traits> __y) noexcept
462     { return __x.compare(__y) == 0; }
464   template<typename _CharT, typename _Traits>
465     bool
466     operator==(basic_string_view<_CharT, _Traits> __x,
467                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
468     { return __x.compare(__y) == 0; }
470   template<typename _CharT, typename _Traits>
471     bool
472     operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
473                basic_string_view<_CharT, _Traits> __y) noexcept
474     { return __x.compare(__y) == 0; }
476   template<typename _CharT, typename _Traits>
477     bool
478     operator!=(basic_string_view<_CharT, _Traits> __x,
479                basic_string_view<_CharT, _Traits> __y) noexcept
480     { return !(__x == __y); }
482   template<typename _CharT, typename _Traits>
483     bool
484     operator!=(basic_string_view<_CharT, _Traits> __x,
485                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
486     { return !(__x == __y); }
488   template<typename _CharT, typename _Traits>
489     bool
490     operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
491                basic_string_view<_CharT, _Traits> __y) noexcept
492     { return !(__x == __y); }
494   template<typename _CharT, typename _Traits>
495     bool
496     operator< (basic_string_view<_CharT, _Traits> __x,
497                basic_string_view<_CharT, _Traits> __y) noexcept
498     { return __x.compare(__y) < 0; }
500   template<typename _CharT, typename _Traits>
501     bool
502     operator< (basic_string_view<_CharT, _Traits> __x,
503                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
504     { return __x.compare(__y) < 0; }
506   template<typename _CharT, typename _Traits>
507     bool
508     operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
509                basic_string_view<_CharT, _Traits> __y) noexcept
510     { return __x.compare(__y) < 0; }
512   template<typename _CharT, typename _Traits>
513     bool
514     operator> (basic_string_view<_CharT, _Traits> __x,
515                basic_string_view<_CharT, _Traits> __y) noexcept
516     { return __x.compare(__y) > 0; }
518   template<typename _CharT, typename _Traits>
519     bool
520     operator> (basic_string_view<_CharT, _Traits> __x,
521                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
522     { return __x.compare(__y) > 0; }
524   template<typename _CharT, typename _Traits>
525     bool
526     operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
527                basic_string_view<_CharT, _Traits> __y) noexcept
528     { return __x.compare(__y) > 0; }
530   template<typename _CharT, typename _Traits>
531     bool
532     operator<=(basic_string_view<_CharT, _Traits> __x,
533                basic_string_view<_CharT, _Traits> __y) noexcept
534     { return __x.compare(__y) <= 0; }
536   template<typename _CharT, typename _Traits>
537     bool
538     operator<=(basic_string_view<_CharT, _Traits> __x,
539                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
540     { return __x.compare(__y) <= 0; }
542   template<typename _CharT, typename _Traits>
543     bool
544     operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
545                basic_string_view<_CharT, _Traits> __y) noexcept
546     { return __x.compare(__y) <= 0; }
548   template<typename _CharT, typename _Traits>
549     bool
550     operator>=(basic_string_view<_CharT, _Traits> __x,
551                basic_string_view<_CharT, _Traits> __y) noexcept
552     { return __x.compare(__y) >= 0; }
554   template<typename _CharT, typename _Traits>
555     bool
556     operator>=(basic_string_view<_CharT, _Traits> __x,
557                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
558     { return __x.compare(__y) >= 0; }
560   template<typename _CharT, typename _Traits>
561     bool
562     operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
563                basic_string_view<_CharT, _Traits> __y) noexcept
564     { return __x.compare(__y) >= 0; }
566   // [string.view.comparison], sufficient additional overloads of comparison functions
568   // [string.view.nonmem], other non-member basic_string_view functions
569   template<typename _CharT, typename _Traits = char_traits<_CharT>,
570            typename _Allocator = allocator<_CharT>>
571     basic_string<_CharT, _Traits, _Allocator>
572     to_string(basic_string_view<_CharT, _Traits> __str,
573               const _Allocator& __alloc = _Allocator())
574     {
575       return basic_string<_CharT, _Traits, _Allocator>
576                         (__str.begin(), __str.end(), __alloc);
577     }
579   template<typename _CharT, typename _Traits>
580     basic_ostream<_CharT, _Traits>&
581       operator<<(basic_ostream<_CharT, _Traits>& __os,
582                  basic_string_view<_CharT,_Traits> __str)
583       { return __ostream_insert(__os, __str.data(), __str.size()); }
586   // basic_string_view typedef names
588   using string_view = basic_string_view<char>;
589 #ifdef _GLIBCXX_USE_WCHAR_T
590   using wstring_view = basic_string_view<wchar_t>;
591 #endif
592 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
593   using u16string_view = basic_string_view<char16_t>;
594   using u32string_view = basic_string_view<char32_t>;
595 #endif
597 _GLIBCXX_END_NAMESPACE_VERSION
598 } // namespace experimental
601   // [string.view.hash], hash support:
603 _GLIBCXX_BEGIN_NAMESPACE_VERSION
604   template<typename _Tp>
605     struct hash;
607   template<>
608     struct hash<experimental::string_view>
609     : public __hash_base<size_t, experimental::string_view>
610     {
611       size_t
612       operator()(const experimental::string_view& __str) const noexcept
613       { return std::_Hash_impl::hash(__str.data(), __str.length()); }
614     };
616   template<>
617     struct __is_fast_hash<hash<experimental::string_view>> : std::false_type
618     { };
620 #ifdef _GLIBCXX_USE_WCHAR_T
621   template<>
622     struct hash<experimental::wstring_view>
623     : public __hash_base<size_t, wstring>
624     {
625       size_t
626       operator()(const experimental::wstring_view& __s) const noexcept
627       { return std::_Hash_impl::hash(__s.data(),
628                                      __s.length() * sizeof(wchar_t)); }
629     };
631   template<>
632     struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type
633     { };
634 #endif
636 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
637   template<>
638     struct hash<experimental::u16string_view>
639     : public __hash_base<size_t, experimental::u16string_view>
640     {
641       size_t
642       operator()(const experimental::u16string_view& __s) const noexcept
643       { return std::_Hash_impl::hash(__s.data(),
644                                      __s.length() * sizeof(char16_t)); }
645     };
647   template<>
648     struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type
649     { };
651   template<>
652     struct hash<experimental::u32string_view>
653     : public __hash_base<size_t, experimental::u32string_view>
654     {
655       size_t
656       operator()(const experimental::u32string_view& __s) const noexcept
657       { return std::_Hash_impl::hash(__s.data(),
658                                      __s.length() * sizeof(char32_t)); }
659     };
661   template<>
662     struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type
663     { };
664 #endif
665 _GLIBCXX_END_NAMESPACE_VERSION
667 namespace experimental
669 _GLIBCXX_BEGIN_NAMESPACE_VERSION
671   // I added these EMSR.
672   inline namespace literals
673   {
674   inline namespace string_view_literals
675   {
677     inline basic_string_view<char>
678     operator""sv(const char* __str, size_t __len)
679     { return basic_string_view<char>{__str, __len}; }
681 #ifdef _GLIBCXX_USE_WCHAR_T
682     inline basic_string_view<wchar_t>
683     operator""sv(const wchar_t* __str, size_t __len)
684     { return basic_string_view<wchar_t>{__str, __len}; }
685 #endif
687 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
688     inline basic_string_view<char16_t>
689     operator""sv(const char16_t* __str, size_t __len)
690     { return basic_string_view<char16_t>{__str, __len}; }
692     inline basic_string_view<char32_t>
693     operator""sv(const char32_t* __str, size_t __len)
694     { return basic_string_view<char32_t>{__str, __len}; }
695 #endif
697   }
698   }
700 _GLIBCXX_END_NAMESPACE_VERSION
701 } // namespace experimental
702 } // namespace std
704 #include <experimental/string_view.tcc>
706 #endif // __cplusplus <= 201103L
708 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW