libstdc++
|
00001 // Components for manipulating non-owning sequences of characters -*- C++ -*- 00002 00003 // Copyright (C) 2013-2017 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 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, size_type __n) const 00123 { 00124 __glibcxx_requires_string_len(__str, __n); 00125 for (; __n && __pos < this->_M_len; ++__pos) 00126 { 00127 const _CharT* __p = traits_type::find(__str, __n, 00128 this->_M_str[__pos]); 00129 if (__p) 00130 return __pos; 00131 } 00132 return npos; 00133 } 00134 00135 template<typename _CharT, typename _Traits> 00136 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00137 basic_string_view<_CharT, _Traits>:: 00138 find_last_of(const _CharT* __str, size_type __pos, size_type __n) const 00139 { 00140 __glibcxx_requires_string_len(__str, __n); 00141 size_type __size = this->size(); 00142 if (__size && __n) 00143 { 00144 if (--__size > __pos) 00145 __size = __pos; 00146 do 00147 { 00148 if (traits_type::find(__str, __n, this->_M_str[__size])) 00149 return __size; 00150 } 00151 while (__size-- != 0); 00152 } 00153 return npos; 00154 } 00155 00156 template<typename _CharT, typename _Traits> 00157 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00158 basic_string_view<_CharT, _Traits>:: 00159 find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const 00160 { 00161 __glibcxx_requires_string_len(__str, __n); 00162 for (; __pos < this->_M_len; ++__pos) 00163 if (!traits_type::find(__str, __n, this->_M_str[__pos])) 00164 return __pos; 00165 return npos; 00166 } 00167 00168 template<typename _CharT, typename _Traits> 00169 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00170 basic_string_view<_CharT, _Traits>:: 00171 find_first_not_of(_CharT __c, size_type __pos) const noexcept 00172 { 00173 for (; __pos < this->_M_len; ++__pos) 00174 if (!traits_type::eq(this->_M_str[__pos], __c)) 00175 return __pos; 00176 return npos; 00177 } 00178 00179 template<typename _CharT, typename _Traits> 00180 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00181 basic_string_view<_CharT, _Traits>:: 00182 find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const 00183 { 00184 __glibcxx_requires_string_len(__str, __n); 00185 size_type __size = this->_M_len; 00186 if (__size) 00187 { 00188 if (--__size > __pos) 00189 __size = __pos; 00190 do 00191 { 00192 if (!traits_type::find(__str, __n, this->_M_str[__size])) 00193 return __size; 00194 } 00195 while (__size--); 00196 } 00197 return npos; 00198 } 00199 00200 template<typename _CharT, typename _Traits> 00201 constexpr typename basic_string_view<_CharT, _Traits>::size_type 00202 basic_string_view<_CharT, _Traits>:: 00203 find_last_not_of(_CharT __c, size_type __pos) const noexcept 00204 { 00205 size_type __size = this->_M_len; 00206 if (__size) 00207 { 00208 if (--__size > __pos) 00209 __size = __pos; 00210 do 00211 { 00212 if (!traits_type::eq(this->_M_str[__size], __c)) 00213 return __size; 00214 } 00215 while (__size--); 00216 } 00217 return npos; 00218 } 00219 00220 _GLIBCXX_END_NAMESPACE_VERSION 00221 } // namespace std 00222 00223 #endif // __cplusplus <= 201402L 00224 00225 #endif // _GLIBCXX_STRING_VIEW_TCC