Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/bits/string_view.tcc
1 // Components for manipulating non-owning sequences of characters -*- C++ -*- 2 3 // Copyright (C) 2013-2025 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 #ifdef _GLIBCXX_SYSHDR 38 #pragma GCC system_header 39 #endif 40 41 #if __cplusplus >= 201703L 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 <= _M_len ? __pos : npos; 56 if (__pos >= _M_len) 57 return npos; 58 59 const _CharT __elem0 = __str[0]; 60 const _CharT* __first = _M_str + __pos; 61 const _CharT* const __last = _M_str + _M_len; 62 size_type __len = _M_len - __pos; 63 64 while (__len >= __n) 65 { 66 // Find the first occurrence of __elem0: 67 __first = traits_type::find(__first, __len - __n + 1, __elem0); 68 if (!__first) 69 return npos; 70 // Compare the full strings from the first occurrence of __elem0. 71 // We already know that __first[0] == __s[0] but compare them again 72 // anyway because __s is probably aligned, which helps memcmp. 73 if (traits_type::compare(__first, __str, __n) == 0) 74 return __first - _M_str; 75 __len = __last - ++__first; 76 } 77 return npos; 78 } 79 80 template<typename _CharT, typename _Traits> 81 constexpr typename basic_string_view<_CharT, _Traits>::size_type 82 basic_string_view<_CharT, _Traits>:: 83 find(_CharT __c, size_type __pos) const noexcept 84 { 85 size_type __ret = npos; 86 if (__pos < this->_M_len) 87 { 88 const size_type __n = this->_M_len - __pos; 89 const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c); 90 if (__p) 91 __ret = __p - this->_M_str; 92 } 93 return __ret; 94 } 95 96 template<typename _CharT, typename _Traits> 97 constexpr typename basic_string_view<_CharT, _Traits>::size_type 98 basic_string_view<_CharT, _Traits>:: 99 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept 100 { 101 __glibcxx_requires_string_len(__str, __n); 102 103 if (__n <= this->_M_len) 104 { 105 __pos = std::min(size_type(this->_M_len - __n), __pos); 106 do 107 { 108 if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0) 109 return __pos; 110 } 111 while (__pos-- > 0); 112 } 113 return npos; 114 } 115 116 template<typename _CharT, typename _Traits> 117 constexpr typename basic_string_view<_CharT, _Traits>::size_type 118 basic_string_view<_CharT, _Traits>:: 119 rfind(_CharT __c, size_type __pos) const noexcept 120 { 121 size_type __size = this->_M_len; 122 if (__size > 0) 123 { 124 if (--__size > __pos) 125 __size = __pos; 126 for (++__size; __size-- > 0; ) 127 if (traits_type::eq(this->_M_str[__size], __c)) 128 return __size; 129 } 130 return npos; 131 } 132 133 template<typename _CharT, typename _Traits> 134 constexpr typename basic_string_view<_CharT, _Traits>::size_type 135 basic_string_view<_CharT, _Traits>:: 136 find_first_of(const _CharT* __str, size_type __pos, 137 size_type __n) const noexcept 138 { 139 __glibcxx_requires_string_len(__str, __n); 140 for (; __n && __pos < this->_M_len; ++__pos) 141 { 142 const _CharT* __p = traits_type::find(__str, __n, 143 this->_M_str[__pos]); 144 if (__p) 145 return __pos; 146 } 147 return npos; 148 } 149 150 template<typename _CharT, typename _Traits> 151 constexpr typename basic_string_view<_CharT, _Traits>::size_type 152 basic_string_view<_CharT, _Traits>:: 153 find_last_of(const _CharT* __str, size_type __pos, 154 size_type __n) const noexcept 155 { 156 __glibcxx_requires_string_len(__str, __n); 157 size_type __size = this->size(); 158 if (__size && __n) 159 { 160 if (--__size > __pos) 161 __size = __pos; 162 do 163 { 164 if (traits_type::find(__str, __n, this->_M_str[__size])) 165 return __size; 166 } 167 while (__size-- != 0); 168 } 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>:: 175 find_first_not_of(const _CharT* __str, size_type __pos, 176 size_type __n) const noexcept 177 { 178 __glibcxx_requires_string_len(__str, __n); 179 for (; __pos < this->_M_len; ++__pos) 180 if (!traits_type::find(__str, __n, this->_M_str[__pos])) 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_first_not_of(_CharT __c, size_type __pos) const noexcept 189 { 190 for (; __pos < this->_M_len; ++__pos) 191 if (!traits_type::eq(this->_M_str[__pos], __c)) 192 return __pos; 193 return npos; 194 } 195 196 template<typename _CharT, typename _Traits> 197 constexpr typename basic_string_view<_CharT, _Traits>::size_type 198 basic_string_view<_CharT, _Traits>:: 199 find_last_not_of(const _CharT* __str, size_type __pos, 200 size_type __n) const noexcept 201 { 202 __glibcxx_requires_string_len(__str, __n); 203 size_type __size = this->_M_len; 204 if (__size) 205 { 206 if (--__size > __pos) 207 __size = __pos; 208 do 209 { 210 if (!traits_type::find(__str, __n, this->_M_str[__size])) 211 return __size; 212 } 213 while (__size--); 214 } 215 return npos; 216 } 217 218 template<typename _CharT, typename _Traits> 219 constexpr typename basic_string_view<_CharT, _Traits>::size_type 220 basic_string_view<_CharT, _Traits>:: 221 find_last_not_of(_CharT __c, size_type __pos) const noexcept 222 { 223 size_type __size = this->_M_len; 224 if (__size) 225 { 226 if (--__size > __pos) 227 __size = __pos; 228 do 229 { 230 if (!traits_type::eq(this->_M_str[__size], __c)) 231 return __size; 232 } 233 while (__size--); 234 } 235 return npos; 236 } 237 238 _GLIBCXX_END_NAMESPACE_VERSION 239 } // namespace std 240 241 #endif // __cplusplus <= 201402L 242 243 #endif // _GLIBCXX_STRING_VIEW_TCC