libstdc++
|
00001 // Components for manipulating non-owning sequences of characters -*- C++ -*- 00002 00003 // Copyright (C) 2013-2018 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/bits/string_view.tcc 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{string_view} 00028 */ 00029 00030 // 00031 // N3762 basic_string_view library 00032 // 00033 00034 #ifndef _GLIBCXX_STRING_VIEW_TCC 00035 #define _GLIBCXX_STRING_VIEW_TCC 1 00036 00037 #pragma GCC system_header 00038 00039 #if __cplusplus >= 201703L 00040 00041 namespace std _GLIBCXX_VISIBILITY(default) 00042 { 00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00044 00045 template<typename _CharT, typename _Traits> 00046 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00047 basic_string_view<_CharT, _Traits>:: 00048 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept 00049 { 00050 __glibcxx_requires_string_len(__str, __n); 00051 00052 if (__n == 0) 00053 return __pos <= this->_M_len ? __pos : npos; 00054 00055 if (__n <= this->_M_len) 00056 { 00057 for (; __pos <= this->_M_len - __n; ++__pos) 00058 if (traits_type::eq(this->_M_str[__pos], __str[0]) 00059 && traits_type::compare(this->_M_str + __pos + 1, 00060 __str + 1, __n - 1) == 0) 00061 return __pos; 00062 } 00063 return npos; 00064 } 00065 00066 template<typename _CharT, typename _Traits> 00067 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00068 basic_string_view<_CharT, _Traits>:: 00069 find(_CharT __c, size_type __pos) const noexcept 00070 { 00071 size_type __ret = npos; 00072 if (__pos < this->_M_len) 00073 { 00074 const size_type __n = this->_M_len - __pos; 00075 const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c); 00076 if (__p) 00077 __ret = __p - this->_M_str; 00078 } 00079 return __ret; 00080 } 00081 00082 template<typename _CharT, typename _Traits> 00083 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00084 basic_string_view<_CharT, _Traits>:: 00085 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept 00086 { 00087 __glibcxx_requires_string_len(__str, __n); 00088 00089 if (__n <= this->_M_len) 00090 { 00091 __pos = std::min(size_type(this->_M_len - __n), __pos); 00092 do 00093 { 00094 if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0) 00095 return __pos; 00096 } 00097 while (__pos-- > 0); 00098 } 00099 return npos; 00100 } 00101 00102 template<typename _CharT, typename _Traits> 00103 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00104 basic_string_view<_CharT, _Traits>:: 00105 rfind(_CharT __c, size_type __pos) const noexcept 00106 { 00107 size_type __size = this->_M_len; 00108 if (__size > 0) 00109 { 00110 if (--__size > __pos) 00111 __size = __pos; 00112 for (++__size; __size-- > 0; ) 00113 if (traits_type::eq(this->_M_str[__size], __c)) 00114 return __size; 00115 } 00116 return npos; 00117 } 00118 00119 template<typename _CharT, typename _Traits> 00120 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00121 basic_string_view<_CharT, _Traits>:: 00122 find_first_of(const _CharT* __str, size_type __pos, 00123 size_type __n) const noexcept 00124 { 00125 __glibcxx_requires_string_len(__str, __n); 00126 for (; __n && __pos < this->_M_len; ++__pos) 00127 { 00128 const _CharT* __p = traits_type::find(__str, __n, 00129 this->_M_str[__pos]); 00130 if (__p) 00131 return __pos; 00132 } 00133 return npos; 00134 } 00135 00136 template<typename _CharT, typename _Traits> 00137 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00138 basic_string_view<_CharT, _Traits>:: 00139 find_last_of(const _CharT* __str, size_type __pos, 00140 size_type __n) const noexcept 00141 { 00142 __glibcxx_requires_string_len(__str, __n); 00143 size_type __size = this->size(); 00144 if (__size && __n) 00145 { 00146 if (--__size > __pos) 00147 __size = __pos; 00148 do 00149 { 00150 if (traits_type::find(__str, __n, this->_M_str[__size])) 00151 return __size; 00152 } 00153 while (__size-- != 0); 00154 } 00155 return npos; 00156 } 00157 00158 template<typename _CharT, typename _Traits> 00159 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00160 basic_string_view<_CharT, _Traits>:: 00161 find_first_not_of(const _CharT* __str, size_type __pos, 00162 size_type __n) const noexcept 00163 { 00164 __glibcxx_requires_string_len(__str, __n); 00165 for (; __pos < this->_M_len; ++__pos) 00166 if (!traits_type::find(__str, __n, this->_M_str[__pos])) 00167 return __pos; 00168 return npos; 00169 } 00170 00171 template<typename _CharT, typename _Traits> 00172 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00173 basic_string_view<_CharT, _Traits>:: 00174 find_first_not_of(_CharT __c, size_type __pos) const noexcept 00175 { 00176 for (; __pos < this->_M_len; ++__pos) 00177 if (!traits_type::eq(this->_M_str[__pos], __c)) 00178 return __pos; 00179 return npos; 00180 } 00181 00182 template<typename _CharT, typename _Traits> 00183 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00184 basic_string_view<_CharT, _Traits>:: 00185 find_last_not_of(const _CharT* __str, size_type __pos, 00186 size_type __n) const noexcept 00187 { 00188 __glibcxx_requires_string_len(__str, __n); 00189 size_type __size = this->_M_len; 00190 if (__size) 00191 { 00192 if (--__size > __pos) 00193 __size = __pos; 00194 do 00195 { 00196 if (!traits_type::find(__str, __n, this->_M_str[__size])) 00197 return __size; 00198 } 00199 while (__size--); 00200 } 00201 return npos; 00202 } 00203 00204 template<typename _CharT, typename _Traits> 00205 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00206 basic_string_view<_CharT, _Traits>:: 00207 find_last_not_of(_CharT __c, size_type __pos) const noexcept 00208 { 00209 size_type __size = this->_M_len; 00210 if (__size) 00211 { 00212 if (--__size > __pos) 00213 __size = __pos; 00214 do 00215 { 00216 if (!traits_type::eq(this->_M_str[__size], __c)) 00217 return __size; 00218 } 00219 while (__size--); 00220 } 00221 return npos; 00222 } 00223 00224 _GLIBCXX_END_NAMESPACE_VERSION 00225 } // namespace std 00226 00227 #endif // __cplusplus <= 201402L 00228 00229 #endif // _GLIBCXX_STRING_VIEW_TCC