xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/std/string_view (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj// Components for manipulating non-owning sequences of characters -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj// Copyright (C) 2013-2018 Free Software Foundation, Inc.
4*38fd1498Szrj//
5*38fd1498Szrj// This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj// software; you can redistribute it and/or modify it under the
7*38fd1498Szrj// terms of the GNU General Public License as published by the
8*38fd1498Szrj// Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj// any later version.
10*38fd1498Szrj
11*38fd1498Szrj// This library is distributed in the hope that it will be useful,
12*38fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj// GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj// permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj// 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj// You should have received a copy of the GNU General Public License and
21*38fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj// <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj/** @file string_view
26*38fd1498Szrj *  This is a Standard C++ Library header.
27*38fd1498Szrj */
28*38fd1498Szrj
29*38fd1498Szrj//
30*38fd1498Szrj// N3762 basic_string_view library
31*38fd1498Szrj//
32*38fd1498Szrj
33*38fd1498Szrj#ifndef _GLIBCXX_STRING_VIEW
34*38fd1498Szrj#define _GLIBCXX_STRING_VIEW 1
35*38fd1498Szrj
36*38fd1498Szrj#pragma GCC system_header
37*38fd1498Szrj
38*38fd1498Szrj#if __cplusplus >= 201703L
39*38fd1498Szrj
40*38fd1498Szrj#include <limits>
41*38fd1498Szrj#include <iosfwd>
42*38fd1498Szrj#include <bits/char_traits.h>
43*38fd1498Szrj#include <bits/functional_hash.h>
44*38fd1498Szrj#include <bits/range_access.h>
45*38fd1498Szrj
46*38fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default)
47*38fd1498Szrj{
48*38fd1498Szrj_GLIBCXX_BEGIN_NAMESPACE_VERSION
49*38fd1498Szrj
50*38fd1498Szrj#define __cpp_lib_string_view 201603
51*38fd1498Szrj
52*38fd1498Szrj  /**
53*38fd1498Szrj   *  @class basic_string_view <string_view>
54*38fd1498Szrj   *  @brief  A non-owning reference to a string.
55*38fd1498Szrj   *
56*38fd1498Szrj   *  @ingroup strings
57*38fd1498Szrj   *  @ingroup sequences
58*38fd1498Szrj   *
59*38fd1498Szrj   *  @tparam _CharT  Type of character
60*38fd1498Szrj   *  @tparam _Traits  Traits for character type, defaults to
61*38fd1498Szrj   *                   char_traits<_CharT>.
62*38fd1498Szrj   *
63*38fd1498Szrj   *  A basic_string_view looks like this:
64*38fd1498Szrj   *
65*38fd1498Szrj   *  @code
66*38fd1498Szrj   *    _CharT*    _M_str
67*38fd1498Szrj   *    size_t     _M_len
68*38fd1498Szrj   *  @endcode
69*38fd1498Szrj   */
70*38fd1498Szrj  template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
71*38fd1498Szrj    class basic_string_view
72*38fd1498Szrj    {
73*38fd1498Szrj    public:
74*38fd1498Szrj
75*38fd1498Szrj      // types
76*38fd1498Szrj      using traits_type = _Traits;
77*38fd1498Szrj      using value_type = _CharT;
78*38fd1498Szrj      using pointer = const _CharT*;
79*38fd1498Szrj      using const_pointer = const _CharT*;
80*38fd1498Szrj      using reference = const _CharT&;
81*38fd1498Szrj      using const_reference = const _CharT&;
82*38fd1498Szrj      using const_iterator = const _CharT*;
83*38fd1498Szrj      using iterator = const_iterator;
84*38fd1498Szrj      using const_reverse_iterator = std::reverse_iterator<const_iterator>;
85*38fd1498Szrj      using reverse_iterator = const_reverse_iterator;
86*38fd1498Szrj      using size_type = size_t;
87*38fd1498Szrj      using difference_type = ptrdiff_t;
88*38fd1498Szrj      static constexpr size_type npos = size_type(-1);
89*38fd1498Szrj
90*38fd1498Szrj      // [string.view.cons], construct/copy
91*38fd1498Szrj
92*38fd1498Szrj      constexpr
93*38fd1498Szrj      basic_string_view() noexcept
94*38fd1498Szrj      : _M_len{0}, _M_str{nullptr}
95*38fd1498Szrj      { }
96*38fd1498Szrj
97*38fd1498Szrj      constexpr basic_string_view(const basic_string_view&) noexcept = default;
98*38fd1498Szrj
99*38fd1498Szrj      constexpr basic_string_view(const _CharT* __str) noexcept
100*38fd1498Szrj      : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
101*38fd1498Szrj	_M_str{__str}
102*38fd1498Szrj      { }
103*38fd1498Szrj
104*38fd1498Szrj      constexpr
105*38fd1498Szrj      basic_string_view(const _CharT* __str, size_type __len) noexcept
106*38fd1498Szrj      : _M_len{__len}, _M_str{__str}
107*38fd1498Szrj      { }
108*38fd1498Szrj
109*38fd1498Szrj      constexpr basic_string_view&
110*38fd1498Szrj      operator=(const basic_string_view&) noexcept = default;
111*38fd1498Szrj
112*38fd1498Szrj      // [string.view.iterators], iterators
113*38fd1498Szrj
114*38fd1498Szrj      constexpr const_iterator
115*38fd1498Szrj      begin() const noexcept
116*38fd1498Szrj      { return this->_M_str; }
117*38fd1498Szrj
118*38fd1498Szrj      constexpr const_iterator
119*38fd1498Szrj      end() const noexcept
120*38fd1498Szrj      { return this->_M_str + this->_M_len; }
121*38fd1498Szrj
122*38fd1498Szrj      constexpr const_iterator
123*38fd1498Szrj      cbegin() const noexcept
124*38fd1498Szrj      { return this->_M_str; }
125*38fd1498Szrj
126*38fd1498Szrj      constexpr const_iterator
127*38fd1498Szrj      cend() const noexcept
128*38fd1498Szrj      { return this->_M_str + this->_M_len; }
129*38fd1498Szrj
130*38fd1498Szrj      constexpr const_reverse_iterator
131*38fd1498Szrj      rbegin() const noexcept
132*38fd1498Szrj      { return const_reverse_iterator(this->end()); }
133*38fd1498Szrj
134*38fd1498Szrj      constexpr const_reverse_iterator
135*38fd1498Szrj      rend() const noexcept
136*38fd1498Szrj      { return const_reverse_iterator(this->begin()); }
137*38fd1498Szrj
138*38fd1498Szrj      constexpr const_reverse_iterator
139*38fd1498Szrj      crbegin() const noexcept
140*38fd1498Szrj      { return const_reverse_iterator(this->end()); }
141*38fd1498Szrj
142*38fd1498Szrj      constexpr const_reverse_iterator
143*38fd1498Szrj      crend() const noexcept
144*38fd1498Szrj      { return const_reverse_iterator(this->begin()); }
145*38fd1498Szrj
146*38fd1498Szrj      // [string.view.capacity], capacity
147*38fd1498Szrj
148*38fd1498Szrj      constexpr size_type
149*38fd1498Szrj      size() const noexcept
150*38fd1498Szrj      { return this->_M_len; }
151*38fd1498Szrj
152*38fd1498Szrj      constexpr size_type
153*38fd1498Szrj      length() const noexcept
154*38fd1498Szrj      { return _M_len; }
155*38fd1498Szrj
156*38fd1498Szrj      constexpr size_type
157*38fd1498Szrj      max_size() const noexcept
158*38fd1498Szrj      {
159*38fd1498Szrj	return (npos - sizeof(size_type) - sizeof(void*))
160*38fd1498Szrj		/ sizeof(value_type) / 4;
161*38fd1498Szrj      }
162*38fd1498Szrj
163*38fd1498Szrj      [[nodiscard]] constexpr bool
164*38fd1498Szrj      empty() const noexcept
165*38fd1498Szrj      { return this->_M_len == 0; }
166*38fd1498Szrj
167*38fd1498Szrj      // [string.view.access], element access
168*38fd1498Szrj
169*38fd1498Szrj      constexpr const _CharT&
170*38fd1498Szrj      operator[](size_type __pos) const noexcept
171*38fd1498Szrj      {
172*38fd1498Szrj	// TODO: Assert to restore in a way compatible with the constexpr.
173*38fd1498Szrj	// __glibcxx_assert(__pos < this->_M_len);
174*38fd1498Szrj	return *(this->_M_str + __pos);
175*38fd1498Szrj      }
176*38fd1498Szrj
177*38fd1498Szrj      constexpr const _CharT&
178*38fd1498Szrj      at(size_type __pos) const
179*38fd1498Szrj      {
180*38fd1498Szrj	if (__pos >= _M_len)
181*38fd1498Szrj	  __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
182*38fd1498Szrj				       "(which is %zu) >= this->size() "
183*38fd1498Szrj				       "(which is %zu)"), __pos, this->size());
184*38fd1498Szrj	return *(this->_M_str + __pos);
185*38fd1498Szrj      }
186*38fd1498Szrj
187*38fd1498Szrj      constexpr const _CharT&
188*38fd1498Szrj      front() const noexcept
189*38fd1498Szrj      {
190*38fd1498Szrj	// TODO: Assert to restore in a way compatible with the constexpr.
191*38fd1498Szrj	// __glibcxx_assert(this->_M_len > 0);
192*38fd1498Szrj	return *this->_M_str;
193*38fd1498Szrj      }
194*38fd1498Szrj
195*38fd1498Szrj      constexpr const _CharT&
196*38fd1498Szrj      back() const noexcept
197*38fd1498Szrj      {
198*38fd1498Szrj	// TODO: Assert to restore in a way compatible with the constexpr.
199*38fd1498Szrj	// __glibcxx_assert(this->_M_len > 0);
200*38fd1498Szrj	return *(this->_M_str + this->_M_len - 1);
201*38fd1498Szrj      }
202*38fd1498Szrj
203*38fd1498Szrj      constexpr const _CharT*
204*38fd1498Szrj      data() const noexcept
205*38fd1498Szrj      { return this->_M_str; }
206*38fd1498Szrj
207*38fd1498Szrj      // [string.view.modifiers], modifiers:
208*38fd1498Szrj
209*38fd1498Szrj      constexpr void
210*38fd1498Szrj      remove_prefix(size_type __n) noexcept
211*38fd1498Szrj      {
212*38fd1498Szrj	__glibcxx_assert(this->_M_len >= __n);
213*38fd1498Szrj	this->_M_str += __n;
214*38fd1498Szrj	this->_M_len -= __n;
215*38fd1498Szrj      }
216*38fd1498Szrj
217*38fd1498Szrj      constexpr void
218*38fd1498Szrj      remove_suffix(size_type __n) noexcept
219*38fd1498Szrj      { this->_M_len -= __n; }
220*38fd1498Szrj
221*38fd1498Szrj      constexpr void
222*38fd1498Szrj      swap(basic_string_view& __sv) noexcept
223*38fd1498Szrj      {
224*38fd1498Szrj	auto __tmp = *this;
225*38fd1498Szrj	*this = __sv;
226*38fd1498Szrj	__sv = __tmp;
227*38fd1498Szrj      }
228*38fd1498Szrj
229*38fd1498Szrj
230*38fd1498Szrj      // [string.view.ops], string operations:
231*38fd1498Szrj
232*38fd1498Szrj      size_type
233*38fd1498Szrj      copy(_CharT* __str, size_type __n, size_type __pos = 0) const
234*38fd1498Szrj      {
235*38fd1498Szrj	__glibcxx_requires_string_len(__str, __n);
236*38fd1498Szrj	__pos = _M_check(__pos, "basic_string_view::copy");
237*38fd1498Szrj	const size_type __rlen = std::min(__n, _M_len - __pos);
238*38fd1498Szrj	for (auto __begin = this->_M_str + __pos,
239*38fd1498Szrj	     __end = __begin + __rlen; __begin != __end;)
240*38fd1498Szrj	  *__str++ = *__begin++;
241*38fd1498Szrj	return __rlen;
242*38fd1498Szrj      }
243*38fd1498Szrj
244*38fd1498Szrj      constexpr basic_string_view
245*38fd1498Szrj      substr(size_type __pos, size_type __n = npos) const noexcept(false)
246*38fd1498Szrj      {
247*38fd1498Szrj	__pos = _M_check(__pos, "basic_string_view::substr");
248*38fd1498Szrj	const size_type __rlen = std::min(__n, _M_len - __pos);
249*38fd1498Szrj	return basic_string_view{_M_str + __pos, __rlen};
250*38fd1498Szrj      }
251*38fd1498Szrj
252*38fd1498Szrj      constexpr int
253*38fd1498Szrj      compare(basic_string_view __str) const noexcept
254*38fd1498Szrj      {
255*38fd1498Szrj	const size_type __rlen = std::min(this->_M_len, __str._M_len);
256*38fd1498Szrj	int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
257*38fd1498Szrj	if (__ret == 0)
258*38fd1498Szrj	  __ret = _S_compare(this->_M_len, __str._M_len);
259*38fd1498Szrj	return __ret;
260*38fd1498Szrj      }
261*38fd1498Szrj
262*38fd1498Szrj      constexpr int
263*38fd1498Szrj      compare(size_type __pos1, size_type __n1, basic_string_view __str) const
264*38fd1498Szrj      { return this->substr(__pos1, __n1).compare(__str); }
265*38fd1498Szrj
266*38fd1498Szrj      constexpr int
267*38fd1498Szrj      compare(size_type __pos1, size_type __n1,
268*38fd1498Szrj	      basic_string_view __str, size_type __pos2, size_type __n2) const
269*38fd1498Szrj      {
270*38fd1498Szrj	return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
271*38fd1498Szrj      }
272*38fd1498Szrj
273*38fd1498Szrj      constexpr int
274*38fd1498Szrj      compare(const _CharT* __str) const noexcept
275*38fd1498Szrj      { return this->compare(basic_string_view{__str}); }
276*38fd1498Szrj
277*38fd1498Szrj      constexpr int
278*38fd1498Szrj      compare(size_type __pos1, size_type __n1, const _CharT* __str) const
279*38fd1498Szrj      { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
280*38fd1498Szrj
281*38fd1498Szrj      constexpr int
282*38fd1498Szrj      compare(size_type __pos1, size_type __n1,
283*38fd1498Szrj	      const _CharT* __str, size_type __n2) const noexcept(false)
284*38fd1498Szrj      {
285*38fd1498Szrj	return this->substr(__pos1, __n1)
286*38fd1498Szrj		   .compare(basic_string_view(__str, __n2));
287*38fd1498Szrj      }
288*38fd1498Szrj
289*38fd1498Szrj      constexpr size_type
290*38fd1498Szrj      find(basic_string_view __str, size_type __pos = 0) const noexcept
291*38fd1498Szrj      { return this->find(__str._M_str, __pos, __str._M_len); }
292*38fd1498Szrj
293*38fd1498Szrj      constexpr size_type
294*38fd1498Szrj      find(_CharT __c, size_type __pos = 0) const noexcept;
295*38fd1498Szrj
296*38fd1498Szrj      constexpr size_type
297*38fd1498Szrj      find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
298*38fd1498Szrj
299*38fd1498Szrj      constexpr size_type
300*38fd1498Szrj      find(const _CharT* __str, size_type __pos = 0) const noexcept
301*38fd1498Szrj      { return this->find(__str, __pos, traits_type::length(__str)); }
302*38fd1498Szrj
303*38fd1498Szrj      constexpr size_type
304*38fd1498Szrj      rfind(basic_string_view __str, size_type __pos = npos) const noexcept
305*38fd1498Szrj      { return this->rfind(__str._M_str, __pos, __str._M_len); }
306*38fd1498Szrj
307*38fd1498Szrj      constexpr size_type
308*38fd1498Szrj      rfind(_CharT __c, size_type __pos = npos) const noexcept;
309*38fd1498Szrj
310*38fd1498Szrj      constexpr size_type
311*38fd1498Szrj      rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
312*38fd1498Szrj
313*38fd1498Szrj      constexpr size_type
314*38fd1498Szrj      rfind(const _CharT* __str, size_type __pos = npos) const noexcept
315*38fd1498Szrj      { return this->rfind(__str, __pos, traits_type::length(__str)); }
316*38fd1498Szrj
317*38fd1498Szrj      constexpr size_type
318*38fd1498Szrj      find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
319*38fd1498Szrj      { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
320*38fd1498Szrj
321*38fd1498Szrj      constexpr size_type
322*38fd1498Szrj      find_first_of(_CharT __c, size_type __pos = 0) const noexcept
323*38fd1498Szrj      { return this->find(__c, __pos); }
324*38fd1498Szrj
325*38fd1498Szrj      constexpr size_type
326*38fd1498Szrj      find_first_of(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
327*38fd1498Szrj
328*38fd1498Szrj      constexpr size_type
329*38fd1498Szrj      find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
330*38fd1498Szrj      { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
331*38fd1498Szrj
332*38fd1498Szrj      constexpr size_type
333*38fd1498Szrj      find_last_of(basic_string_view __str,
334*38fd1498Szrj		   size_type __pos = npos) const noexcept
335*38fd1498Szrj      { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
336*38fd1498Szrj
337*38fd1498Szrj      constexpr size_type
338*38fd1498Szrj      find_last_of(_CharT __c, size_type __pos=npos) const noexcept
339*38fd1498Szrj      { return this->rfind(__c, __pos); }
340*38fd1498Szrj
341*38fd1498Szrj      constexpr size_type
342*38fd1498Szrj      find_last_of(const _CharT* __str, size_type __pos,
343*38fd1498Szrj		   size_type __n) const noexcept;
344*38fd1498Szrj
345*38fd1498Szrj      constexpr size_type
346*38fd1498Szrj      find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
347*38fd1498Szrj      { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
348*38fd1498Szrj
349*38fd1498Szrj      constexpr size_type
350*38fd1498Szrj      find_first_not_of(basic_string_view __str,
351*38fd1498Szrj			size_type __pos = 0) const noexcept
352*38fd1498Szrj      { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
353*38fd1498Szrj
354*38fd1498Szrj      constexpr size_type
355*38fd1498Szrj      find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
356*38fd1498Szrj
357*38fd1498Szrj      constexpr size_type
358*38fd1498Szrj      find_first_not_of(const _CharT* __str,
359*38fd1498Szrj			size_type __pos, size_type __n) const noexcept;
360*38fd1498Szrj
361*38fd1498Szrj      constexpr size_type
362*38fd1498Szrj      find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
363*38fd1498Szrj      {
364*38fd1498Szrj	return this->find_first_not_of(__str, __pos,
365*38fd1498Szrj				       traits_type::length(__str));
366*38fd1498Szrj      }
367*38fd1498Szrj
368*38fd1498Szrj      constexpr size_type
369*38fd1498Szrj      find_last_not_of(basic_string_view __str,
370*38fd1498Szrj		       size_type __pos = npos) const noexcept
371*38fd1498Szrj      { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
372*38fd1498Szrj
373*38fd1498Szrj      constexpr size_type
374*38fd1498Szrj      find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
375*38fd1498Szrj
376*38fd1498Szrj      constexpr size_type
377*38fd1498Szrj      find_last_not_of(const _CharT* __str,
378*38fd1498Szrj		       size_type __pos, size_type __n) const noexcept;
379*38fd1498Szrj
380*38fd1498Szrj      constexpr size_type
381*38fd1498Szrj      find_last_not_of(const _CharT* __str,
382*38fd1498Szrj		       size_type __pos = npos) const noexcept
383*38fd1498Szrj      {
384*38fd1498Szrj	return this->find_last_not_of(__str, __pos,
385*38fd1498Szrj				      traits_type::length(__str));
386*38fd1498Szrj      }
387*38fd1498Szrj
388*38fd1498Szrj      constexpr size_type
389*38fd1498Szrj      _M_check(size_type __pos, const char* __s) const noexcept(false)
390*38fd1498Szrj      {
391*38fd1498Szrj	if (__pos > this->size())
392*38fd1498Szrj	  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
393*38fd1498Szrj				       "this->size() (which is %zu)"),
394*38fd1498Szrj				   __s, __pos, this->size());
395*38fd1498Szrj	return __pos;
396*38fd1498Szrj      }
397*38fd1498Szrj
398*38fd1498Szrj      // NB: _M_limit doesn't check for a bad __pos value.
399*38fd1498Szrj      constexpr size_type
400*38fd1498Szrj      _M_limit(size_type __pos, size_type __off) const noexcept
401*38fd1498Szrj      {
402*38fd1498Szrj	const bool __testoff =  __off < this->size() - __pos;
403*38fd1498Szrj	return __testoff ? __off : this->size() - __pos;
404*38fd1498Szrj      }
405*38fd1498Szrj
406*38fd1498Szrj    private:
407*38fd1498Szrj
408*38fd1498Szrj      static constexpr int
409*38fd1498Szrj      _S_compare(size_type __n1, size_type __n2) noexcept
410*38fd1498Szrj      {
411*38fd1498Szrj	const difference_type __diff = __n1 - __n2;
412*38fd1498Szrj	if (__diff > std::numeric_limits<int>::max())
413*38fd1498Szrj	  return std::numeric_limits<int>::max();
414*38fd1498Szrj	if (__diff < std::numeric_limits<int>::min())
415*38fd1498Szrj	  return std::numeric_limits<int>::min();
416*38fd1498Szrj	return static_cast<int>(__diff);
417*38fd1498Szrj      }
418*38fd1498Szrj
419*38fd1498Szrj      size_t	    _M_len;
420*38fd1498Szrj      const _CharT* _M_str;
421*38fd1498Szrj    };
422*38fd1498Szrj
423*38fd1498Szrj  // [string.view.comparison], non-member basic_string_view comparison function
424*38fd1498Szrj
425*38fd1498Szrj  namespace __detail
426*38fd1498Szrj  {
427*38fd1498Szrj    // Identity transform to create a non-deduced context, so that only one
428*38fd1498Szrj    // argument participates in template argument deduction and the other
429*38fd1498Szrj    // argument gets implicitly converted to the deduced type. See n3766.html.
430*38fd1498Szrj    template<typename _Tp>
431*38fd1498Szrj      using __idt = common_type_t<_Tp>;
432*38fd1498Szrj  }
433*38fd1498Szrj
434*38fd1498Szrj  template<typename _CharT, typename _Traits>
435*38fd1498Szrj    constexpr bool
436*38fd1498Szrj    operator==(basic_string_view<_CharT, _Traits> __x,
437*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
438*38fd1498Szrj    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
439*38fd1498Szrj
440*38fd1498Szrj  template<typename _CharT, typename _Traits>
441*38fd1498Szrj    constexpr bool
442*38fd1498Szrj    operator==(basic_string_view<_CharT, _Traits> __x,
443*38fd1498Szrj               __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
444*38fd1498Szrj    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
445*38fd1498Szrj
446*38fd1498Szrj  template<typename _CharT, typename _Traits>
447*38fd1498Szrj    constexpr bool
448*38fd1498Szrj    operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
449*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
450*38fd1498Szrj    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
451*38fd1498Szrj
452*38fd1498Szrj  template<typename _CharT, typename _Traits>
453*38fd1498Szrj    constexpr bool
454*38fd1498Szrj    operator!=(basic_string_view<_CharT, _Traits> __x,
455*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
456*38fd1498Szrj    { return !(__x == __y); }
457*38fd1498Szrj
458*38fd1498Szrj  template<typename _CharT, typename _Traits>
459*38fd1498Szrj    constexpr bool
460*38fd1498Szrj    operator!=(basic_string_view<_CharT, _Traits> __x,
461*38fd1498Szrj               __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
462*38fd1498Szrj    { return !(__x == __y); }
463*38fd1498Szrj
464*38fd1498Szrj  template<typename _CharT, typename _Traits>
465*38fd1498Szrj    constexpr bool
466*38fd1498Szrj    operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
467*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
468*38fd1498Szrj    { return !(__x == __y); }
469*38fd1498Szrj
470*38fd1498Szrj  template<typename _CharT, typename _Traits>
471*38fd1498Szrj    constexpr bool
472*38fd1498Szrj    operator< (basic_string_view<_CharT, _Traits> __x,
473*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
474*38fd1498Szrj    { return __x.compare(__y) < 0; }
475*38fd1498Szrj
476*38fd1498Szrj  template<typename _CharT, typename _Traits>
477*38fd1498Szrj    constexpr bool
478*38fd1498Szrj    operator< (basic_string_view<_CharT, _Traits> __x,
479*38fd1498Szrj               __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
480*38fd1498Szrj    { return __x.compare(__y) < 0; }
481*38fd1498Szrj
482*38fd1498Szrj  template<typename _CharT, typename _Traits>
483*38fd1498Szrj    constexpr bool
484*38fd1498Szrj    operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
485*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
486*38fd1498Szrj    { return __x.compare(__y) < 0; }
487*38fd1498Szrj
488*38fd1498Szrj  template<typename _CharT, typename _Traits>
489*38fd1498Szrj    constexpr bool
490*38fd1498Szrj    operator> (basic_string_view<_CharT, _Traits> __x,
491*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
492*38fd1498Szrj    { return __x.compare(__y) > 0; }
493*38fd1498Szrj
494*38fd1498Szrj  template<typename _CharT, typename _Traits>
495*38fd1498Szrj    constexpr bool
496*38fd1498Szrj    operator> (basic_string_view<_CharT, _Traits> __x,
497*38fd1498Szrj               __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
498*38fd1498Szrj    { return __x.compare(__y) > 0; }
499*38fd1498Szrj
500*38fd1498Szrj  template<typename _CharT, typename _Traits>
501*38fd1498Szrj    constexpr bool
502*38fd1498Szrj    operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
503*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
504*38fd1498Szrj    { return __x.compare(__y) > 0; }
505*38fd1498Szrj
506*38fd1498Szrj  template<typename _CharT, typename _Traits>
507*38fd1498Szrj    constexpr bool
508*38fd1498Szrj    operator<=(basic_string_view<_CharT, _Traits> __x,
509*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
510*38fd1498Szrj    { return __x.compare(__y) <= 0; }
511*38fd1498Szrj
512*38fd1498Szrj  template<typename _CharT, typename _Traits>
513*38fd1498Szrj    constexpr bool
514*38fd1498Szrj    operator<=(basic_string_view<_CharT, _Traits> __x,
515*38fd1498Szrj               __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
516*38fd1498Szrj    { return __x.compare(__y) <= 0; }
517*38fd1498Szrj
518*38fd1498Szrj  template<typename _CharT, typename _Traits>
519*38fd1498Szrj    constexpr bool
520*38fd1498Szrj    operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
521*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
522*38fd1498Szrj    { return __x.compare(__y) <= 0; }
523*38fd1498Szrj
524*38fd1498Szrj  template<typename _CharT, typename _Traits>
525*38fd1498Szrj    constexpr bool
526*38fd1498Szrj    operator>=(basic_string_view<_CharT, _Traits> __x,
527*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
528*38fd1498Szrj    { return __x.compare(__y) >= 0; }
529*38fd1498Szrj
530*38fd1498Szrj  template<typename _CharT, typename _Traits>
531*38fd1498Szrj    constexpr bool
532*38fd1498Szrj    operator>=(basic_string_view<_CharT, _Traits> __x,
533*38fd1498Szrj               __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
534*38fd1498Szrj    { return __x.compare(__y) >= 0; }
535*38fd1498Szrj
536*38fd1498Szrj  template<typename _CharT, typename _Traits>
537*38fd1498Szrj    constexpr bool
538*38fd1498Szrj    operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
539*38fd1498Szrj               basic_string_view<_CharT, _Traits> __y) noexcept
540*38fd1498Szrj    { return __x.compare(__y) >= 0; }
541*38fd1498Szrj
542*38fd1498Szrj  // [string.view.io], Inserters and extractors
543*38fd1498Szrj  template<typename _CharT, typename _Traits>
544*38fd1498Szrj    inline basic_ostream<_CharT, _Traits>&
545*38fd1498Szrj    operator<<(basic_ostream<_CharT, _Traits>& __os,
546*38fd1498Szrj	       basic_string_view<_CharT,_Traits> __str)
547*38fd1498Szrj    { return __ostream_insert(__os, __str.data(), __str.size()); }
548*38fd1498Szrj
549*38fd1498Szrj
550*38fd1498Szrj  // basic_string_view typedef names
551*38fd1498Szrj
552*38fd1498Szrj  using string_view = basic_string_view<char>;
553*38fd1498Szrj#ifdef _GLIBCXX_USE_WCHAR_T
554*38fd1498Szrj  using wstring_view = basic_string_view<wchar_t>;
555*38fd1498Szrj#endif
556*38fd1498Szrj#ifdef _GLIBCXX_USE_C99_STDINT_TR1
557*38fd1498Szrj  using u16string_view = basic_string_view<char16_t>;
558*38fd1498Szrj  using u32string_view = basic_string_view<char32_t>;
559*38fd1498Szrj#endif
560*38fd1498Szrj
561*38fd1498Szrj  // [string.view.hash], hash support:
562*38fd1498Szrj
563*38fd1498Szrj  template<typename _Tp>
564*38fd1498Szrj    struct hash;
565*38fd1498Szrj
566*38fd1498Szrj  template<>
567*38fd1498Szrj    struct hash<string_view>
568*38fd1498Szrj    : public __hash_base<size_t, string_view>
569*38fd1498Szrj    {
570*38fd1498Szrj      size_t
571*38fd1498Szrj      operator()(const string_view& __str) const noexcept
572*38fd1498Szrj      { return std::_Hash_impl::hash(__str.data(), __str.length()); }
573*38fd1498Szrj    };
574*38fd1498Szrj
575*38fd1498Szrj  template<>
576*38fd1498Szrj    struct __is_fast_hash<hash<string_view>> : std::false_type
577*38fd1498Szrj    { };
578*38fd1498Szrj
579*38fd1498Szrj#ifdef _GLIBCXX_USE_WCHAR_T
580*38fd1498Szrj  template<>
581*38fd1498Szrj    struct hash<wstring_view>
582*38fd1498Szrj    : public __hash_base<size_t, wstring>
583*38fd1498Szrj    {
584*38fd1498Szrj      size_t
585*38fd1498Szrj      operator()(const wstring_view& __s) const noexcept
586*38fd1498Szrj      { return std::_Hash_impl::hash(__s.data(),
587*38fd1498Szrj                                     __s.length() * sizeof(wchar_t)); }
588*38fd1498Szrj    };
589*38fd1498Szrj
590*38fd1498Szrj  template<>
591*38fd1498Szrj    struct __is_fast_hash<hash<wstring_view>> : std::false_type
592*38fd1498Szrj    { };
593*38fd1498Szrj#endif
594*38fd1498Szrj
595*38fd1498Szrj#ifdef _GLIBCXX_USE_C99_STDINT_TR1
596*38fd1498Szrj  template<>
597*38fd1498Szrj    struct hash<u16string_view>
598*38fd1498Szrj    : public __hash_base<size_t, u16string_view>
599*38fd1498Szrj    {
600*38fd1498Szrj      size_t
601*38fd1498Szrj      operator()(const u16string_view& __s) const noexcept
602*38fd1498Szrj      { return std::_Hash_impl::hash(__s.data(),
603*38fd1498Szrj                                     __s.length() * sizeof(char16_t)); }
604*38fd1498Szrj    };
605*38fd1498Szrj
606*38fd1498Szrj  template<>
607*38fd1498Szrj    struct __is_fast_hash<hash<u16string_view>> : std::false_type
608*38fd1498Szrj    { };
609*38fd1498Szrj
610*38fd1498Szrj  template<>
611*38fd1498Szrj    struct hash<u32string_view>
612*38fd1498Szrj    : public __hash_base<size_t, u32string_view>
613*38fd1498Szrj    {
614*38fd1498Szrj      size_t
615*38fd1498Szrj      operator()(const u32string_view& __s) const noexcept
616*38fd1498Szrj      { return std::_Hash_impl::hash(__s.data(),
617*38fd1498Szrj                                     __s.length() * sizeof(char32_t)); }
618*38fd1498Szrj    };
619*38fd1498Szrj
620*38fd1498Szrj  template<>
621*38fd1498Szrj    struct __is_fast_hash<hash<u32string_view>> : std::false_type
622*38fd1498Szrj    { };
623*38fd1498Szrj#endif
624*38fd1498Szrj
625*38fd1498Szrj  inline namespace literals
626*38fd1498Szrj  {
627*38fd1498Szrj  inline namespace string_view_literals
628*38fd1498Szrj  {
629*38fd1498Szrj#pragma GCC diagnostic push
630*38fd1498Szrj#pragma GCC diagnostic ignored "-Wliteral-suffix"
631*38fd1498Szrj    inline constexpr basic_string_view<char>
632*38fd1498Szrj    operator""sv(const char* __str, size_t __len) noexcept
633*38fd1498Szrj    { return basic_string_view<char>{__str, __len}; }
634*38fd1498Szrj
635*38fd1498Szrj#ifdef _GLIBCXX_USE_WCHAR_T
636*38fd1498Szrj    inline constexpr basic_string_view<wchar_t>
637*38fd1498Szrj    operator""sv(const wchar_t* __str, size_t __len) noexcept
638*38fd1498Szrj    { return basic_string_view<wchar_t>{__str, __len}; }
639*38fd1498Szrj#endif
640*38fd1498Szrj
641*38fd1498Szrj#ifdef _GLIBCXX_USE_C99_STDINT_TR1
642*38fd1498Szrj    inline constexpr basic_string_view<char16_t>
643*38fd1498Szrj    operator""sv(const char16_t* __str, size_t __len) noexcept
644*38fd1498Szrj    { return basic_string_view<char16_t>{__str, __len}; }
645*38fd1498Szrj
646*38fd1498Szrj    inline constexpr basic_string_view<char32_t>
647*38fd1498Szrj    operator""sv(const char32_t* __str, size_t __len) noexcept
648*38fd1498Szrj    { return basic_string_view<char32_t>{__str, __len}; }
649*38fd1498Szrj#endif
650*38fd1498Szrj#pragma GCC diagnostic pop
651*38fd1498Szrj  } // namespace string_literals
652*38fd1498Szrj  } // namespace literals
653*38fd1498Szrj
654*38fd1498Szrj_GLIBCXX_END_NAMESPACE_VERSION
655*38fd1498Szrj} // namespace std
656*38fd1498Szrj
657*38fd1498Szrj#include <bits/string_view.tcc>
658*38fd1498Szrj
659*38fd1498Szrj#endif // __cplusplus <= 201402L
660*38fd1498Szrj
661*38fd1498Szrj#endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
662