libstdc++
string_view
Go to the documentation of this file.
1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
2 
3 // Copyright (C) 2013-2017 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.
10 
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.
15 
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.
19 
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/>.
24 
25 /** @file experimental/string_view
26  * This is a TS C++ Library header.
27  */
28 
29 //
30 // N3762 basic_string_view library
31 //
32 
33 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW
34 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1
35 
36 #pragma GCC system_header
37 
38 #if __cplusplus <= 201103L
39 # include <bits/c++14_warning.h>
40 #else
41 
42 #include <string>
43 #include <limits>
45 
46 namespace std _GLIBCXX_VISIBILITY(default)
47 {
48 namespace experimental
49 {
50 inline namespace fundamentals_v1
51 {
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 
54 #define __cpp_lib_experimental_string_view 201411
55 
56  /**
57  * @class basic_string_view <experimental/string_view>
58  * @brief A non-owning reference to a string.
59  *
60  * @ingroup strings
61  * @ingroup sequences
62  * @ingroup experimental
63  *
64  * @tparam _CharT Type of character
65  * @tparam _Traits Traits for character type, defaults to
66  * char_traits<_CharT>.
67  *
68  * A basic_string_view looks like this:
69  *
70  * @code
71  * _CharT* _M_str
72  * size_t _M_len
73  * @endcode
74  */
75  template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
77  {
78  public:
79 
80  // types
81  using traits_type = _Traits;
82  using value_type = _CharT;
83  using pointer = const _CharT*;
84  using const_pointer = const _CharT*;
85  using reference = const _CharT&;
86  using const_reference = const _CharT&;
87  using const_iterator = const _CharT*;
88  using iterator = const_iterator;
91  using size_type = size_t;
92  using difference_type = ptrdiff_t;
93  static constexpr size_type npos = size_type(-1);
94 
95  // [string.view.cons], construct/copy
96 
97  constexpr
98  basic_string_view() noexcept
99  : _M_len{0}, _M_str{nullptr}
100  { }
101 
102  constexpr basic_string_view(const basic_string_view&) noexcept = default;
103 
104  template<typename _Allocator>
105  basic_string_view(const basic_string<_CharT, _Traits,
106  _Allocator>& __str) noexcept
107  : _M_len{__str.length()}, _M_str{__str.data()}
108  { }
109 
110  constexpr basic_string_view(const _CharT* __str)
111  : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
112  _M_str{__str}
113  { }
114 
115  constexpr basic_string_view(const _CharT* __str, size_type __len)
116  : _M_len{__len},
117  _M_str{__str}
118  { }
119 
120  basic_string_view&
121  operator=(const basic_string_view&) noexcept = default;
122 
123  // [string.view.iterators], iterators
124 
125  constexpr const_iterator
126  begin() const noexcept
127  { return this->_M_str; }
128 
129  constexpr const_iterator
130  end() const noexcept
131  { return this->_M_str + this->_M_len; }
132 
133  constexpr const_iterator
134  cbegin() const noexcept
135  { return this->_M_str; }
136 
137  constexpr const_iterator
138  cend() const noexcept
139  { return this->_M_str + this->_M_len; }
140 
142  rbegin() const noexcept
143  { return const_reverse_iterator(this->end()); }
144 
146  rend() const noexcept
147  { return const_reverse_iterator(this->begin()); }
148 
150  crbegin() const noexcept
151  { return const_reverse_iterator(this->end()); }
152 
154  crend() const noexcept
155  { return const_reverse_iterator(this->begin()); }
156 
157  // [string.view.capacity], capacity
158 
159  constexpr size_type
160  size() const noexcept
161  { return this->_M_len; }
162 
163  constexpr size_type
164  length() const noexcept
165  { return _M_len; }
166 
167  constexpr size_type
168  max_size() const noexcept
169  {
170  return (npos - sizeof(size_type) - sizeof(void*))
171  / sizeof(value_type) / 4;
172  }
173 
174  constexpr bool
175  empty() const noexcept
176  { return this->_M_len == 0; }
177 
178  // [string.view.access], element access
179 
180  constexpr const _CharT&
181  operator[](size_type __pos) const
182  {
183  // TODO: Assert to restore in a way compatible with the constexpr.
184  // __glibcxx_assert(__pos < this->_M_len);
185  return *(this->_M_str + __pos);
186  }
187 
188  constexpr const _CharT&
189  at(size_type __pos) const
190  {
191  return __pos < this->_M_len
192  ? *(this->_M_str + __pos)
193  : (__throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
194  "(which is %zu) >= this->size() "
195  "(which is %zu)"),
196  __pos, this->size()),
197  *this->_M_str);
198  }
199 
200  constexpr const _CharT&
201  front() const
202  {
203  // TODO: Assert to restore in a way compatible with the constexpr.
204  // __glibcxx_assert(this->_M_len > 0);
205  return *this->_M_str;
206  }
207 
208  constexpr const _CharT&
209  back() const
210  {
211  // TODO: Assert to restore in a way compatible with the constexpr.
212  // __glibcxx_assert(this->_M_len > 0);
213  return *(this->_M_str + this->_M_len - 1);
214  }
215 
216  constexpr const _CharT*
217  data() const noexcept
218  { return this->_M_str; }
219 
220  // [string.view.modifiers], modifiers:
221 
222  constexpr void
223  remove_prefix(size_type __n)
224  {
225  __glibcxx_assert(this->_M_len >= __n);
226  this->_M_str += __n;
227  this->_M_len -= __n;
228  }
229 
230  constexpr void
231  remove_suffix(size_type __n)
232  { this->_M_len -= __n; }
233 
234  constexpr void
235  swap(basic_string_view& __sv) noexcept
236  {
237  auto __tmp = *this;
238  *this = __sv;
239  __sv = __tmp;
240  }
241 
242 
243  // [string.view.ops], string operations:
244 
245  template<typename _Allocator>
246  explicit operator basic_string<_CharT, _Traits, _Allocator>() const
247  {
248  return { this->_M_str, this->_M_len };
249  }
250 
251  template<typename _Allocator = std::allocator<_CharT>>
253  to_string(const _Allocator& __alloc = _Allocator()) const
254  {
255  return { this->_M_str, this->_M_len, __alloc };
256  }
257 
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  }
273 
274 
275  // [string.view.ops], string operations:
276 
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  }
288 
289  constexpr 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  }
298 
299  constexpr int
300  compare(size_type __pos1, size_type __n1, basic_string_view __str) const
301  { return this->substr(__pos1, __n1).compare(__str); }
302 
303  constexpr 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)); }
307 
308  constexpr int
309  compare(const _CharT* __str) const noexcept
310  { return this->compare(basic_string_view{__str}); }
311 
312  constexpr int
313  compare(size_type __pos1, size_type __n1, const _CharT* __str) const
314  { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
315 
316  constexpr 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  }
323 
324  constexpr 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); }
327 
328  constexpr size_type
329  find(_CharT __c, size_type __pos=0) const noexcept;
330 
331  constexpr size_type
332  find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
333 
334  constexpr size_type
335  find(const _CharT* __str, size_type __pos=0) const noexcept
336  { return this->find(__str, __pos, traits_type::length(__str)); }
337 
338  constexpr 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); }
341 
342  constexpr size_type
343  rfind(_CharT __c, size_type __pos = npos) const noexcept;
344 
345  constexpr size_type
346  rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
347 
348  constexpr size_type
349  rfind(const _CharT* __str, size_type __pos = npos) const noexcept
350  { return this->rfind(__str, __pos, traits_type::length(__str)); }
351 
352  constexpr 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); }
355 
356  constexpr size_type
357  find_first_of(_CharT __c, size_type __pos = 0) const noexcept
358  { return this->find(__c, __pos); }
359 
360  constexpr size_type
361  find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
362 
363  constexpr 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)); }
366 
367  constexpr 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); }
371 
372  constexpr size_type
373  find_last_of(_CharT __c, size_type __pos=npos) const noexcept
374  { return this->rfind(__c, __pos); }
375 
376  constexpr size_type
377  find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
378 
379  constexpr 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)); }
382 
383  constexpr 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); }
387 
388  constexpr size_type
389  find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
390 
391  constexpr size_type
392  find_first_not_of(const _CharT* __str,
393  size_type __pos, size_type __n) const;
394 
395  constexpr 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  }
401 
402  constexpr 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); }
406 
407  constexpr size_type
408  find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
409 
410  constexpr size_type
411  find_last_not_of(const _CharT* __str,
412  size_type __pos, size_type __n) const;
413 
414  constexpr 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  }
421 
422  private:
423 
424  static constexpr int
425  _S_compare(size_type __n1, size_type __n2) noexcept
426  {
427  return difference_type{__n1 - __n2} > std::numeric_limits<int>::max()
429  : difference_type{__n1 - __n2} < std::numeric_limits<int>::min()
431  : static_cast<int>(difference_type{__n1 - __n2});
432  }
433 
434  size_t _M_len;
435  const _CharT* _M_str;
436  };
437 
438 _GLIBCXX_END_NAMESPACE_VERSION
439 
440  // [string.view.comparison], non-member basic_string_view comparison functions
441 
442  namespace __detail
443  {
444 _GLIBCXX_BEGIN_NAMESPACE_VERSION
445  // Identity transform to create a non-deduced context, so that only one
446  // argument participates in template argument deduction and the other
447  // argument gets implicitly converted to the deduced type. See n3766.html.
448  template<typename _Tp>
449  using __idt = common_type_t<_Tp>;
450 _GLIBCXX_END_NAMESPACE_VERSION
451  }
452 
453 _GLIBCXX_BEGIN_NAMESPACE_VERSION
454 
455  template<typename _CharT, typename _Traits>
456  constexpr bool
457  operator==(basic_string_view<_CharT, _Traits> __x,
459  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
460 
461  template<typename _CharT, typename _Traits>
462  constexpr bool
463  operator==(basic_string_view<_CharT, _Traits> __x,
464  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
465  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
466 
467  template<typename _CharT, typename _Traits>
468  constexpr bool
469  operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
471  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
472 
473  template<typename _CharT, typename _Traits>
474  constexpr bool
475  operator!=(basic_string_view<_CharT, _Traits> __x,
477  { return !(__x == __y); }
478 
479  template<typename _CharT, typename _Traits>
480  constexpr bool
481  operator!=(basic_string_view<_CharT, _Traits> __x,
482  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
483  { return !(__x == __y); }
484 
485  template<typename _CharT, typename _Traits>
486  constexpr bool
487  operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
489  { return !(__x == __y); }
490 
491  template<typename _CharT, typename _Traits>
492  constexpr bool
493  operator< (basic_string_view<_CharT, _Traits> __x,
495  { return __x.compare(__y) < 0; }
496 
497  template<typename _CharT, typename _Traits>
498  constexpr bool
499  operator< (basic_string_view<_CharT, _Traits> __x,
500  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
501  { return __x.compare(__y) < 0; }
502 
503  template<typename _CharT, typename _Traits>
504  constexpr bool
505  operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
507  { return __x.compare(__y) < 0; }
508 
509  template<typename _CharT, typename _Traits>
510  constexpr bool
511  operator> (basic_string_view<_CharT, _Traits> __x,
513  { return __x.compare(__y) > 0; }
514 
515  template<typename _CharT, typename _Traits>
516  constexpr bool
517  operator> (basic_string_view<_CharT, _Traits> __x,
518  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
519  { return __x.compare(__y) > 0; }
520 
521  template<typename _CharT, typename _Traits>
522  constexpr bool
523  operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
525  { return __x.compare(__y) > 0; }
526 
527  template<typename _CharT, typename _Traits>
528  constexpr bool
529  operator<=(basic_string_view<_CharT, _Traits> __x,
531  { return __x.compare(__y) <= 0; }
532 
533  template<typename _CharT, typename _Traits>
534  constexpr bool
535  operator<=(basic_string_view<_CharT, _Traits> __x,
536  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
537  { return __x.compare(__y) <= 0; }
538 
539  template<typename _CharT, typename _Traits>
540  constexpr bool
541  operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
543  { return __x.compare(__y) <= 0; }
544 
545  template<typename _CharT, typename _Traits>
546  constexpr bool
547  operator>=(basic_string_view<_CharT, _Traits> __x,
549  { return __x.compare(__y) >= 0; }
550 
551  template<typename _CharT, typename _Traits>
552  constexpr bool
553  operator>=(basic_string_view<_CharT, _Traits> __x,
554  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
555  { return __x.compare(__y) >= 0; }
556 
557  template<typename _CharT, typename _Traits>
558  constexpr bool
559  operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
561  { return __x.compare(__y) >= 0; }
562 
563  // [string.view.io], Inserters and extractors
564  template<typename _CharT, typename _Traits>
566  operator<<(basic_ostream<_CharT, _Traits>& __os,
568  { return __ostream_insert(__os, __str.data(), __str.size()); }
569 
570 
571  // basic_string_view typedef names
572 
574 #ifdef _GLIBCXX_USE_WCHAR_T
576 #endif
577 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
580 #endif
581 
582 _GLIBCXX_END_NAMESPACE_VERSION
583 } // namespace fundamentals_v1
584 } // namespace experimental
585 
586 
587  // [string.view.hash], hash support:
588 
589 _GLIBCXX_BEGIN_NAMESPACE_VERSION
590  template<typename _Tp>
591  struct hash;
592 
593  template<>
594  struct hash<experimental::string_view>
595  : public __hash_base<size_t, experimental::string_view>
596  {
597  size_t
598  operator()(const experimental::string_view& __str) const noexcept
599  { return std::_Hash_impl::hash(__str.data(), __str.length()); }
600  };
601 
602  template<>
603  struct __is_fast_hash<hash<experimental::string_view>> : std::false_type
604  { };
605 
606 #ifdef _GLIBCXX_USE_WCHAR_T
607  template<>
608  struct hash<experimental::wstring_view>
609  : public __hash_base<size_t, wstring>
610  {
611  size_t
612  operator()(const experimental::wstring_view& __s) const noexcept
613  { return std::_Hash_impl::hash(__s.data(),
614  __s.length() * sizeof(wchar_t)); }
615  };
616 
617  template<>
618  struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type
619  { };
620 #endif
621 
622 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
623  template<>
624  struct hash<experimental::u16string_view>
625  : public __hash_base<size_t, experimental::u16string_view>
626  {
627  size_t
628  operator()(const experimental::u16string_view& __s) const noexcept
629  { return std::_Hash_impl::hash(__s.data(),
630  __s.length() * sizeof(char16_t)); }
631  };
632 
633  template<>
634  struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type
635  { };
636 
637  template<>
638  struct hash<experimental::u32string_view>
639  : public __hash_base<size_t, experimental::u32string_view>
640  {
641  size_t
642  operator()(const experimental::u32string_view& __s) const noexcept
643  { return std::_Hash_impl::hash(__s.data(),
644  __s.length() * sizeof(char32_t)); }
645  };
646 
647  template<>
648  struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type
649  { };
650 #endif
651 _GLIBCXX_END_NAMESPACE_VERSION
652 
653 namespace experimental
654 {
655  // I added these EMSR.
656  inline namespace literals
657  {
658  inline namespace string_view_literals
659  {
660  _GLIBCXX_BEGIN_NAMESPACE_VERSION
661 
662  inline constexpr basic_string_view<char>
663  operator""sv(const char* __str, size_t __len) noexcept
664  { return basic_string_view<char>{__str, __len}; }
665 
666 #ifdef _GLIBCXX_USE_WCHAR_T
667  inline constexpr basic_string_view<wchar_t>
668  operator""sv(const wchar_t* __str, size_t __len) noexcept
669  { return basic_string_view<wchar_t>{__str, __len}; }
670 #endif
671 
672 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
673  inline constexpr basic_string_view<char16_t>
674  operator""sv(const char16_t* __str, size_t __len) noexcept
675  { return basic_string_view<char16_t>{__str, __len}; }
676 
677  inline constexpr basic_string_view<char32_t>
678  operator""sv(const char32_t* __str, size_t __len) noexcept
679  { return basic_string_view<char32_t>{__str, __len}; }
680 #endif
681 
682  _GLIBCXX_END_NAMESPACE_VERSION
683  } // namespace string_literals
684  } // namespace literals
685 } // namespace experimental
686 } // namespace std
687 
689 
690 #endif // __cplusplus <= 201103L
691 
692 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:195
Primary class template hash.
Definition: system_error:142
A non-owning reference to a string.
Definition: string_view:76
Managing sequences of characters and character-like objects.
Template class basic_ostream.
Definition: iosfwd:86
static constexpr _Tp min() noexcept
Definition: limits:317
integral_constant
Definition: type_traits:69
static constexpr _Tp max() noexcept
Definition: limits:321