libstdc++: string_view.tcc Source File

libstdc++

experimental/bits/string_view.tcc
Go to the documentation of this file.
1// Components for manipulating non-owning sequences of characters -*- C++ -*-
2
3// Copyright (C) 2013-2026 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#ifdef _GLIBCXX_SYSHDR
38#pragma GCC system_header
39#endif
40
41#if __cplusplus >= 201402L
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47namespace experimental
48{
49inline namespace fundamentals_v1
50{
51 template<typename _CharT, typename _Traits>
52 constexpr typename basic_string_view<_CharT, _Traits>::size_type
53 basic_string_view<_CharT, _Traits>::
54 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
55 {
56 __glibcxx_requires_string_len(__str, __n);
57
58 if (__n == 0)
59 return __pos <= this->_M_len ? __pos : npos;
60
61 if (__n <= this->_M_len)
62 {
63 for (; __pos <= this->_M_len - __n; ++__pos)
64 if (traits_type::eq(this->_M_str[__pos], __str[0])
65 && traits_type::compare(this->_M_str + __pos + 1,
66 __str + 1, __n - 1) == 0)
67 return __pos;
68 }
69 return npos;
70 }
71
72 template<typename _CharT, typename _Traits>
73 constexpr typename basic_string_view<_CharT, _Traits>::size_type
74 basic_string_view<_CharT, _Traits>::
75 find(_CharT __c, size_type __pos) const noexcept
76 {
77 size_type __ret = npos;
78 if (__pos < this->_M_len)
79 {
80 const size_type __n = this->_M_len - __pos;
81 const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
82 if (__p)
83 __ret = __p - this->_M_str;
84 }
85 return __ret;
86 }
87
88 template<typename _CharT, typename _Traits>
89 constexpr typename basic_string_view<_CharT, _Traits>::size_type
90 basic_string_view<_CharT, _Traits>::
91 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
92 {
93 __glibcxx_requires_string_len(__str, __n);
94
95 if (__n <= this->_M_len)
96 {
97 __pos = std::min(size_type(this->_M_len - __n), __pos);
98 do
99 {
100 if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
101 return __pos;
102 }
103 while (__pos-- > 0);
104 }
105 return npos;
106 }
107
108 template<typename _CharT, typename _Traits>
109 constexpr typename basic_string_view<_CharT, _Traits>::size_type
110 basic_string_view<_CharT, _Traits>::
111 rfind(_CharT __c, size_type __pos) const noexcept
112 {
113 size_type __size = this->_M_len;
114 if (__size > 0)
115 {
116 if (--__size > __pos)
117 __size = __pos;
118 for (++__size; __size-- > 0; )
119 if (traits_type::eq(this->_M_str[__size], __c))
120 return __size;
121 }
122 return npos;
123 }
124
125 template<typename _CharT, typename _Traits>
126 constexpr typename basic_string_view<_CharT, _Traits>::size_type
127 basic_string_view<_CharT, _Traits>::
128 find_first_of(const _CharT* __str, size_type __pos, size_type __n) const
129 {
130 __glibcxx_requires_string_len(__str, __n);
131 for (; __n && __pos < this->_M_len; ++__pos)
132 {
133 const _CharT* __p = traits_type::find(__str, __n,
134 this->_M_str[__pos]);
135 if (__p)
136 return __pos;
137 }
138 return npos;
139 }
140
141 template<typename _CharT, typename _Traits>
142 constexpr typename basic_string_view<_CharT, _Traits>::size_type
143 basic_string_view<_CharT, _Traits>::
144 find_last_of(const _CharT* __str, size_type __pos, size_type __n) const
145 {
146 __glibcxx_requires_string_len(__str, __n);
147 size_type __size = this->size();
148 if (__size && __n)
149 {
150 if (--__size > __pos)
151 __size = __pos;
152 do
153 {
154 if (traits_type::find(__str, __n, this->_M_str[__size]))
155 return __size;
156 }
157 while (__size-- != 0);
158 }
159 return npos;
160 }
161
162 template<typename _CharT, typename _Traits>
163 constexpr typename basic_string_view<_CharT, _Traits>::size_type
164 basic_string_view<_CharT, _Traits>::
165 find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const
166 {
167 __glibcxx_requires_string_len(__str, __n);
168 for (; __pos < this->_M_len; ++__pos)
169 if (!traits_type::find(__str, __n, this->_M_str[__pos]))
170 return __pos;
171 return npos;
172 }
173
174 template<typename _CharT, typename _Traits>
175 constexpr typename basic_string_view<_CharT, _Traits>::size_type
176 basic_string_view<_CharT, _Traits>::
177 find_first_not_of(_CharT __c, size_type __pos) const noexcept
178 {
179 for (; __pos < this->_M_len; ++__pos)
180 if (!traits_type::eq(this->_M_str[__pos], __c))
181 return __pos;
182 return npos;
183 }
184
185 template<typename _CharT, typename _Traits>
186 constexpr typename basic_string_view<_CharT, _Traits>::size_type
187 basic_string_view<_CharT, _Traits>::
188 find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const
189 {
190 __glibcxx_requires_string_len(__str, __n);
191 size_type __size = this->_M_len;
192 if (__size)
193 {
194 if (--__size > __pos)
195 __size = __pos;
196 do
197 {
198 if (!traits_type::find(__str, __n, this->_M_str[__size]))
199 return __size;
200 }
201 while (__size--);
202 }
203 return npos;
204 }
205
206 template<typename _CharT, typename _Traits>
207 constexpr typename basic_string_view<_CharT, _Traits>::size_type
208 basic_string_view<_CharT, _Traits>::
209 find_last_not_of(_CharT __c, size_type __pos) const noexcept
210 {
211 size_type __size = this->_M_len;
212 if (__size)
213 {
214 if (--__size > __pos)
215 __size = __pos;
216 do
217 {
218 if (!traits_type::eq(this->_M_str[__size], __c))
219 return __size;
220 }
221 while (__size--);
222 }
223 return npos;
224 }
225} // namespace fundamentals_v1
226} // namespace experimental
227
228_GLIBCXX_END_NAMESPACE_VERSION
229} // namespace std
230
231#endif // __cplusplus <= 201103L
232
233#endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC
std::min
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:232
std
ISO C++ entities toplevel namespace is std.
std::size
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition range_access.h:294
std::experimental
Namespace for features defined in ISO Technical Specifications.
std::experimental::fundamentals_v1::basic_string_view
A non-owning reference to a string.
Definition experimental/string_view:81