xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/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 include/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{string_view}
28*38fd1498Szrj  */
29*38fd1498Szrj 
30*38fd1498Szrj //
31*38fd1498Szrj // N3762 basic_string_view library
32*38fd1498Szrj //
33*38fd1498Szrj 
34*38fd1498Szrj #ifndef _GLIBCXX_STRING_VIEW_TCC
35*38fd1498Szrj #define _GLIBCXX_STRING_VIEW_TCC 1
36*38fd1498Szrj 
37*38fd1498Szrj #pragma GCC system_header
38*38fd1498Szrj 
39*38fd1498Szrj #if __cplusplus >= 201703L
40*38fd1498Szrj 
41*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
42*38fd1498Szrj {
43*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
44*38fd1498Szrj 
45*38fd1498Szrj   template<typename _CharT, typename _Traits>
46*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
47*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find(const _CharT * __str,size_type __pos,size_type __n) const48*38fd1498Szrj     find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
49*38fd1498Szrj     {
50*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
51*38fd1498Szrj 
52*38fd1498Szrj       if (__n == 0)
53*38fd1498Szrj 	return __pos <= this->_M_len ? __pos : npos;
54*38fd1498Szrj 
55*38fd1498Szrj       if (__n <= this->_M_len)
56*38fd1498Szrj 	{
57*38fd1498Szrj 	  for (; __pos <= this->_M_len - __n; ++__pos)
58*38fd1498Szrj 	    if (traits_type::eq(this->_M_str[__pos], __str[0])
59*38fd1498Szrj 		&& traits_type::compare(this->_M_str + __pos + 1,
60*38fd1498Szrj 					__str + 1, __n - 1) == 0)
61*38fd1498Szrj 	      return __pos;
62*38fd1498Szrj 	}
63*38fd1498Szrj       return npos;
64*38fd1498Szrj     }
65*38fd1498Szrj 
66*38fd1498Szrj   template<typename _CharT, typename _Traits>
67*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
68*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find(_CharT __c,size_type __pos) const69*38fd1498Szrj     find(_CharT __c, size_type __pos) const noexcept
70*38fd1498Szrj     {
71*38fd1498Szrj       size_type __ret = npos;
72*38fd1498Szrj       if (__pos < this->_M_len)
73*38fd1498Szrj 	{
74*38fd1498Szrj 	  const size_type __n = this->_M_len - __pos;
75*38fd1498Szrj 	  const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
76*38fd1498Szrj 	  if (__p)
77*38fd1498Szrj 	    __ret = __p - this->_M_str;
78*38fd1498Szrj 	}
79*38fd1498Szrj       return __ret;
80*38fd1498Szrj     }
81*38fd1498Szrj 
82*38fd1498Szrj   template<typename _CharT, typename _Traits>
83*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
84*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
rfind(const _CharT * __str,size_type __pos,size_type __n) const85*38fd1498Szrj     rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
86*38fd1498Szrj     {
87*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
88*38fd1498Szrj 
89*38fd1498Szrj       if (__n <= this->_M_len)
90*38fd1498Szrj 	{
91*38fd1498Szrj 	  __pos = std::min(size_type(this->_M_len - __n), __pos);
92*38fd1498Szrj 	  do
93*38fd1498Szrj 	    {
94*38fd1498Szrj 	      if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
95*38fd1498Szrj 		return __pos;
96*38fd1498Szrj 	    }
97*38fd1498Szrj 	  while (__pos-- > 0);
98*38fd1498Szrj 	}
99*38fd1498Szrj       return npos;
100*38fd1498Szrj     }
101*38fd1498Szrj 
102*38fd1498Szrj   template<typename _CharT, typename _Traits>
103*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
104*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
rfind(_CharT __c,size_type __pos) const105*38fd1498Szrj     rfind(_CharT __c, size_type __pos) const noexcept
106*38fd1498Szrj     {
107*38fd1498Szrj       size_type __size = this->_M_len;
108*38fd1498Szrj       if (__size > 0)
109*38fd1498Szrj 	{
110*38fd1498Szrj 	  if (--__size > __pos)
111*38fd1498Szrj 	    __size = __pos;
112*38fd1498Szrj 	  for (++__size; __size-- > 0; )
113*38fd1498Szrj 	    if (traits_type::eq(this->_M_str[__size], __c))
114*38fd1498Szrj 	      return __size;
115*38fd1498Szrj 	}
116*38fd1498Szrj       return npos;
117*38fd1498Szrj     }
118*38fd1498Szrj 
119*38fd1498Szrj   template<typename _CharT, typename _Traits>
120*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
121*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_first_of(const _CharT * __str,size_type __pos,size_type __n) const122*38fd1498Szrj     find_first_of(const _CharT* __str, size_type __pos,
123*38fd1498Szrj 		  size_type __n) const noexcept
124*38fd1498Szrj     {
125*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
126*38fd1498Szrj       for (; __n && __pos < this->_M_len; ++__pos)
127*38fd1498Szrj 	{
128*38fd1498Szrj 	  const _CharT* __p = traits_type::find(__str, __n,
129*38fd1498Szrj 						this->_M_str[__pos]);
130*38fd1498Szrj 	  if (__p)
131*38fd1498Szrj 	    return __pos;
132*38fd1498Szrj 	}
133*38fd1498Szrj       return npos;
134*38fd1498Szrj     }
135*38fd1498Szrj 
136*38fd1498Szrj   template<typename _CharT, typename _Traits>
137*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
138*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_last_of(const _CharT * __str,size_type __pos,size_type __n) const139*38fd1498Szrj     find_last_of(const _CharT* __str, size_type __pos,
140*38fd1498Szrj 		 size_type __n) const noexcept
141*38fd1498Szrj     {
142*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
143*38fd1498Szrj       size_type __size = this->size();
144*38fd1498Szrj       if (__size && __n)
145*38fd1498Szrj 	{
146*38fd1498Szrj 	  if (--__size > __pos)
147*38fd1498Szrj 	    __size = __pos;
148*38fd1498Szrj 	  do
149*38fd1498Szrj 	    {
150*38fd1498Szrj 	      if (traits_type::find(__str, __n, this->_M_str[__size]))
151*38fd1498Szrj 		return __size;
152*38fd1498Szrj 	    }
153*38fd1498Szrj 	  while (__size-- != 0);
154*38fd1498Szrj 	}
155*38fd1498Szrj       return npos;
156*38fd1498Szrj     }
157*38fd1498Szrj 
158*38fd1498Szrj   template<typename _CharT, typename _Traits>
159*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
160*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_first_not_of(const _CharT * __str,size_type __pos,size_type __n) const161*38fd1498Szrj     find_first_not_of(const _CharT* __str, size_type __pos,
162*38fd1498Szrj 		      size_type __n) const noexcept
163*38fd1498Szrj     {
164*38fd1498Szrj       __glibcxx_requires_string_len(__str, __n);
165*38fd1498Szrj       for (; __pos < this->_M_len; ++__pos)
166*38fd1498Szrj 	if (!traits_type::find(__str, __n, this->_M_str[__pos]))
167*38fd1498Szrj 	  return __pos;
168*38fd1498Szrj       return npos;
169*38fd1498Szrj     }
170*38fd1498Szrj 
171*38fd1498Szrj   template<typename _CharT, typename _Traits>
172*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
173*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_first_not_of(_CharT __c,size_type __pos) const174*38fd1498Szrj     find_first_not_of(_CharT __c, size_type __pos) const noexcept
175*38fd1498Szrj     {
176*38fd1498Szrj       for (; __pos < this->_M_len; ++__pos)
177*38fd1498Szrj 	if (!traits_type::eq(this->_M_str[__pos], __c))
178*38fd1498Szrj 	  return __pos;
179*38fd1498Szrj       return npos;
180*38fd1498Szrj     }
181*38fd1498Szrj 
182*38fd1498Szrj   template<typename _CharT, typename _Traits>
183*38fd1498Szrj     constexpr typename basic_string_view<_CharT, _Traits>::size_type
184*38fd1498Szrj     basic_string_view<_CharT, _Traits>::
find_last_not_of(const _CharT * __str,size_type __pos,size_type __n) const185*38fd1498Szrj     find_last_not_of(const _CharT* __str, size_type __pos,
186*38fd1498Szrj 		     size_type __n) const noexcept
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 
224*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
225*38fd1498Szrj } // namespace std
226*38fd1498Szrj 
227*38fd1498Szrj #endif // __cplusplus <= 201402L
228*38fd1498Szrj 
229*38fd1498Szrj #endif // _GLIBCXX_STRING_VIEW_TCC
230