xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/experimental/bits/string_view.tcc (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 experimental/bits/string_view.tcc
26*38fd1498Szrj  *  This is an internal header file, included by other library headers.
27*38fd1498Szrj  *  Do not attempt to use it directly. @headername{experimental/string_view}
28*38fd1498Szrj  */
29*38fd1498Szrj 
30*38fd1498Szrj //
31*38fd1498Szrj // N3762 basic_string_view library
32*38fd1498Szrj //
33*38fd1498Szrj 
34*38fd1498Szrj #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC
35*38fd1498Szrj #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC 1
36*38fd1498Szrj 
37*38fd1498Szrj #pragma GCC system_header
38*38fd1498Szrj 
39*38fd1498Szrj #if __cplusplus >= 201402L
40*38fd1498Szrj 
41*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
42*38fd1498Szrj {
43*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
44*38fd1498Szrj 
45*38fd1498Szrj namespace experimental
46*38fd1498Szrj {
47*38fd1498Szrj inline namespace fundamentals_v1
48*38fd1498Szrj {
49*38fd1498Szrj   template<typename _CharT, typename _Traits>
50*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
51*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find(const _CharT * __str,size_type __pos,size_type __n) const52*38fd1498Szrj     find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
53*38fd1498Szrj     {
54*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
55*38fd1498Szrj 
56*38fd1498Szrj       if (__n == 0)
57*38fd1498Szrj 	return __pos <= this->_M_len ? __pos : npos;
58*38fd1498Szrj 
59*38fd1498Szrj       if (__n <= this->_M_len)
60*38fd1498Szrj 	{
61*38fd1498Szrj 	  for (; __pos <= this->_M_len - __n; ++__pos)
62*38fd1498Szrj 	    if (traits_type::eq(this->_M_str[__pos], __str[0])
63*38fd1498Szrj 		&& traits_type::compare(this->_M_str + __pos + 1,
64*38fd1498Szrj 					__str + 1, __n - 1) == 0)
65*38fd1498Szrj 	      return __pos;
66*38fd1498Szrj 	}
67*38fd1498Szrj       return npos;
68*38fd1498Szrj     }
69*38fd1498Szrj 
70*38fd1498Szrj   template<typename _CharT, typename _Traits>
71*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
72*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find(_CharT __c,size_type __pos) const73*38fd1498Szrj     find(_CharT __c, size_type __pos) const noexcept
74*38fd1498Szrj     {
75*38fd1498Szrj       size_type __ret = npos;
76*38fd1498Szrj       if (__pos < this->_M_len)
77*38fd1498Szrj 	{
78*38fd1498Szrj 	  const size_type __n = this->_M_len - __pos;
79*38fd1498Szrj 	  const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
80*38fd1498Szrj 	  if (__p)
81*38fd1498Szrj 	    __ret = __p - this->_M_str;
82*38fd1498Szrj 	}
83*38fd1498Szrj       return __ret;
84*38fd1498Szrj     }
85*38fd1498Szrj 
86*38fd1498Szrj   template<typename _CharT, typename _Traits>
87*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
88*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
rfind(const _CharT * __str,size_type __pos,size_type __n) const89*38fd1498Szrj     rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
90*38fd1498Szrj     {
91*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
92*38fd1498Szrj 
93*38fd1498Szrj       if (__n <= this->_M_len)
94*38fd1498Szrj 	{
95*38fd1498Szrj 	  __pos = std::min(size_type(this->_M_len - __n), __pos);
96*38fd1498Szrj 	  do
97*38fd1498Szrj 	    {
98*38fd1498Szrj 	      if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
99*38fd1498Szrj 		return __pos;
100*38fd1498Szrj 	    }
101*38fd1498Szrj 	  while (__pos-- > 0);
102*38fd1498Szrj 	}
103*38fd1498Szrj       return npos;
104*38fd1498Szrj     }
105*38fd1498Szrj 
106*38fd1498Szrj   template<typename _CharT, typename _Traits>
107*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
108*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
rfind(_CharT __c,size_type __pos) const109*38fd1498Szrj     rfind(_CharT __c, size_type __pos) const noexcept
110*38fd1498Szrj     {
111*38fd1498Szrj       size_type __size = this->_M_len;
112*38fd1498Szrj       if (__size > 0)
113*38fd1498Szrj 	{
114*38fd1498Szrj 	  if (--__size > __pos)
115*38fd1498Szrj 	    __size = __pos;
116*38fd1498Szrj 	  for (++__size; __size-- > 0; )
117*38fd1498Szrj 	    if (traits_type::eq(this->_M_str[__size], __c))
118*38fd1498Szrj 	      return __size;
119*38fd1498Szrj 	}
120*38fd1498Szrj       return npos;
121*38fd1498Szrj     }
122*38fd1498Szrj 
123*38fd1498Szrj   template<typename _CharT, typename _Traits>
124*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
125*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_first_of(const _CharT * __str,size_type __pos,size_type __n) const126*38fd1498Szrj     find_first_of(const _CharT* __str, size_type __pos, size_type __n) const
127*38fd1498Szrj     {
128*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
129*38fd1498Szrj       for (; __n && __pos < this->_M_len; ++__pos)
130*38fd1498Szrj 	{
131*38fd1498Szrj 	  const _CharT* __p = traits_type::find(__str, __n,
132*38fd1498Szrj 						this->_M_str[__pos]);
133*38fd1498Szrj 	  if (__p)
134*38fd1498Szrj 	    return __pos;
135*38fd1498Szrj 	}
136*38fd1498Szrj       return npos;
137*38fd1498Szrj     }
138*38fd1498Szrj 
139*38fd1498Szrj   template<typename _CharT, typename _Traits>
140*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
141*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_last_of(const _CharT * __str,size_type __pos,size_type __n) const142*38fd1498Szrj     find_last_of(const _CharT* __str, size_type __pos, size_type __n) const
143*38fd1498Szrj     {
144*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
145*38fd1498Szrj       size_type __size = this->size();
146*38fd1498Szrj       if (__size && __n)
147*38fd1498Szrj 	{
148*38fd1498Szrj 	  if (--__size > __pos)
149*38fd1498Szrj 	    __size = __pos;
150*38fd1498Szrj 	  do
151*38fd1498Szrj 	    {
152*38fd1498Szrj 	      if (traits_type::find(__str, __n, this->_M_str[__size]))
153*38fd1498Szrj 		return __size;
154*38fd1498Szrj 	    }
155*38fd1498Szrj 	  while (__size-- != 0);
156*38fd1498Szrj 	}
157*38fd1498Szrj       return npos;
158*38fd1498Szrj     }
159*38fd1498Szrj 
160*38fd1498Szrj   template<typename _CharT, typename _Traits>
161*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
162*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_first_not_of(const _CharT * __str,size_type __pos,size_type __n) const163*38fd1498Szrj     find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const
164*38fd1498Szrj     {
165*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
166*38fd1498Szrj       for (; __pos < this->_M_len; ++__pos)
167*38fd1498Szrj 	if (!traits_type::find(__str, __n, this->_M_str[__pos]))
168*38fd1498Szrj 	  return __pos;
169*38fd1498Szrj       return npos;
170*38fd1498Szrj     }
171*38fd1498Szrj 
172*38fd1498Szrj   template<typename _CharT, typename _Traits>
173*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
174*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_first_not_of(_CharT __c,size_type __pos) const175*38fd1498Szrj     find_first_not_of(_CharT __c, size_type __pos) const noexcept
176*38fd1498Szrj     {
177*38fd1498Szrj       for (; __pos < this->_M_len; ++__pos)
178*38fd1498Szrj 	if (!traits_type::eq(this->_M_str[__pos], __c))
179*38fd1498Szrj 	  return __pos;
180*38fd1498Szrj       return npos;
181*38fd1498Szrj     }
182*38fd1498Szrj 
183*38fd1498Szrj   template<typename _CharT, typename _Traits>
184*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
185*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_last_not_of(const _CharT * __str,size_type __pos,size_type __n) const186*38fd1498Szrj     find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const
187*38fd1498Szrj     {
188*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
189*38fd1498Szrj       size_type __size = this->_M_len;
190*38fd1498Szrj       if (__size)
191*38fd1498Szrj 	{
192*38fd1498Szrj 	  if (--__size > __pos)
193*38fd1498Szrj 	    __size = __pos;
194*38fd1498Szrj 	  do
195*38fd1498Szrj 	    {
196*38fd1498Szrj 	      if (!traits_type::find(__str, __n, this->_M_str[__size]))
197*38fd1498Szrj 		return __size;
198*38fd1498Szrj 	    }
199*38fd1498Szrj 	  while (__size--);
200*38fd1498Szrj 	}
201*38fd1498Szrj       return npos;
202*38fd1498Szrj     }
203*38fd1498Szrj 
204*38fd1498Szrj   template<typename _CharT, typename _Traits>
205*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
206*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_last_not_of(_CharT __c,size_type __pos) const207*38fd1498Szrj     find_last_not_of(_CharT __c, size_type __pos) const noexcept
208*38fd1498Szrj     {
209*38fd1498Szrj       size_type __size = this->_M_len;
210*38fd1498Szrj       if (__size)
211*38fd1498Szrj 	{
212*38fd1498Szrj 	  if (--__size > __pos)
213*38fd1498Szrj 	    __size = __pos;
214*38fd1498Szrj 	  do
215*38fd1498Szrj 	    {
216*38fd1498Szrj 	      if (!traits_type::eq(this->_M_str[__size], __c))
217*38fd1498Szrj 		return __size;
218*38fd1498Szrj 	    }
219*38fd1498Szrj 	  while (__size--);
220*38fd1498Szrj 	}
221*38fd1498Szrj       return npos;
222*38fd1498Szrj     }
223*38fd1498Szrj } // namespace fundamentals_v1
224*38fd1498Szrj } // namespace experimental
225*38fd1498Szrj 
226*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
227*38fd1498Szrj } // namespace std
228*38fd1498Szrj 
229*38fd1498Szrj #endif // __cplusplus <= 201103L
230*38fd1498Szrj 
231*38fd1498Szrj #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC
232