2014-08-04 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / libstdc++-v3 / include / experimental / string_view
blob041f7489deb09e51c8c04476242a5c4908488625
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 <string>
43 #include <limits>
45 namespace std _GLIBCXX_VISIBILITY(default)
47 namespace experimental
49 inline namespace fundamentals_v1
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53   /**
54    *  @class basic_string_view <experimental/string_view>
55    *  @brief  A non-owning reference to a string.
56    *
57    *  @ingroup strings
58    *  @ingroup sequences
59    *  @ingroup experimental
60    *
61    *  @tparam _CharT  Type of character
62    *  @tparam _Traits  Traits for character type, defaults to
63    *                   char_traits<_CharT>.
64    *
65    *  A basic_string_view looks like this:
66    *
67    *  @code
68    *    _CharT*    _M_str
69    *    size_t     _M_len
70    *  @endcode
71    */
72   template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
73     class basic_string_view
74     {
75     public:
77       // types
78       using traits_type = _Traits;
79       using value_type = _CharT;
80       using pointer = const _CharT*;
81       using const_pointer = const _CharT*;
82       using reference = const _CharT&;
83       using const_reference = const _CharT&;
84       using const_iterator = const _CharT*;
85       using iterator = const_iterator;
86       using const_reverse_iterator = std::reverse_iterator<const_iterator>;
87       using reverse_iterator = const_reverse_iterator;
88       using size_type = size_t;
89       using difference_type = ptrdiff_t;
90       static constexpr size_type npos = size_type(-1);
92       // [string.view.cons], construct/copy
94       constexpr
95       basic_string_view() noexcept
96       : _M_len{0}, _M_str{nullptr}
97       { }
99       constexpr basic_string_view(const basic_string_view&) noexcept = default;
101       template<typename _Allocator>
102         basic_string_view(const basic_string<_CharT, _Traits,
103                           _Allocator>& __str) noexcept
104         : _M_len{__str.length()}, _M_str{__str.data()}
105         { }
107       constexpr basic_string_view(const _CharT* __str)
108       : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
109         _M_str{__str}
110       { }
112       constexpr basic_string_view(const _CharT* __str, size_type __len)
113       : _M_len{__len},
114         _M_str{__str}
115       { }
117       basic_string_view&
118       operator=(const basic_string_view&) noexcept = default;
120       // [string.view.iterators], iterators
122       constexpr const_iterator
123       begin() const noexcept
124       { return this->_M_str; }
126       constexpr const_iterator
127       end() const noexcept
128       { return this->_M_str + this->_M_len; }
130       constexpr const_iterator
131       cbegin() const noexcept
132       { return this->_M_str; }
134       constexpr const_iterator
135       cend() const noexcept
136       { return this->_M_str + this->_M_len; }
138       const_reverse_iterator
139       rbegin() const noexcept
140       { return const_reverse_iterator(this->end()); }
142       const_reverse_iterator
143       rend() const noexcept
144       { return const_reverse_iterator(this->begin()); }
146       const_reverse_iterator
147       crbegin() const noexcept
148       { return const_reverse_iterator(this->end()); }
150       const_reverse_iterator
151       crend() const noexcept
152       { return const_reverse_iterator(this->begin()); }
154       // [string.view.capacity], capacity
156       constexpr size_type
157       size() const noexcept
158       { return this->_M_len; }
160       constexpr size_type
161       length() const noexcept
162       { return _M_len; }
164       constexpr size_type
165       max_size() const noexcept
166       {
167         return (npos - sizeof(size_type) - sizeof(void*))
168                 / sizeof(value_type) / 4;
169       }
171       constexpr bool
172       empty() const noexcept
173       { return this->_M_len == 0; }
175       // [string.view.access], element access
177       constexpr const _CharT&
178       operator[](size_type __pos) const
179       {
180         // TODO: Assert to restore in a way compatible with the constexpr.
181         // _GLIBCXX_DEBUG_ASSERT(__pos < this->_M_len);
182         return *(this->_M_str + __pos);
183       }
185       constexpr const _CharT&
186       at(size_type __pos) const
187       {
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() "
192                                              "(which is %zu)"),
193                                          __pos, this->size()),
194                 *this->_M_str);
195       }
197       constexpr const _CharT&
198       front() const
199       {
200         // TODO: Assert to restore in a way compatible with the constexpr.
201         // _GLIBCXX_DEBUG_ASSERT(this->_M_len > 0);
202         return *this->_M_str;
203       }
205       constexpr const _CharT&
206       back() const
207       {
208         // TODO: Assert to restore in a way compatible with the constexpr.
209         // _GLIBCXX_DEBUG_ASSERT(this->_M_len > 0);
210         return *(this->_M_str + this->_M_len - 1);
211       }
213       constexpr const _CharT*
214       data() const noexcept
215       { return this->_M_str; }
217       // [string.view.modifiers], modifiers:
219       void
220       clear() noexcept
221       {
222         this->_M_len = 0;
223         this->_M_str = nullptr;
224       }
226       void
227       remove_prefix(size_type __n)
228       {
229         _GLIBCXX_DEBUG_ASSERT(this->_M_len >= __n);
230         this->_M_str += __n;
231         this->_M_len -= __n;
232       }
234       void
235       remove_suffix(size_type __n)
236       { this->_M_len -= __n; }
238       void
239       swap(basic_string_view& __sv) noexcept
240       {
241         std::swap(this->_M_len, __sv._M_len);
242         std::swap(this->_M_str, __sv._M_str);
243       }
246       // [string.view.ops], string operations:
248       template<typename _Allocator>
249         explicit operator basic_string<_CharT, _Traits, _Allocator>() const
250         {
251           return { this->_M_str, this->_M_len };
252         }
254       template<typename _Allocator = std::allocator<_CharT>>
255         basic_string<_CharT, _Traits, _Allocator>
256         to_string(const _Allocator& __alloc = _Allocator()) const
257         {
258           return { this->_M_str, this->_M_len, __alloc };
259         }
261       size_type
262       copy(_CharT* __str, size_type __n, size_type __pos = 0) const
263       {
264         __glibcxx_requires_string_len(__str, __n);
265         if (__pos > this->_M_len)
266           __throw_out_of_range_fmt(__N("basic_string_view::copy: __pos "
267                                        "(which is %zu) > this->size() "
268                                        "(which is %zu)"),
269                                    __pos, this->size());
270         size_type __rlen{std::min(__n, size_type{this->_M_len  - __pos})};
271         for (auto __begin = this->_M_str + __pos,
272              __end = __begin + __rlen; __begin != __end;)
273           *__str++ = *__begin++;
274         return __rlen;
275       }
278       // [string.view.ops], string operations:
280       constexpr basic_string_view
281       substr(size_type __pos, size_type __n=npos) const
282       {
283         return __pos <= this->_M_len
284              ? basic_string_view{this->_M_str + __pos,
285                                 std::min(__n, size_type{this->_M_len  - __pos})}
286              : (__throw_out_of_range_fmt(__N("basic_string_view::substr: __pos "
287                                              "(which is %zu) > this->size() "
288                                              "(which is %zu)"),
289                                      __pos, this->size()), basic_string_view{});
290       }
292       int
293       compare(basic_string_view __str) const noexcept
294       {
295         int __ret = traits_type::compare(this->_M_str, __str._M_str,
296                                          std::min(this->_M_len, __str._M_len));
297         if (__ret == 0)
298           __ret = _S_compare(this->_M_len, __str._M_len);
299         return __ret;
300       }
302       int
303       compare(size_type __pos1, size_type __n1, basic_string_view __str) const
304       { return this->substr(__pos1, __n1).compare(__str); }
306       int
307       compare(size_type __pos1, size_type __n1,
308               basic_string_view __str, size_type __pos2, size_type __n2) const
309       { return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
311       int
312       compare(const _CharT* __str) const noexcept
313       { return this->compare(basic_string_view{__str}); }
315       int
316       compare(size_type __pos1, size_type __n1, const _CharT* __str) const
317       { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
319       int
320       compare(size_type __pos1, size_type __n1,
321               const _CharT* __str, size_type __n2) const
322       {
323         return this->substr(__pos1, __n1)
324                    .compare(basic_string_view(__str, __n2));
325       }
327       size_type
328       find(basic_string_view __str, size_type __pos = 0) const noexcept
329       { return this->find(__str._M_str, __pos, __str._M_len); }
331       size_type
332       find(_CharT __c, size_type __pos=0) const noexcept;
334       size_type
335       find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
337       size_type
338       find(const _CharT* __str, size_type __pos=0) const noexcept
339       { return this->find(__str, __pos, traits_type::length(__str)); }
341       size_type
342       rfind(basic_string_view __str, size_type __pos = npos) const noexcept
343       { return this->rfind(__str._M_str, __pos, __str._M_len); }
345       size_type
346       rfind(_CharT __c, size_type __pos = npos) const noexcept;
348       size_type
349       rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
351       size_type
352       rfind(const _CharT* __str, size_type __pos = npos) const noexcept
353       { return this->rfind(__str, __pos, traits_type::length(__str)); }
355       size_type
356       find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
357       { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
359       size_type
360       find_first_of(_CharT __c, size_type __pos = 0) const noexcept
361       { return this->find(__c, __pos); }
363       size_type
364       find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
366       size_type
367       find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
368       { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
370       size_type
371       find_last_of(basic_string_view __str,
372                    size_type __pos = npos) const noexcept
373       { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
375       size_type
376       find_last_of(_CharT __c, size_type __pos=npos) const noexcept
377       { return this->rfind(__c, __pos); }
379       size_type
380       find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
382       size_type
383       find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
384       { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
386       size_type
387       find_first_not_of(basic_string_view __str,
388                         size_type __pos = 0) const noexcept
389       { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
391       size_type
392       find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
394       size_type
395       find_first_not_of(const _CharT* __str,
396                         size_type __pos, size_type __n) const;
398       size_type
399       find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
400       {
401         return this->find_first_not_of(__str, __pos,
402                                        traits_type::length(__str));
403       }
405       size_type
406       find_last_not_of(basic_string_view __str,
407                        size_type __pos = npos) const noexcept
408       { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
410       size_type
411       find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
413       size_type
414       find_last_not_of(const _CharT* __str,
415                        size_type __pos, size_type __n) const;
417       size_type
418       find_last_not_of(const _CharT* __str,
419                        size_type __pos = npos) const noexcept
420       {
421         return this->find_last_not_of(__str, __pos,
422                                       traits_type::length(__str));
423       }
425     private:
427       static constexpr const int
428       _S_compare(size_type __n1, size_type __n2) noexcept
429       {
430         return difference_type{__n1 - __n2} > std::numeric_limits<int>::max()
431              ? std::numeric_limits<int>::max()
432              : difference_type{__n1 - __n2} < std::numeric_limits<int>::min()
433              ? std::numeric_limits<int>::min()
434              : static_cast<int>(difference_type{__n1 - __n2});
435       }
437       size_t        _M_len;
438       const _CharT* _M_str;
439     };
442   // [string.view.comparison], non-member basic_string_view comparison functions
444   namespace __detail
445   {
446     //  Identity transform to make ADL work with just one argument.
447     //  See n3766.html.
448     template<typename _Tp = void>
449       struct __identity
450       { typedef _Tp type; };
452     template<>
453       struct __identity<void>;
455     template<typename _Tp>
456       using __idt = typename __identity<_Tp>::type;
457   }
459   template<typename _CharT, typename _Traits>
460     inline bool
461     operator==(basic_string_view<_CharT, _Traits> __x,
462                basic_string_view<_CharT, _Traits> __y) noexcept
463     { return __x.compare(__y) == 0; }
465   template<typename _CharT, typename _Traits>
466     inline bool
467     operator==(basic_string_view<_CharT, _Traits> __x,
468                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
469     { return __x.compare(__y) == 0; }
471   template<typename _CharT, typename _Traits>
472     inline bool
473     operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
474                basic_string_view<_CharT, _Traits> __y) noexcept
475     { return __x.compare(__y) == 0; }
477   template<typename _CharT, typename _Traits>
478     inline bool
479     operator!=(basic_string_view<_CharT, _Traits> __x,
480                basic_string_view<_CharT, _Traits> __y) noexcept
481     { return !(__x == __y); }
483   template<typename _CharT, typename _Traits>
484     inline bool
485     operator!=(basic_string_view<_CharT, _Traits> __x,
486                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
487     { return !(__x == __y); }
489   template<typename _CharT, typename _Traits>
490     inline bool
491     operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
492                basic_string_view<_CharT, _Traits> __y) noexcept
493     { return !(__x == __y); }
495   template<typename _CharT, typename _Traits>
496     inline bool
497     operator< (basic_string_view<_CharT, _Traits> __x,
498                basic_string_view<_CharT, _Traits> __y) noexcept
499     { return __x.compare(__y) < 0; }
501   template<typename _CharT, typename _Traits>
502     inline bool
503     operator< (basic_string_view<_CharT, _Traits> __x,
504                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
505     { return __x.compare(__y) < 0; }
507   template<typename _CharT, typename _Traits>
508     inline bool
509     operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
510                basic_string_view<_CharT, _Traits> __y) noexcept
511     { return __x.compare(__y) < 0; }
513   template<typename _CharT, typename _Traits>
514     inline bool
515     operator> (basic_string_view<_CharT, _Traits> __x,
516                basic_string_view<_CharT, _Traits> __y) noexcept
517     { return __x.compare(__y) > 0; }
519   template<typename _CharT, typename _Traits>
520     inline bool
521     operator> (basic_string_view<_CharT, _Traits> __x,
522                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
523     { return __x.compare(__y) > 0; }
525   template<typename _CharT, typename _Traits>
526     inline bool
527     operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
528                basic_string_view<_CharT, _Traits> __y) noexcept
529     { return __x.compare(__y) > 0; }
531   template<typename _CharT, typename _Traits>
532     inline bool
533     operator<=(basic_string_view<_CharT, _Traits> __x,
534                basic_string_view<_CharT, _Traits> __y) noexcept
535     { return __x.compare(__y) <= 0; }
537   template<typename _CharT, typename _Traits>
538     inline bool
539     operator<=(basic_string_view<_CharT, _Traits> __x,
540                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
541     { return __x.compare(__y) <= 0; }
543   template<typename _CharT, typename _Traits>
544     inline bool
545     operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
546                basic_string_view<_CharT, _Traits> __y) noexcept
547     { return __x.compare(__y) <= 0; }
549   template<typename _CharT, typename _Traits>
550     inline bool
551     operator>=(basic_string_view<_CharT, _Traits> __x,
552                basic_string_view<_CharT, _Traits> __y) noexcept
553     { return __x.compare(__y) >= 0; }
555   template<typename _CharT, typename _Traits>
556     inline bool
557     operator>=(basic_string_view<_CharT, _Traits> __x,
558                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
559     { return __x.compare(__y) >= 0; }
561   template<typename _CharT, typename _Traits>
562     inline bool
563     operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
564                basic_string_view<_CharT, _Traits> __y) noexcept
565     { return __x.compare(__y) >= 0; }
567   // [string.view.io], Inserters and extractors
568   template<typename _CharT, typename _Traits>
569     inline basic_ostream<_CharT, _Traits>&
570     operator<<(basic_ostream<_CharT, _Traits>& __os,
571                basic_string_view<_CharT,_Traits> __str)
572     { return __ostream_insert(__os, __str.data(), __str.size()); }
575   // basic_string_view typedef names
577   using string_view = basic_string_view<char>;
578 #ifdef _GLIBCXX_USE_WCHAR_T
579   using wstring_view = basic_string_view<wchar_t>;
580 #endif
581 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
582   using u16string_view = basic_string_view<char16_t>;
583   using u32string_view = basic_string_view<char32_t>;
584 #endif
586 _GLIBCXX_END_NAMESPACE_VERSION
587 } // namespace fundamentals_v1
588 } // namespace experimental
591   // [string.view.hash], hash support:
593 _GLIBCXX_BEGIN_NAMESPACE_VERSION
594   template<typename _Tp>
595     struct hash;
597   template<>
598     struct hash<experimental::string_view>
599     : public __hash_base<size_t, experimental::string_view>
600     {
601       size_t
602       operator()(const experimental::string_view& __str) const noexcept
603       { return std::_Hash_impl::hash(__str.data(), __str.length()); }
604     };
606   template<>
607     struct __is_fast_hash<hash<experimental::string_view>> : std::false_type
608     { };
610 #ifdef _GLIBCXX_USE_WCHAR_T
611   template<>
612     struct hash<experimental::wstring_view>
613     : public __hash_base<size_t, wstring>
614     {
615       size_t
616       operator()(const experimental::wstring_view& __s) const noexcept
617       { return std::_Hash_impl::hash(__s.data(),
618                                      __s.length() * sizeof(wchar_t)); }
619     };
621   template<>
622     struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type
623     { };
624 #endif
626 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
627   template<>
628     struct hash<experimental::u16string_view>
629     : public __hash_base<size_t, experimental::u16string_view>
630     {
631       size_t
632       operator()(const experimental::u16string_view& __s) const noexcept
633       { return std::_Hash_impl::hash(__s.data(),
634                                      __s.length() * sizeof(char16_t)); }
635     };
637   template<>
638     struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type
639     { };
641   template<>
642     struct hash<experimental::u32string_view>
643     : public __hash_base<size_t, experimental::u32string_view>
644     {
645       size_t
646       operator()(const experimental::u32string_view& __s) const noexcept
647       { return std::_Hash_impl::hash(__s.data(),
648                                      __s.length() * sizeof(char32_t)); }
649     };
651   template<>
652     struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type
653     { };
654 #endif
655 _GLIBCXX_END_NAMESPACE_VERSION
657 namespace experimental
659 _GLIBCXX_BEGIN_NAMESPACE_VERSION
661   // I added these EMSR.
662   inline namespace literals
663   {
664   inline namespace string_view_literals
665   {
667     inline constexpr basic_string_view<char>
668     operator""sv(const char* __str, size_t __len)
669     { return basic_string_view<char>{__str, __len}; }
671 #ifdef _GLIBCXX_USE_WCHAR_T
672     inline constexpr basic_string_view<wchar_t>
673     operator""sv(const wchar_t* __str, size_t __len)
674     { return basic_string_view<wchar_t>{__str, __len}; }
675 #endif
677 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
678     inline constexpr basic_string_view<char16_t>
679     operator""sv(const char16_t* __str, size_t __len)
680     { return basic_string_view<char16_t>{__str, __len}; }
682     inline constexpr basic_string_view<char32_t>
683     operator""sv(const char32_t* __str, size_t __len)
684     { return basic_string_view<char32_t>{__str, __len}; }
685 #endif
687   }
688   }
690 _GLIBCXX_END_NAMESPACE_VERSION
691 } // namespace experimental
692 } // namespace std
694 #include <experimental/string_view.tcc>
696 #endif // __cplusplus <= 201103L
698 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW