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>
44 #include <experimental/bits/lfts_config.h>
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>>
76  class basic_string_view
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;
89  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
90  using reverse_iterator = const_reverse_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 
141  const_reverse_iterator
142  rbegin() const noexcept
143  { return const_reverse_iterator(this->end()); }
144 
145  const_reverse_iterator
146  rend() const noexcept
147  { return const_reverse_iterator(this->begin()); }
148 
149  const_reverse_iterator
150  crbegin() const noexcept
151  { return const_reverse_iterator(this->end()); }
152 
153  const_reverse_iterator
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  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  void
231  remove_suffix(size_type __n)
232  { this->_M_len -= __n; }
233 
234  void
235  swap(basic_string_view& __sv) noexcept
236  {
237  std::swap(this->_M_len, __sv._M_len);
238  std::swap(this->_M_str, __sv._M_str);
239  }
240 
241 
242  // [string.view.ops], string operations:
243 
244  template<typename _Allocator>
245  explicit operator basic_string<_CharT, _Traits, _Allocator>() const
246  {
247  return { this->_M_str, this->_M_len };
248  }
249 
250  template<typename _Allocator = std::allocator<_CharT>>
251  basic_string<_CharT, _Traits, _Allocator>
252  to_string(const _Allocator& __alloc = _Allocator()) const
253  {
254  return { this->_M_str, this->_M_len, __alloc };
255  }
256 
257  size_type
258  copy(_CharT* __str, size_type __n, size_type __pos = 0) const
259  {
260  __glibcxx_requires_string_len(__str, __n);
261  if (__pos > this->_M_len)
262  __throw_out_of_range_fmt(__N("basic_string_view::copy: __pos "
263  "(which is %zu) > this->size() "
264  "(which is %zu)"),
265  __pos, this->size());
266  size_type __rlen{std::min(__n, size_type{this->_M_len - __pos})};
267  for (auto __begin = this->_M_str + __pos,
268  __end = __begin + __rlen; __begin != __end;)
269  *__str++ = *__begin++;
270  return __rlen;
271  }
272 
273 
274  // [string.view.ops], string operations:
275 
276  constexpr basic_string_view
277  substr(size_type __pos, size_type __n=npos) const
278  {
279  return __pos <= this->_M_len
280  ? basic_string_view{this->_M_str + __pos,
281  std::min(__n, size_type{this->_M_len - __pos})}
282  : (__throw_out_of_range_fmt(__N("basic_string_view::substr: __pos "
283  "(which is %zu) > this->size() "
284  "(which is %zu)"),
285  __pos, this->size()), basic_string_view{});
286  }
287 
288  int
289  compare(basic_string_view __str) const noexcept
290  {
291  int __ret = traits_type::compare(this->_M_str, __str._M_str,
292  std::min(this->_M_len, __str._M_len));
293  if (__ret == 0)
294  __ret = _S_compare(this->_M_len, __str._M_len);
295  return __ret;
296  }
297 
298  int
299  compare(size_type __pos1, size_type __n1, basic_string_view __str) const
300  { return this->substr(__pos1, __n1).compare(__str); }
301 
302  int
303  compare(size_type __pos1, size_type __n1,
304  basic_string_view __str, size_type __pos2, size_type __n2) const
305  { return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
306 
307  int
308  compare(const _CharT* __str) const noexcept
309  { return this->compare(basic_string_view{__str}); }
310 
311  int
312  compare(size_type __pos1, size_type __n1, const _CharT* __str) const
313  { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
314 
315  int
316  compare(size_type __pos1, size_type __n1,
317  const _CharT* __str, size_type __n2) const
318  {
319  return this->substr(__pos1, __n1)
320  .compare(basic_string_view(__str, __n2));
321  }
322 
323  size_type
324  find(basic_string_view __str, size_type __pos = 0) const noexcept
325  { return this->find(__str._M_str, __pos, __str._M_len); }
326 
327  size_type
328  find(_CharT __c, size_type __pos=0) const noexcept;
329 
330  size_type
331  find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
332 
333  size_type
334  find(const _CharT* __str, size_type __pos=0) const noexcept
335  { return this->find(__str, __pos, traits_type::length(__str)); }
336 
337  size_type
338  rfind(basic_string_view __str, size_type __pos = npos) const noexcept
339  { return this->rfind(__str._M_str, __pos, __str._M_len); }
340 
341  size_type
342  rfind(_CharT __c, size_type __pos = npos) const noexcept;
343 
344  size_type
345  rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
346 
347  size_type
348  rfind(const _CharT* __str, size_type __pos = npos) const noexcept
349  { return this->rfind(__str, __pos, traits_type::length(__str)); }
350 
351  size_type
352  find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
353  { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
354 
355  size_type
356  find_first_of(_CharT __c, size_type __pos = 0) const noexcept
357  { return this->find(__c, __pos); }
358 
359  size_type
360  find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
361 
362  size_type
363  find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
364  { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
365 
366  size_type
367  find_last_of(basic_string_view __str,
368  size_type __pos = npos) const noexcept
369  { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
370 
371  size_type
372  find_last_of(_CharT __c, size_type __pos=npos) const noexcept
373  { return this->rfind(__c, __pos); }
374 
375  size_type
376  find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
377 
378  size_type
379  find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
380  { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
381 
382  size_type
383  find_first_not_of(basic_string_view __str,
384  size_type __pos = 0) const noexcept
385  { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
386 
387  size_type
388  find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
389 
390  size_type
391  find_first_not_of(const _CharT* __str,
392  size_type __pos, size_type __n) const;
393 
394  size_type
395  find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
396  {
397  return this->find_first_not_of(__str, __pos,
398  traits_type::length(__str));
399  }
400 
401  size_type
402  find_last_not_of(basic_string_view __str,
403  size_type __pos = npos) const noexcept
404  { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
405 
406  size_type
407  find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
408 
409  size_type
410  find_last_not_of(const _CharT* __str,
411  size_type __pos, size_type __n) const;
412 
413  size_type
414  find_last_not_of(const _CharT* __str,
415  size_type __pos = npos) const noexcept
416  {
417  return this->find_last_not_of(__str, __pos,
418  traits_type::length(__str));
419  }
420 
421  private:
422 
423  static constexpr int
424  _S_compare(size_type __n1, size_type __n2) noexcept
425  {
426  return difference_type{__n1 - __n2} > std::numeric_limits<int>::max()
427  ? std::numeric_limits<int>::max()
428  : difference_type{__n1 - __n2} < std::numeric_limits<int>::min()
429  ? std::numeric_limits<int>::min()
430  : static_cast<int>(difference_type{__n1 - __n2});
431  }
432 
433  size_t _M_len;
434  const _CharT* _M_str;
435  };
436 
437 _GLIBCXX_END_NAMESPACE_VERSION
438 
439  // [string.view.comparison], non-member basic_string_view comparison functions
440 
441  namespace __detail
442  {
443 _GLIBCXX_BEGIN_NAMESPACE_VERSION
444  // Identity transform to create a non-deduced context, so that only one
445  // argument participates in template argument deduction and the other
446  // argument gets implicitly converted to the deduced type. See n3766.html.
447  template<typename _Tp>
448  using __idt = common_type_t<_Tp>;
449 _GLIBCXX_END_NAMESPACE_VERSION
450  }
451 
452 _GLIBCXX_BEGIN_NAMESPACE_VERSION
453 
454  template<typename _CharT, typename _Traits>
455  inline bool
456  operator==(basic_string_view<_CharT, _Traits> __x,
457  basic_string_view<_CharT, _Traits> __y) noexcept
458  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
459 
460  template<typename _CharT, typename _Traits>
461  inline bool
462  operator==(basic_string_view<_CharT, _Traits> __x,
463  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
464  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
465 
466  template<typename _CharT, typename _Traits>
467  inline bool
468  operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
469  basic_string_view<_CharT, _Traits> __y) noexcept
470  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
471 
472  template<typename _CharT, typename _Traits>
473  inline bool
474  operator!=(basic_string_view<_CharT, _Traits> __x,
475  basic_string_view<_CharT, _Traits> __y) noexcept
476  { return !(__x == __y); }
477 
478  template<typename _CharT, typename _Traits>
479  inline bool
480  operator!=(basic_string_view<_CharT, _Traits> __x,
481  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
482  { return !(__x == __y); }
483 
484  template<typename _CharT, typename _Traits>
485  inline bool
486  operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
487  basic_string_view<_CharT, _Traits> __y) noexcept
488  { return !(__x == __y); }
489 
490  template<typename _CharT, typename _Traits>
491  inline bool
492  operator< (basic_string_view<_CharT, _Traits> __x,
493  basic_string_view<_CharT, _Traits> __y) noexcept
494  { return __x.compare(__y) < 0; }
495 
496  template<typename _CharT, typename _Traits>
497  inline 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; }
501 
502  template<typename _CharT, typename _Traits>
503  inline 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; }
507 
508  template<typename _CharT, typename _Traits>
509  inline bool
510  operator> (basic_string_view<_CharT, _Traits> __x,
511  basic_string_view<_CharT, _Traits> __y) noexcept
512  { return __x.compare(__y) > 0; }
513 
514  template<typename _CharT, typename _Traits>
515  inline 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; }
519 
520  template<typename _CharT, typename _Traits>
521  inline 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; }
525 
526  template<typename _CharT, typename _Traits>
527  inline bool
528  operator<=(basic_string_view<_CharT, _Traits> __x,
529  basic_string_view<_CharT, _Traits> __y) noexcept
530  { return __x.compare(__y) <= 0; }
531 
532  template<typename _CharT, typename _Traits>
533  inline 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; }
537 
538  template<typename _CharT, typename _Traits>
539  inline 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; }
543 
544  template<typename _CharT, typename _Traits>
545  inline bool
546  operator>=(basic_string_view<_CharT, _Traits> __x,
547  basic_string_view<_CharT, _Traits> __y) noexcept
548  { return __x.compare(__y) >= 0; }
549 
550  template<typename _CharT, typename _Traits>
551  inline bool
552  operator>=(basic_string_view<_CharT, _Traits> __x,
553  __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
554  { return __x.compare(__y) >= 0; }
555 
556  template<typename _CharT, typename _Traits>
557  inline bool
558  operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
559  basic_string_view<_CharT, _Traits> __y) noexcept
560  { return __x.compare(__y) >= 0; }
561 
562  // [string.view.io], Inserters and extractors
563  template<typename _CharT, typename _Traits>
564  inline basic_ostream<_CharT, _Traits>&
565  operator<<(basic_ostream<_CharT, _Traits>& __os,
566  basic_string_view<_CharT,_Traits> __str)
567  { return __ostream_insert(__os, __str.data(), __str.size()); }
568 
569 
570  // basic_string_view typedef names
571 
572  using string_view = basic_string_view<char>;
573 #ifdef _GLIBCXX_USE_WCHAR_T
574  using wstring_view = basic_string_view<wchar_t>;
575 #endif
576 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
577  using u16string_view = basic_string_view<char16_t>;
578  using u32string_view = basic_string_view<char32_t>;
579 #endif
580 
581 _GLIBCXX_END_NAMESPACE_VERSION
582 } // namespace fundamentals_v1
583 } // namespace experimental
584 
585 
586  // [string.view.hash], hash support:
587 
588 _GLIBCXX_BEGIN_NAMESPACE_VERSION
589  template<typename _Tp>
590  struct hash;
591 
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  };
600 
601  template<>
602  struct __is_fast_hash<hash<experimental::string_view>> : std::false_type
603  { };
604 
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  };
615 
616  template<>
617  struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type
618  { };
619 #endif
620 
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  };
631 
632  template<>
633  struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type
634  { };
635 
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  };
645 
646  template<>
647  struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type
648  { };
649 #endif
650 _GLIBCXX_END_NAMESPACE_VERSION
651 
652 namespace experimental
653 {
654  // I added these EMSR.
655  inline namespace literals
656  {
657  inline namespace string_view_literals
658  {
659  _GLIBCXX_BEGIN_NAMESPACE_VERSION
660 
661  inline constexpr basic_string_view<char>
662  operator""sv(const char* __str, size_t __len)
663  { return basic_string_view<char>{__str, __len}; }
664 
665 #ifdef _GLIBCXX_USE_WCHAR_T
666  inline constexpr basic_string_view<wchar_t>
667  operator""sv(const wchar_t* __str, size_t __len)
668  { return basic_string_view<wchar_t>{__str, __len}; }
669 #endif
670 
671 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
672  inline constexpr basic_string_view<char16_t>
673  operator""sv(const char16_t* __str, size_t __len)
674  { return basic_string_view<char16_t>{__str, __len}; }
675 
676  inline constexpr basic_string_view<char32_t>
677  operator""sv(const char32_t* __str, size_t __len)
678  { return basic_string_view<char32_t>{__str, __len}; }
679 #endif
680 
681  _GLIBCXX_END_NAMESPACE_VERSION
682  } // namespace string_literals
683  } // namespace literals
684 } // namespace experimental
685 } // namespace std
686 
687 #include <experimental/bits/string_view.tcc>
688 
689 #endif // __cplusplus <= 201103L
690 
691 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW