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