libstdc++
string_view.tcc
Go to the documentation of this file.
00001 // Components for manipulating non-owning sequences of characters -*- C++ -*-
00002 
00003 // Copyright (C) 2013-2015 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/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 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00048 
00049   template<typename _CharT, typename _Traits>
00050     typename basic_string_view<_CharT, _Traits>::size_type
00051     basic_string_view<_CharT, _Traits>::
00052     find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
00053     {
00054       __glibcxx_requires_string_len(__str, __n);
00055 
00056       if (__n == 0)
00057         return __pos <= this->_M_len ? __pos : npos;
00058 
00059       if (__n <= this->_M_len)
00060         {
00061           for (; __pos <= this->_M_len - __n; ++__pos)
00062             if (traits_type::eq(this->_M_str[__pos], __str[0])
00063                 && traits_type::compare(this->_M_str + __pos + 1,
00064                                         __str + 1, __n - 1) == 0)
00065               return __pos;
00066         }
00067       return npos;
00068     }
00069 
00070   template<typename _CharT, typename _Traits>
00071     typename basic_string_view<_CharT, _Traits>::size_type
00072     basic_string_view<_CharT, _Traits>::
00073     find(_CharT __c, size_type __pos) const noexcept
00074     {
00075       size_type __ret = npos;
00076       if (__pos < this->_M_len)
00077         {
00078           const size_type __n = this->_M_len - __pos;
00079           const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
00080           if (__p)
00081             __ret = __p - this->_M_str;
00082         }
00083       return __ret;
00084     }
00085 
00086   template<typename _CharT, typename _Traits>
00087     typename basic_string_view<_CharT, _Traits>::size_type
00088     basic_string_view<_CharT, _Traits>::
00089     rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
00090     {
00091       __glibcxx_requires_string_len(__str, __n);
00092 
00093       if (__n <= this->_M_len)
00094         {
00095           __pos = std::min(size_type(this->_M_len - __n), __pos);
00096           do
00097             {
00098               if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
00099                 return __pos;
00100             }
00101           while (__pos-- > 0);
00102         }
00103       return npos;
00104     }
00105 
00106   template<typename _CharT, typename _Traits>
00107     typename basic_string_view<_CharT, _Traits>::size_type
00108     basic_string_view<_CharT, _Traits>::
00109     rfind(_CharT __c, size_type __pos) const noexcept
00110     {
00111       size_type __size = this->_M_len;
00112       if (__size > 0)
00113         {
00114           if (--__size > __pos)
00115             __size = __pos;
00116           for (++__size; __size-- > 0; )
00117             if (traits_type::eq(this->_M_str[__size], __c))
00118               return __size;
00119         }
00120       return npos;
00121     }
00122 
00123   template<typename _CharT, typename _Traits>
00124     typename basic_string_view<_CharT, _Traits>::size_type
00125     basic_string_view<_CharT, _Traits>::
00126     find_first_of(const _CharT* __str, size_type __pos, size_type __n) const
00127     {
00128       __glibcxx_requires_string_len(__str, __n);
00129       for (; __n && __pos < this->_M_len; ++__pos)
00130         {
00131           const _CharT* __p = traits_type::find(__str, __n,
00132                                                 this->_M_str[__pos]);
00133           if (__p)
00134             return __pos;
00135         }
00136       return npos;
00137     }
00138 
00139   template<typename _CharT, typename _Traits>
00140     typename basic_string_view<_CharT, _Traits>::size_type
00141     basic_string_view<_CharT, _Traits>::
00142     find_last_of(const _CharT* __str, size_type __pos, size_type __n) const
00143     {
00144       __glibcxx_requires_string_len(__str, __n);
00145       size_type __size = this->size();
00146       if (__size && __n)
00147         {
00148           if (--__size > __pos)
00149             __size = __pos;
00150           do
00151             {
00152               if (traits_type::find(__str, __n, this->_M_str[__size]))
00153                 return __size;
00154             }
00155           while (__size-- != 0);
00156         }
00157       return npos;
00158     }
00159 
00160   template<typename _CharT, typename _Traits>
00161     typename basic_string_view<_CharT, _Traits>::size_type
00162     basic_string_view<_CharT, _Traits>::
00163     find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const
00164     {
00165       __glibcxx_requires_string_len(__str, __n);
00166       for (; __pos < this->_M_len; ++__pos)
00167         if (!traits_type::find(__str, __n, this->_M_str[__pos]))
00168           return __pos;
00169       return npos;
00170     }
00171 
00172   template<typename _CharT, typename _Traits>
00173     typename basic_string_view<_CharT, _Traits>::size_type
00174     basic_string_view<_CharT, _Traits>::
00175     find_first_not_of(_CharT __c, size_type __pos) const noexcept
00176     {
00177       for (; __pos < this->_M_len; ++__pos)
00178         if (!traits_type::eq(this->_M_str[__pos], __c))
00179           return __pos;
00180       return npos;
00181     }
00182 
00183   template<typename _CharT, typename _Traits>
00184     typename basic_string_view<_CharT, _Traits>::size_type
00185     basic_string_view<_CharT, _Traits>::
00186     find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const
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     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 experimental
00226 } // namespace std
00227 
00228 #endif // __cplusplus <= 201103L
00229 
00230 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC