1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013-2022 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
31 // N3762 basic_string_view library
34 #ifndef _GLIBCXX_STRING_VIEW_TCC
35 #define _GLIBCXX_STRING_VIEW_TCC 1
37 #pragma GCC system_header
39 #if __cplusplus >= 201703L
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 template<typename _CharT, typename _Traits>
46 constexpr typename basic_string_view<_CharT, _Traits>::size_type
47 basic_string_view<_CharT, _Traits>::
48 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
50 __glibcxx_requires_string_len(__str, __n);
53 return __pos <= _M_len ? __pos : npos;
57 const _CharT __elem0 = __str[0];
58 const _CharT* __first = _M_str + __pos;
59 const _CharT* const __last = _M_str + _M_len;
60 size_type __len = _M_len - __pos;
64 // Find the first occurrence of __elem0:
65 __first = traits_type::find(__first, __len - __n + 1, __elem0);
68 // Compare the full strings from the first occurrence of __elem0.
69 // We already know that __first[0] == __s[0] but compare them again
70 // anyway because __s is probably aligned, which helps memcmp.
71 if (traits_type::compare(__first, __str, __n) == 0)
72 return __first - _M_str;
73 __len = __last - ++__first;
78 template<typename _CharT, typename _Traits>
79 constexpr typename basic_string_view<_CharT, _Traits>::size_type
80 basic_string_view<_CharT, _Traits>::
81 find(_CharT __c, size_type __pos) const noexcept
83 size_type __ret = npos;
84 if (__pos < this->_M_len)
86 const size_type __n = this->_M_len - __pos;
87 const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
89 __ret = __p - this->_M_str;
94 template<typename _CharT, typename _Traits>
95 constexpr typename basic_string_view<_CharT, _Traits>::size_type
96 basic_string_view<_CharT, _Traits>::
97 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
99 __glibcxx_requires_string_len(__str, __n);
101 if (__n <= this->_M_len)
103 __pos = std::min(size_type(this->_M_len - __n), __pos);
106 if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
114 template<typename _CharT, typename _Traits>
115 constexpr typename basic_string_view<_CharT, _Traits>::size_type
116 basic_string_view<_CharT, _Traits>::
117 rfind(_CharT __c, size_type __pos) const noexcept
119 size_type __size = this->_M_len;
122 if (--__size > __pos)
124 for (++__size; __size-- > 0; )
125 if (traits_type::eq(this->_M_str[__size], __c))
131 template<typename _CharT, typename _Traits>
132 constexpr typename basic_string_view<_CharT, _Traits>::size_type
133 basic_string_view<_CharT, _Traits>::
134 find_first_of(const _CharT* __str, size_type __pos,
135 size_type __n) const noexcept
137 __glibcxx_requires_string_len(__str, __n);
138 for (; __n && __pos < this->_M_len; ++__pos)
140 const _CharT* __p = traits_type::find(__str, __n,
141 this->_M_str[__pos]);
148 template<typename _CharT, typename _Traits>
149 constexpr typename basic_string_view<_CharT, _Traits>::size_type
150 basic_string_view<_CharT, _Traits>::
151 find_last_of(const _CharT* __str, size_type __pos,
152 size_type __n) const noexcept
154 __glibcxx_requires_string_len(__str, __n);
155 size_type __size = this->size();
158 if (--__size > __pos)
162 if (traits_type::find(__str, __n, this->_M_str[__size]))
165 while (__size-- != 0);
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(const _CharT* __str, size_type __pos,
174 size_type __n) const noexcept
176 __glibcxx_requires_string_len(__str, __n);
177 for (; __pos < this->_M_len; ++__pos)
178 if (!traits_type::find(__str, __n, this->_M_str[__pos]))
183 template<typename _CharT, typename _Traits>
184 constexpr typename basic_string_view<_CharT, _Traits>::size_type
185 basic_string_view<_CharT, _Traits>::
186 find_first_not_of(_CharT __c, size_type __pos) const noexcept
188 for (; __pos < this->_M_len; ++__pos)
189 if (!traits_type::eq(this->_M_str[__pos], __c))
194 template<typename _CharT, typename _Traits>
195 constexpr typename basic_string_view<_CharT, _Traits>::size_type
196 basic_string_view<_CharT, _Traits>::
197 find_last_not_of(const _CharT* __str, size_type __pos,
198 size_type __n) const noexcept
200 __glibcxx_requires_string_len(__str, __n);
201 size_type __size = this->_M_len;
204 if (--__size > __pos)
208 if (!traits_type::find(__str, __n, this->_M_str[__size]))
216 template<typename _CharT, typename _Traits>
217 constexpr typename basic_string_view<_CharT, _Traits>::size_type
218 basic_string_view<_CharT, _Traits>::
219 find_last_not_of(_CharT __c, size_type __pos) const noexcept
221 size_type __size = this->_M_len;
224 if (--__size > __pos)
228 if (!traits_type::eq(this->_M_str[__size], __c))
236 _GLIBCXX_END_NAMESPACE_VERSION
239 #endif // __cplusplus <= 201402L
241 #endif // _GLIBCXX_STRING_VIEW_TCC