C++: fix-it hint for missing "typename" (PR c++/63392)
[official-gcc.git] / libstdc++-v3 / include / std / string_view
blob9e0f6a723e4e88de349d060a83e3d23bc9c031f1
1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013-2018 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 string_view
26  *  This is a Standard C++ Library header.
27  */
30 // N3762 basic_string_view library
33 #ifndef _GLIBCXX_STRING_VIEW
34 #define _GLIBCXX_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201703L
40 #include <limits>
41 #include <iosfwd>
42 #include <bits/char_traits.h>
43 #include <bits/functional_hash.h>
44 #include <bits/range_access.h>
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 #define __cpp_lib_string_view 201603
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   template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
71     class basic_string_view
72     {
73     public:
75       // types
76       using traits_type = _Traits;
77       using value_type = _CharT;
78       using pointer = const _CharT*;
79       using const_pointer = const _CharT*;
80       using reference = const _CharT&;
81       using const_reference = const _CharT&;
82       using const_iterator = const _CharT*;
83       using iterator = const_iterator;
84       using const_reverse_iterator = std::reverse_iterator<const_iterator>;
85       using reverse_iterator = const_reverse_iterator;
86       using size_type = size_t;
87       using difference_type = ptrdiff_t;
88       static constexpr size_type npos = size_type(-1);
90       // [string.view.cons], construct/copy
92       constexpr
93       basic_string_view() noexcept
94       : _M_len{0}, _M_str{nullptr}
95       { }
97       constexpr basic_string_view(const basic_string_view&) noexcept = default;
99       __attribute__((__nonnull__)) constexpr
100       basic_string_view(const _CharT* __str) noexcept
101       : _M_len{traits_type::length(__str)},
102         _M_str{__str}
103       { }
105       constexpr
106       basic_string_view(const _CharT* __str, size_type __len) noexcept
107       : _M_len{__len}, _M_str{__str}
108       { }
110       constexpr basic_string_view&
111       operator=(const basic_string_view&) noexcept = default;
113       // [string.view.iterators], iterators
115       constexpr const_iterator
116       begin() const noexcept
117       { return this->_M_str; }
119       constexpr const_iterator
120       end() const noexcept
121       { return this->_M_str + this->_M_len; }
123       constexpr const_iterator
124       cbegin() const noexcept
125       { return this->_M_str; }
127       constexpr const_iterator
128       cend() const noexcept
129       { return this->_M_str + this->_M_len; }
131       constexpr const_reverse_iterator
132       rbegin() const noexcept
133       { return const_reverse_iterator(this->end()); }
135       constexpr const_reverse_iterator
136       rend() const noexcept
137       { return const_reverse_iterator(this->begin()); }
139       constexpr const_reverse_iterator
140       crbegin() const noexcept
141       { return const_reverse_iterator(this->end()); }
143       constexpr const_reverse_iterator
144       crend() const noexcept
145       { return const_reverse_iterator(this->begin()); }
147       // [string.view.capacity], capacity
149       constexpr size_type
150       size() const noexcept
151       { return this->_M_len; }
153       constexpr size_type
154       length() const noexcept
155       { return _M_len; }
157       constexpr size_type
158       max_size() const noexcept
159       {
160         return (npos - sizeof(size_type) - sizeof(void*))
161                 / sizeof(value_type) / 4;
162       }
164       [[nodiscard]] constexpr bool
165       empty() const noexcept
166       { return this->_M_len == 0; }
168       // [string.view.access], element access
170       constexpr const _CharT&
171       operator[](size_type __pos) const noexcept
172       {
173         // TODO: Assert to restore in a way compatible with the constexpr.
174         // __glibcxx_assert(__pos < this->_M_len);
175         return *(this->_M_str + __pos);
176       }
178       constexpr const _CharT&
179       at(size_type __pos) const
180       {
181         if (__pos >= _M_len)
182           __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
183                                        "(which is %zu) >= this->size() "
184                                        "(which is %zu)"), __pos, this->size());
185         return *(this->_M_str + __pos);
186       }
188       constexpr const _CharT&
189       front() const noexcept
190       {
191         // TODO: Assert to restore in a way compatible with the constexpr.
192         // __glibcxx_assert(this->_M_len > 0);
193         return *this->_M_str;
194       }
196       constexpr const _CharT&
197       back() const noexcept
198       {
199         // TODO: Assert to restore in a way compatible with the constexpr.
200         // __glibcxx_assert(this->_M_len > 0);
201         return *(this->_M_str + this->_M_len - 1);
202       }
204       constexpr const _CharT*
205       data() const noexcept
206       { return this->_M_str; }
208       // [string.view.modifiers], modifiers:
210       constexpr void
211       remove_prefix(size_type __n) noexcept
212       {
213         __glibcxx_assert(this->_M_len >= __n);
214         this->_M_str += __n;
215         this->_M_len -= __n;
216       }
218       constexpr void
219       remove_suffix(size_type __n) noexcept
220       { this->_M_len -= __n; }
222       constexpr void
223       swap(basic_string_view& __sv) noexcept
224       {
225         auto __tmp = *this;
226         *this = __sv;
227         __sv = __tmp;
228       }
231       // [string.view.ops], string operations:
233       size_type
234       copy(_CharT* __str, size_type __n, size_type __pos = 0) const
235       {
236         __glibcxx_requires_string_len(__str, __n);
237         __pos = _M_check(__pos, "basic_string_view::copy");
238         const size_type __rlen = std::min(__n, _M_len - __pos);
239         for (auto __begin = this->_M_str + __pos,
240              __end = __begin + __rlen; __begin != __end;)
241           *__str++ = *__begin++;
242         return __rlen;
243       }
245       constexpr basic_string_view
246       substr(size_type __pos, size_type __n = npos) const noexcept(false)
247       {
248         __pos = _M_check(__pos, "basic_string_view::substr");
249         const size_type __rlen = std::min(__n, _M_len - __pos);
250         return basic_string_view{_M_str + __pos, __rlen};
251       }
253       constexpr int
254       compare(basic_string_view __str) const noexcept
255       {
256         const size_type __rlen = std::min(this->_M_len, __str._M_len);
257         int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
258         if (__ret == 0)
259           __ret = _S_compare(this->_M_len, __str._M_len);
260         return __ret;
261       }
263       constexpr int
264       compare(size_type __pos1, size_type __n1, basic_string_view __str) const
265       { return this->substr(__pos1, __n1).compare(__str); }
267       constexpr int
268       compare(size_type __pos1, size_type __n1,
269               basic_string_view __str, size_type __pos2, size_type __n2) const
270       {
271         return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
272       }
274       __attribute__((__nonnull__)) constexpr int
275       compare(const _CharT* __str) const noexcept
276       { return this->compare(basic_string_view{__str}); }
278       __attribute__((__nonnull__)) constexpr int
279       compare(size_type __pos1, size_type __n1, const _CharT* __str) const
280       { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
282       constexpr int
283       compare(size_type __pos1, size_type __n1,
284               const _CharT* __str, size_type __n2) const noexcept(false)
285       {
286         return this->substr(__pos1, __n1)
287                    .compare(basic_string_view(__str, __n2));
288       }
290       constexpr size_type
291       find(basic_string_view __str, size_type __pos = 0) const noexcept
292       { return this->find(__str._M_str, __pos, __str._M_len); }
294       constexpr size_type
295       find(_CharT __c, size_type __pos = 0) const noexcept;
297       constexpr size_type
298       find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
300       __attribute__((__nonnull__)) constexpr size_type
301       find(const _CharT* __str, size_type __pos = 0) const noexcept
302       { return this->find(__str, __pos, traits_type::length(__str)); }
304       constexpr size_type
305       rfind(basic_string_view __str, size_type __pos = npos) const noexcept
306       { return this->rfind(__str._M_str, __pos, __str._M_len); }
308       constexpr size_type
309       rfind(_CharT __c, size_type __pos = npos) const noexcept;
311       constexpr size_type
312       rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
314       __attribute__((__nonnull__)) constexpr size_type
315       rfind(const _CharT* __str, size_type __pos = npos) const noexcept
316       { return this->rfind(__str, __pos, traits_type::length(__str)); }
318       constexpr size_type
319       find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
320       { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
322       constexpr size_type
323       find_first_of(_CharT __c, size_type __pos = 0) const noexcept
324       { return this->find(__c, __pos); }
326       constexpr size_type
327       find_first_of(const _CharT* __str, size_type __pos,
328                     size_type __n) const noexcept;
330       __attribute__((__nonnull__)) constexpr size_type
331       find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
332       { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
334       constexpr size_type
335       find_last_of(basic_string_view __str,
336                    size_type __pos = npos) const noexcept
337       { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
339       constexpr size_type
340       find_last_of(_CharT __c, size_type __pos=npos) const noexcept
341       { return this->rfind(__c, __pos); }
343       constexpr size_type
344       find_last_of(const _CharT* __str, size_type __pos,
345                    size_type __n) const noexcept;
347       __attribute__((__nonnull__)) constexpr size_type
348       find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
349       { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
351       constexpr size_type
352       find_first_not_of(basic_string_view __str,
353                         size_type __pos = 0) const noexcept
354       { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
356       constexpr size_type
357       find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
359       constexpr size_type
360       find_first_not_of(const _CharT* __str,
361                         size_type __pos, size_type __n) const noexcept;
363       __attribute__((__nonnull__)) constexpr size_type
364       find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
365       {
366         return this->find_first_not_of(__str, __pos,
367                                        traits_type::length(__str));
368       }
370       constexpr size_type
371       find_last_not_of(basic_string_view __str,
372                        size_type __pos = npos) const noexcept
373       { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
375       constexpr size_type
376       find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
378       constexpr size_type
379       find_last_not_of(const _CharT* __str,
380                        size_type __pos, size_type __n) const noexcept;
382       __attribute__((__nonnull__)) constexpr size_type
383       find_last_not_of(const _CharT* __str,
384                        size_type __pos = npos) const noexcept
385       {
386         return this->find_last_not_of(__str, __pos,
387                                       traits_type::length(__str));
388       }
390       constexpr size_type
391       _M_check(size_type __pos, const char* __s) const noexcept(false)
392       {
393         if (__pos > this->size())
394           __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
395                                        "this->size() (which is %zu)"),
396                                    __s, __pos, this->size());
397         return __pos;
398       }
400       // NB: _M_limit doesn't check for a bad __pos value.
401       constexpr size_type
402       _M_limit(size_type __pos, size_type __off) const noexcept
403       {
404         const bool __testoff =  __off < this->size() - __pos;
405         return __testoff ? __off : this->size() - __pos;
406       }
407       
408     private:
410       static constexpr int
411       _S_compare(size_type __n1, size_type __n2) noexcept
412       {
413         const difference_type __diff = __n1 - __n2;
414         if (__diff > std::numeric_limits<int>::max())
415           return std::numeric_limits<int>::max();
416         if (__diff < std::numeric_limits<int>::min())
417           return std::numeric_limits<int>::min();
418         return static_cast<int>(__diff);
419       }
421       size_t        _M_len;
422       const _CharT* _M_str;
423     };
425   // [string.view.comparison], non-member basic_string_view comparison function
427   namespace __detail
428   {
429     // Identity transform to create a non-deduced context, so that only one
430     // argument participates in template argument deduction and the other
431     // argument gets implicitly converted to the deduced type. See n3766.html.
432     template<typename _Tp>
433       using __idt = common_type_t<_Tp>;
434   }
436   template<typename _CharT, typename _Traits>
437     constexpr bool
438     operator==(basic_string_view<_CharT, _Traits> __x,
439                basic_string_view<_CharT, _Traits> __y) noexcept
440     { return __x.size() == __y.size() && __x.compare(__y) == 0; }
442   template<typename _CharT, typename _Traits>
443     constexpr bool
444     operator==(basic_string_view<_CharT, _Traits> __x,
445                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
446     { return __x.size() == __y.size() && __x.compare(__y) == 0; }
448   template<typename _CharT, typename _Traits>
449     constexpr bool
450     operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
451                basic_string_view<_CharT, _Traits> __y) noexcept
452     { return __x.size() == __y.size() && __x.compare(__y) == 0; }
454   template<typename _CharT, typename _Traits>
455     constexpr bool
456     operator!=(basic_string_view<_CharT, _Traits> __x,
457                basic_string_view<_CharT, _Traits> __y) noexcept
458     { return !(__x == __y); }
460   template<typename _CharT, typename _Traits>
461     constexpr bool
462     operator!=(basic_string_view<_CharT, _Traits> __x,
463                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
464     { return !(__x == __y); }
466   template<typename _CharT, typename _Traits>
467     constexpr bool
468     operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
469                basic_string_view<_CharT, _Traits> __y) noexcept
470     { return !(__x == __y); }
472   template<typename _CharT, typename _Traits>
473     constexpr bool
474     operator< (basic_string_view<_CharT, _Traits> __x,
475                basic_string_view<_CharT, _Traits> __y) noexcept
476     { return __x.compare(__y) < 0; }
478   template<typename _CharT, typename _Traits>
479     constexpr bool
480     operator< (basic_string_view<_CharT, _Traits> __x,
481                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
482     { return __x.compare(__y) < 0; }
484   template<typename _CharT, typename _Traits>
485     constexpr bool
486     operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
487                basic_string_view<_CharT, _Traits> __y) noexcept
488     { return __x.compare(__y) < 0; }
490   template<typename _CharT, typename _Traits>
491     constexpr bool
492     operator> (basic_string_view<_CharT, _Traits> __x,
493                basic_string_view<_CharT, _Traits> __y) noexcept
494     { return __x.compare(__y) > 0; }
496   template<typename _CharT, typename _Traits>
497     constexpr bool
498     operator> (basic_string_view<_CharT, _Traits> __x,
499                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
500     { return __x.compare(__y) > 0; }
502   template<typename _CharT, typename _Traits>
503     constexpr bool
504     operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
505                basic_string_view<_CharT, _Traits> __y) noexcept
506     { return __x.compare(__y) > 0; }
508   template<typename _CharT, typename _Traits>
509     constexpr bool
510     operator<=(basic_string_view<_CharT, _Traits> __x,
511                basic_string_view<_CharT, _Traits> __y) noexcept
512     { return __x.compare(__y) <= 0; }
514   template<typename _CharT, typename _Traits>
515     constexpr bool
516     operator<=(basic_string_view<_CharT, _Traits> __x,
517                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
518     { return __x.compare(__y) <= 0; }
520   template<typename _CharT, typename _Traits>
521     constexpr bool
522     operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
523                basic_string_view<_CharT, _Traits> __y) noexcept
524     { return __x.compare(__y) <= 0; }
526   template<typename _CharT, typename _Traits>
527     constexpr bool
528     operator>=(basic_string_view<_CharT, _Traits> __x,
529                basic_string_view<_CharT, _Traits> __y) noexcept
530     { return __x.compare(__y) >= 0; }
532   template<typename _CharT, typename _Traits>
533     constexpr bool
534     operator>=(basic_string_view<_CharT, _Traits> __x,
535                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
536     { return __x.compare(__y) >= 0; }
538   template<typename _CharT, typename _Traits>
539     constexpr bool
540     operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
541                basic_string_view<_CharT, _Traits> __y) noexcept
542     { return __x.compare(__y) >= 0; }
544   // [string.view.io], Inserters and extractors
545   template<typename _CharT, typename _Traits>
546     inline basic_ostream<_CharT, _Traits>&
547     operator<<(basic_ostream<_CharT, _Traits>& __os,
548                basic_string_view<_CharT,_Traits> __str)
549     { return __ostream_insert(__os, __str.data(), __str.size()); }
552   // basic_string_view typedef names
554   using string_view = basic_string_view<char>;
555 #ifdef _GLIBCXX_USE_WCHAR_T
556   using wstring_view = basic_string_view<wchar_t>;
557 #endif
559   using u16string_view = basic_string_view<char16_t>;
560   using u32string_view = basic_string_view<char32_t>;
562   // [string.view.hash], hash support:
564   template<typename _Tp>
565     struct hash;
567   template<>
568     struct hash<string_view>
569     : public __hash_base<size_t, string_view>
570     {
571       size_t
572       operator()(const string_view& __str) const noexcept
573       { return std::_Hash_impl::hash(__str.data(), __str.length()); }
574     };
576   template<>
577     struct __is_fast_hash<hash<string_view>> : std::false_type
578     { };
580 #ifdef _GLIBCXX_USE_WCHAR_T
581   template<>
582     struct hash<wstring_view>
583     : public __hash_base<size_t, wstring>
584     {
585       size_t
586       operator()(const wstring_view& __s) const noexcept
587       { return std::_Hash_impl::hash(__s.data(),
588                                      __s.length() * sizeof(wchar_t)); }
589     };
591   template<>
592     struct __is_fast_hash<hash<wstring_view>> : std::false_type
593     { };
594 #endif
596   template<>
597     struct hash<u16string_view>
598     : public __hash_base<size_t, u16string_view>
599     {
600       size_t
601       operator()(const u16string_view& __s) const noexcept
602       { return std::_Hash_impl::hash(__s.data(),
603                                      __s.length() * sizeof(char16_t)); }
604     };
606   template<>
607     struct __is_fast_hash<hash<u16string_view>> : std::false_type
608     { };
610   template<>
611     struct hash<u32string_view>
612     : public __hash_base<size_t, u32string_view>
613     {
614       size_t
615       operator()(const u32string_view& __s) const noexcept
616       { return std::_Hash_impl::hash(__s.data(),
617                                      __s.length() * sizeof(char32_t)); }
618     };
620   template<>
621     struct __is_fast_hash<hash<u32string_view>> : std::false_type
622     { };
624   inline namespace literals
625   {
626   inline namespace string_view_literals
627   {
628 #pragma GCC diagnostic push
629 #pragma GCC diagnostic ignored "-Wliteral-suffix"
630     inline constexpr basic_string_view<char>
631     operator""sv(const char* __str, size_t __len) noexcept
632     { return basic_string_view<char>{__str, __len}; }
634 #ifdef _GLIBCXX_USE_WCHAR_T
635     inline constexpr basic_string_view<wchar_t>
636     operator""sv(const wchar_t* __str, size_t __len) noexcept
637     { return basic_string_view<wchar_t>{__str, __len}; }
638 #endif
640     inline constexpr basic_string_view<char16_t>
641     operator""sv(const char16_t* __str, size_t __len) noexcept
642     { return basic_string_view<char16_t>{__str, __len}; }
644     inline constexpr basic_string_view<char32_t>
645     operator""sv(const char32_t* __str, size_t __len) noexcept
646     { return basic_string_view<char32_t>{__str, __len}; }
648 #pragma GCC diagnostic pop
649   } // namespace string_literals
650   } // namespace literals
652 _GLIBCXX_END_NAMESPACE_VERSION
653 } // namespace std
655 #include <bits/string_view.tcc>
657 #endif // __cplusplus <= 201402L
659 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW