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 experimental/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{experimental/string_view} 00028 */ 00029 00030 // 00031 // N3762 basic_string_view library 00032 // 00033 00034 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC 00035 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC 1 00036 00037 #pragma GCC system_header 00038 00039 #if __cplusplus <= 201103L 00040 # include <bits/c++14_warning.h> 00041 #else 00042 00043 namespace std _GLIBCXX_VISIBILITY(default) 00044 { 00045 namespace experimental 00046 { 00047 inline namespace fundamentals_v1 00048 { 00049 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00050 00051 template<typename _CharT, typename _Traits> 00052 typename basic_string_view<_CharT, _Traits>::size_type 00053 basic_string_view<_CharT, _Traits>:: 00054 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept 00055 { 00056 __glibcxx_requires_string_len(__str, __n); 00057 00058 if (__n == 0) 00059 return __pos <= this->_M_len ? __pos : npos; 00060 00061 if (__n <= this->_M_len) 00062 { 00063 for (; __pos <= this->_M_len - __n; ++__pos) 00064 if (traits_type::eq(this->_M_str[__pos], __str[0]) 00065 && traits_type::compare(this->_M_str + __pos + 1, 00066 __str + 1, __n - 1) == 0) 00067 return __pos; 00068 } 00069 return npos; 00070 } 00071 00072 template<typename _CharT, typename _Traits> 00073 typename basic_string_view<_CharT, _Traits>::size_type 00074 basic_string_view<_CharT, _Traits>:: 00075 find(_CharT __c, size_type __pos) const noexcept 00076 { 00077 size_type __ret = npos; 00078 if (__pos < this->_M_len) 00079 { 00080 const size_type __n = this->_M_len - __pos; 00081 const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c); 00082 if (__p) 00083 __ret = __p - this->_M_str; 00084 } 00085 return __ret; 00086 } 00087 00088 template<typename _CharT, typename _Traits> 00089 typename basic_string_view<_CharT, _Traits>::size_type 00090 basic_string_view<_CharT, _Traits>:: 00091 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept 00092 { 00093 __glibcxx_requires_string_len(__str, __n); 00094 00095 if (__n <= this->_M_len) 00096 { 00097 __pos = std::min(size_type(this->_M_len - __n), __pos); 00098 do 00099 { 00100 if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0) 00101 return __pos; 00102 } 00103 while (__pos-- > 0); 00104 } 00105 return npos; 00106 } 00107 00108 template<typename _CharT, typename _Traits> 00109 typename basic_string_view<_CharT, _Traits>::size_type 00110 basic_string_view<_CharT, _Traits>:: 00111 rfind(_CharT __c, size_type __pos) const noexcept 00112 { 00113 size_type __size = this->_M_len; 00114 if (__size > 0) 00115 { 00116 if (--__size > __pos) 00117 __size = __pos; 00118 for (++__size; __size-- > 0; ) 00119 if (traits_type::eq(this->_M_str[__size], __c)) 00120 return __size; 00121 } 00122 return npos; 00123 } 00124 00125 template<typename _CharT, typename _Traits> 00126 typename basic_string_view<_CharT, _Traits>::size_type 00127 basic_string_view<_CharT, _Traits>:: 00128 find_first_of(const _CharT* __str, size_type __pos, size_type __n) const 00129 { 00130 __glibcxx_requires_string_len(__str, __n); 00131 for (; __n && __pos < this->_M_len; ++__pos) 00132 { 00133 const _CharT* __p = traits_type::find(__str, __n, 00134 this->_M_str[__pos]); 00135 if (__p) 00136 return __pos; 00137 } 00138 return npos; 00139 } 00140 00141 template<typename _CharT, typename _Traits> 00142 typename basic_string_view<_CharT, _Traits>::size_type 00143 basic_string_view<_CharT, _Traits>:: 00144 find_last_of(const _CharT* __str, size_type __pos, size_type __n) const 00145 { 00146 __glibcxx_requires_string_len(__str, __n); 00147 size_type __size = this->size(); 00148 if (__size && __n) 00149 { 00150 if (--__size > __pos) 00151 __size = __pos; 00152 do 00153 { 00154 if (traits_type::find(__str, __n, this->_M_str[__size])) 00155 return __size; 00156 } 00157 while (__size-- != 0); 00158 } 00159 return npos; 00160 } 00161 00162 template<typename _CharT, typename _Traits> 00163 typename basic_string_view<_CharT, _Traits>::size_type 00164 basic_string_view<_CharT, _Traits>:: 00165 find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const 00166 { 00167 __glibcxx_requires_string_len(__str, __n); 00168 for (; __pos < this->_M_len; ++__pos) 00169 if (!traits_type::find(__str, __n, this->_M_str[__pos])) 00170 return __pos; 00171 return npos; 00172 } 00173 00174 template<typename _CharT, typename _Traits> 00175 typename basic_string_view<_CharT, _Traits>::size_type 00176 basic_string_view<_CharT, _Traits>:: 00177 find_first_not_of(_CharT __c, size_type __pos) const noexcept 00178 { 00179 for (; __pos < this->_M_len; ++__pos) 00180 if (!traits_type::eq(this->_M_str[__pos], __c)) 00181 return __pos; 00182 return npos; 00183 } 00184 00185 template<typename _CharT, typename _Traits> 00186 typename basic_string_view<_CharT, _Traits>::size_type 00187 basic_string_view<_CharT, _Traits>:: 00188 find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const 00189 { 00190 __glibcxx_requires_string_len(__str, __n); 00191 size_type __size = this->_M_len; 00192 if (__size) 00193 { 00194 if (--__size > __pos) 00195 __size = __pos; 00196 do 00197 { 00198 if (!traits_type::find(__str, __n, this->_M_str[__size])) 00199 return __size; 00200 } 00201 while (__size--); 00202 } 00203 return npos; 00204 } 00205 00206 template<typename _CharT, typename _Traits> 00207 typename basic_string_view<_CharT, _Traits>::size_type 00208 basic_string_view<_CharT, _Traits>:: 00209 find_last_not_of(_CharT __c, size_type __pos) const noexcept 00210 { 00211 size_type __size = this->_M_len; 00212 if (__size) 00213 { 00214 if (--__size > __pos) 00215 __size = __pos; 00216 do 00217 { 00218 if (!traits_type::eq(this->_M_str[__size], __c)) 00219 return __size; 00220 } 00221 while (__size--); 00222 } 00223 return npos; 00224 } 00225 00226 _GLIBCXX_END_NAMESPACE_VERSION 00227 } // namespace fundamentals_v1 00228 } // namespace experimental 00229 } // namespace std 00230 00231 #endif // __cplusplus <= 201103L 00232 00233 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC