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