]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/string_view.tcc
b8ab78cedf1781c2184fcf53bc1429c204ffa078
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / string_view.tcc
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 include/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 <= 201402L
40 # include <bits/c++17_warning.h>
41 #else
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 template<typename _CharT, typename _Traits>
48 constexpr typename basic_string_view<_CharT, _Traits>::size_type
49 basic_string_view<_CharT, _Traits>::
50 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
51 {
52 __glibcxx_requires_string_len(__str, __n);
53
54 if (__n == 0)
55 return __pos <= this->_M_len ? __pos : npos;
56
57 if (__n <= this->_M_len)
58 {
59 for (; __pos <= this->_M_len - __n; ++__pos)
60 if (traits_type::eq(this->_M_str[__pos], __str[0])
61 && traits_type::compare(this->_M_str + __pos + 1,
62 __str + 1, __n - 1) == 0)
63 return __pos;
64 }
65 return npos;
66 }
67
68 template<typename _CharT, typename _Traits>
69 constexpr typename basic_string_view<_CharT, _Traits>::size_type
70 basic_string_view<_CharT, _Traits>::
71 find(_CharT __c, size_type __pos) const noexcept
72 {
73 size_type __ret = npos;
74 if (__pos < this->_M_len)
75 {
76 const size_type __n = this->_M_len - __pos;
77 const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
78 if (__p)
79 __ret = __p - this->_M_str;
80 }
81 return __ret;
82 }
83
84 template<typename _CharT, typename _Traits>
85 constexpr typename basic_string_view<_CharT, _Traits>::size_type
86 basic_string_view<_CharT, _Traits>::
87 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
88 {
89 __glibcxx_requires_string_len(__str, __n);
90
91 if (__n <= this->_M_len)
92 {
93 __pos = std::min(size_type(this->_M_len - __n), __pos);
94 do
95 {
96 if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
97 return __pos;
98 }
99 while (__pos-- > 0);
100 }
101 return npos;
102 }
103
104 template<typename _CharT, typename _Traits>
105 constexpr typename basic_string_view<_CharT, _Traits>::size_type
106 basic_string_view<_CharT, _Traits>::
107 rfind(_CharT __c, size_type __pos) const noexcept
108 {
109 size_type __size = this->_M_len;
110 if (__size > 0)
111 {
112 if (--__size > __pos)
113 __size = __pos;
114 for (++__size; __size-- > 0; )
115 if (traits_type::eq(this->_M_str[__size], __c))
116 return __size;
117 }
118 return npos;
119 }
120
121 template<typename _CharT, typename _Traits>
122 constexpr typename basic_string_view<_CharT, _Traits>::size_type
123 basic_string_view<_CharT, _Traits>::
124 find_first_of(const _CharT* __str, size_type __pos, size_type __n) const
125 {
126 __glibcxx_requires_string_len(__str, __n);
127 for (; __n && __pos < this->_M_len; ++__pos)
128 {
129 const _CharT* __p = traits_type::find(__str, __n,
130 this->_M_str[__pos]);
131 if (__p)
132 return __pos;
133 }
134 return npos;
135 }
136
137 template<typename _CharT, typename _Traits>
138 constexpr typename basic_string_view<_CharT, _Traits>::size_type
139 basic_string_view<_CharT, _Traits>::
140 find_last_of(const _CharT* __str, size_type __pos, size_type __n) const
141 {
142 __glibcxx_requires_string_len(__str, __n);
143 size_type __size = this->size();
144 if (__size && __n)
145 {
146 if (--__size > __pos)
147 __size = __pos;
148 do
149 {
150 if (traits_type::find(__str, __n, this->_M_str[__size]))
151 return __size;
152 }
153 while (__size-- != 0);
154 }
155 return npos;
156 }
157
158 template<typename _CharT, typename _Traits>
159 constexpr typename basic_string_view<_CharT, _Traits>::size_type
160 basic_string_view<_CharT, _Traits>::
161 find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const
162 {
163 __glibcxx_requires_string_len(__str, __n);
164 for (; __pos < this->_M_len; ++__pos)
165 if (!traits_type::find(__str, __n, this->_M_str[__pos]))
166 return __pos;
167 return npos;
168 }
169
170 template<typename _CharT, typename _Traits>
171 constexpr typename basic_string_view<_CharT, _Traits>::size_type
172 basic_string_view<_CharT, _Traits>::
173 find_first_not_of(_CharT __c, size_type __pos) const noexcept
174 {
175 for (; __pos < this->_M_len; ++__pos)
176 if (!traits_type::eq(this->_M_str[__pos], __c))
177 return __pos;
178 return npos;
179 }
180
181 template<typename _CharT, typename _Traits>
182 constexpr typename basic_string_view<_CharT, _Traits>::size_type
183 basic_string_view<_CharT, _Traits>::
184 find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const
185 {
186 __glibcxx_requires_string_len(__str, __n);
187 size_type __size = this->_M_len;
188 if (__size)
189 {
190 if (--__size > __pos)
191 __size = __pos;
192 do
193 {
194 if (!traits_type::find(__str, __n, this->_M_str[__size]))
195 return __size;
196 }
197 while (__size--);
198 }
199 return npos;
200 }
201
202 template<typename _CharT, typename _Traits>
203 constexpr typename basic_string_view<_CharT, _Traits>::size_type
204 basic_string_view<_CharT, _Traits>::
205 find_last_not_of(_CharT __c, size_type __pos) const noexcept
206 {
207 size_type __size = this->_M_len;
208 if (__size)
209 {
210 if (--__size > __pos)
211 __size = __pos;
212 do
213 {
214 if (!traits_type::eq(this->_M_str[__size], __c))
215 return __size;
216 }
217 while (__size--);
218 }
219 return npos;
220 }
221
222 _GLIBCXX_END_NAMESPACE_VERSION
223 } // namespace std
224
225 #endif // __cplusplus <= 201402L
226
227 #endif // _GLIBCXX_STRING_VIEW_TCC