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