2013-11-19 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / libstdc++-v3 / include / experimental / string_view
blob6a95e8d0bd0ba38a5f23a5c9113b8a48079adda5
1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013 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   template<typename _CharT, typename _Traits = char_traits<_CharT>>
71     class basic_string_view
72     {
74     public:
76       // types
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
93       constexpr
94       basic_string_view() noexcept
95       : _M_len{0}, _M_str{nullptr}
96       { }
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()}
104         { }
106       constexpr basic_string_view(const _CharT* __str)
107       : _M_len{__str == nullptr ? 0 : traits_type::length(__str)}, _M_str{__str}
108       { }
110       constexpr basic_string_view(const _CharT* __str, size_type __len)
111       : _M_len{__len}, _M_str{__str}
112       { }
114       basic_string_view&
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
124       end() const noexcept
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
153       constexpr size_type
154       size() const noexcept
155       { return this->_M_len; }
157       constexpr size_type
158       length() const noexcept
159       { return _M_len; }
161       constexpr size_type
162       max_size() const noexcept
163       { return ((npos - sizeof(size_type) - sizeof(void*))
164                 / sizeof(value_type) / 4); }
166       constexpr bool
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
174       {
175         _GLIBCXX_DEBUG_ASSERT(__pos <= this->_M_len);
176         return *(this->_M_str + __pos);
177       }
179       constexpr const _CharT&
180       at(size_type __pos) const
181       {
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() "
186                                              "(which is %zu)"),
187                                          __pos, this->size()),
188                 *static_cast<pointer>(nullptr));
189       }
191       constexpr const _CharT&
192       front() const
193       {
194         _GLIBCXX_DEBUG_ASSERT(this->_M_len > 0);
195         return *this->_M_str;
196       }
198       constexpr const _CharT&
199       back() const
200       {
201         _GLIBCXX_DEBUG_ASSERT(this->_M_len > 0);
202         return *(this->_M_str + this->_M_len - 1);
203       }
205       constexpr const _CharT*
206       data() const noexcept
207       { return this->_M_str; }
209       // [string.view.modifiers], modifiers:
210       void
211       clear() noexcept
212       {
213         this->_M_len = 0;
214         this->_M_str = nullptr;
215       }
217       void
218       remove_prefix(size_type __n)
219       {
220         _GLIBCXX_DEBUG_ASSERT(this->_M_len >= __n);
221         this->_M_str += __n;
222         this->_M_len -= __n;
223       }
225       void
226       remove_suffix(size_type __n)
227       { this->_M_len -= __n; }
229       void
230       swap(basic_string_view& __sv) noexcept
231       {
232         std::swap(this->_M_len, __sv._M_len);
233         std::swap(this->_M_str, __sv._M_str);
234       }
237       // [string.view.ops], string operations:
239       template<typename _Allocator>
240         explicit operator basic_string<_CharT, _Traits, _Allocator>() const
241         {
242           return basic_string<_CharT, _Traits, _Allocator>
243                                         (this->_M_len, this->_M_str);
244         }
246       size_type
247       copy(_CharT* __str, size_type __n, size_type __pos = 0) const
248       {
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() "
253                                        "(which is %zu)"),
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++;
259         return __rlen;
260       }
263       // [string.view.ops], string operations:
265       constexpr basic_string_view
266       substr(size_type __pos, size_type __n=npos) const
267       {
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() "
273                                              "(which is %zu)"),
274                                      __pos, this->size()), basic_string_view{});
275       }
277       int
278       compare(basic_string_view __str) const noexcept
279       {
280         int __ret = traits_type::compare(this->_M_str, __str._M_str,
281                                          std::min(this->_M_len, __str._M_len));
282         if (__ret == 0)
283           __ret = _S_compare(this->_M_len, __str._M_len);
284         return __ret;
285       }
287       int
288       compare(size_type __pos1, size_type __n1, basic_string_view __str) const
289       { return this->substr(__pos1, __n1).compare(__str); }
291       int
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)); }
296       int
297       compare(const _CharT* __str) const noexcept
298       { return this->compare(basic_string_view{__str}); }
300       int
301       compare(size_type __pos1, size_type __n1, const _CharT* __str) const
302       { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
304       int
305       compare(size_type __pos1, size_type __n1,
306               const _CharT* __str, size_type __n2) const
307       {
308         return this->substr(__pos1, __n1)
309                    .compare(basic_string_view(__str, __n2));
310       }
312       size_type
313       find(basic_string_view __str, size_type __pos = 0) const noexcept
314       { return this->find(__str._M_str, __pos, __str._M_len); }
316       size_type
317       find(_CharT __c, size_type __pos=0) const noexcept;
319       size_type
320       find(const _CharT* __str, size_type __pos, size_type __n) const;
322       size_type
323       find(const _CharT* __str, size_type __pos=0) const noexcept
324       { return this->find(__str, __pos, traits_type::length(__str)); }
326       size_type
327       rfind(basic_string_view __str, size_type __pos = npos) const noexcept
328       { return this->rfind(__str._M_str, __pos, __str._M_len); }
330       size_type
331       rfind(_CharT __c, size_type __pos = npos) const noexcept;
333       size_type
334       rfind(const _CharT* __str, size_type __pos, size_type __n) const;
336       size_type
337       rfind(const _CharT* __str, size_type __pos = npos) const noexcept
338       { return this->rfind(__str, __pos, traits_type::length(__str)); }
340       size_type
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); }
344       size_type
345       find_first_of(_CharT __c, size_type __pos = 0) const noexcept
346       { return this->find(__c, __pos); }
348       size_type
349       find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
351       size_type
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)); }
355       size_type
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); }
360       size_type
361       find_last_of(_CharT __c, size_type __pos=npos) const noexcept
362       { return this->rfind(__c, __pos); }
364       size_type
365       find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
367       size_type
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)); }
371       size_type
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); }
376       size_type
377       find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
379       size_type
380       find_first_not_of(const _CharT* __str,
381                         size_type __pos, size_type __n) const;
383       size_type
384       find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
385       {
386         return this->find_first_not_of(__str, __pos,
387                                        traits_type::length(__str));
388       }
390       size_type
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); }
395       size_type
396       find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
398       size_type
399       find_last_not_of(const _CharT* __str,
400                        size_type __pos, size_type __n) const;
402       size_type
403       find_last_not_of(const _CharT* __str,
404                        size_type __pos = npos) const noexcept
405       {
406         return this->find_last_not_of(__str, __pos,
407                                       traits_type::length(__str));
408       }
410     private:
411       static constexpr const int
412       _S_compare(size_type __n1, size_type __n2) noexcept
413       {
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});
419       }
421       size_t        _M_len;
422       const _CharT* _M_str;
423     };
426   // [string.view.comparison], non-member basic_string_view comparison functions
428   namespace __detail
429   {
430     //  Identity transform to make ADL work with just one argument.
431     //  See n3766.html.
432     template<typename _Tp = void>
433       struct __identity
434       { typedef _Tp type; };
436     template<>
437       struct __identity<void>;
439     template<typename _Tp>
440       using __idt = typename __identity<_Tp>::type;
441   }
443   template<typename _CharT, typename _Traits>
444     bool
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>
450     bool
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>
456     bool
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>
462     bool
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>
468     bool
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>
474     bool
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>
480     bool
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>
486     bool
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>
492     bool
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>
498     bool
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>
504     bool
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>
510     bool
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>
516     bool
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>
522     bool
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>
528     bool
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>
534     bool
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>
540     bool
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>
546     bool
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())
559     {
560       return basic_string<_CharT, _Traits, _Allocator>
561                         (__str.begin(), __str.end(), __alloc);
562     }
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>;
576 #endif
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>;
580 #endif
582 _GLIBCXX_END_NAMESPACE_VERSION
583 } // namespace experimental
586   // [string.view.hash], hash support:
588 _GLIBCXX_BEGIN_NAMESPACE_VERSION
589   template<typename _Tp>
590     struct hash;
592   template<>
593     struct hash<experimental::string_view>
594     : public __hash_base<size_t, experimental::string_view>
595     {
596       size_t
597       operator()(const experimental::string_view& __str) const noexcept
598       { return std::_Hash_impl::hash(__str.data(), __str.length()); }
599     };
601   template<>
602     struct __is_fast_hash<hash<experimental::string_view>> : std::false_type
603     { };
605 #ifdef _GLIBCXX_USE_WCHAR_T
606   template<>
607     struct hash<experimental::wstring_view>
608     : public __hash_base<size_t, wstring>
609     {
610       size_t
611       operator()(const experimental::wstring_view& __s) const noexcept
612       { return std::_Hash_impl::hash(__s.data(),
613                                      __s.length() * sizeof(wchar_t)); }
614     };
616   template<>
617     struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type
618     { };
619 #endif
621 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
622   template<>
623     struct hash<experimental::u16string_view>
624     : public __hash_base<size_t, experimental::u16string_view>
625     {
626       size_t
627       operator()(const experimental::u16string_view& __s) const noexcept
628       { return std::_Hash_impl::hash(__s.data(),
629                                      __s.length() * sizeof(char16_t)); }
630     };
632   template<>
633     struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type
634     { };
636   template<>
637     struct hash<experimental::u32string_view>
638     : public __hash_base<size_t, experimental::u32string_view>
639     {
640       size_t
641       operator()(const experimental::u32string_view& __s) const noexcept
642       { return std::_Hash_impl::hash(__s.data(),
643                                      __s.length() * sizeof(char32_t)); }
644     };
646   template<>
647     struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type
648     { };
649 #endif
650 _GLIBCXX_END_NAMESPACE_VERSION
652 namespace experimental
654 _GLIBCXX_BEGIN_NAMESPACE_VERSION
656   // I added these EMSR.
657   inline namespace literals
658   {
659   inline namespace string_view_literals
660   {
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}; }
670 #endif
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}; }
680 #endif
682   }
683   }
685 _GLIBCXX_END_NAMESPACE_VERSION
686 } // namespace experimental
687 } // namespace std
689 #include <experimental/string_view.tcc>
691 #endif // __cplusplus <= 201103L
693 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW