2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libstdc++-v3 / include / experimental / string_view.tcc
blobeeccb026344bf2dc8652f60c84a0ca83b9961973
1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file experimental/string_view.tcc
26  *  This is an internal header file, included by other library headers.
27  *  Do not attempt to use it directly. @headername{string_view}
28  */
31 // N3762 basic_string_view library
34 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC
35 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC 1
37 #pragma GCC system_header
39 #if __cplusplus <= 201103L
40 # include <bits/c++14_warning.h>
41 #else
43 namespace std _GLIBCXX_VISIBILITY(default)
45 namespace experimental
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49   template<typename _CharT, typename _Traits>
50     constexpr _CharT
51     basic_string_view<_CharT, _Traits>::_S_empty_str[1];
53   template<typename _CharT, typename _Traits>
54     typename basic_string_view<_CharT, _Traits>::size_type
55     basic_string_view<_CharT, _Traits>::
56     find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
57     {
58       __glibcxx_requires_string_len(__str, __n);
60       if (__n == 0)
61         return __pos <= this->_M_len ? __pos : npos;
63       if (__n <= this->_M_len)
64         {
65           for (; __pos <= this->_M_len - __n; ++__pos)
66             if (traits_type::eq(this->_M_str[__pos], __str[0])
67                 && traits_type::compare(this->_M_str + __pos + 1,
68                                         __str + 1, __n - 1) == 0)
69               return __pos;
70         }
71       return npos;
72     }
74   template<typename _CharT, typename _Traits>
75     typename basic_string_view<_CharT, _Traits>::size_type
76     basic_string_view<_CharT, _Traits>::
77     find(_CharT __c, size_type __pos) const noexcept
78     {
79       size_type __ret = npos;
80       if (__pos < this->_M_len)
81         {
82           const size_type __n = this->_M_len - __pos;
83           const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
84           if (__p)
85             __ret = __p - this->_M_str;
86         }
87       return __ret;
88     }
90   template<typename _CharT, typename _Traits>
91     typename basic_string_view<_CharT, _Traits>::size_type
92     basic_string_view<_CharT, _Traits>::
93     rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
94     {
95       __glibcxx_requires_string_len(__str, __n);
97       if (__n <= this->_M_len)
98         {
99           __pos = std::min(size_type(this->_M_len - __n), __pos);
100           do
101             {
102               if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
103                 return __pos;
104             }
105           while (__pos-- > 0);
106         }
107       return npos;
108     }
110   template<typename _CharT, typename _Traits>
111     typename basic_string_view<_CharT, _Traits>::size_type
112     basic_string_view<_CharT, _Traits>::
113     rfind(_CharT __c, size_type __pos) const noexcept
114     {
115       size_type __size = this->_M_len;
116       if (__size > 0)
117         {
118           if (--__size > __pos)
119             __size = __pos;
120           for (++__size; __size-- > 0; )
121             if (traits_type::eq(this->_M_str[__size], __c))
122               return __size;
123         }
124       return npos;
125     }
127   template<typename _CharT, typename _Traits>
128     typename basic_string_view<_CharT, _Traits>::size_type
129     basic_string_view<_CharT, _Traits>::
130     find_first_of(const _CharT* __str, size_type __pos, size_type __n) const
131     {
132       __glibcxx_requires_string_len(__str, __n);
133       for (; __n && __pos < this->_M_len; ++__pos)
134         {
135           const _CharT* __p = traits_type::find(__str, __n,
136                                                 this->_M_str[__pos]);
137           if (__p)
138             return __pos;
139         }
140       return npos;
141     }
143   template<typename _CharT, typename _Traits>
144     typename basic_string_view<_CharT, _Traits>::size_type
145     basic_string_view<_CharT, _Traits>::
146     find_last_of(const _CharT* __str, size_type __pos, size_type __n) const
147     {
148       __glibcxx_requires_string_len(__str, __n);
149       size_type __size = this->size();
150       if (__size && __n)
151         {
152           if (--__size > __pos)
153             __size = __pos;
154           do
155             {
156               if (traits_type::find(__str, __n, this->_M_str[__size]))
157                 return __size;
158             }
159           while (__size-- != 0);
160         }
161       return npos;
162     }
164   template<typename _CharT, typename _Traits>
165     typename basic_string_view<_CharT, _Traits>::size_type
166     basic_string_view<_CharT, _Traits>::
167     find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const
168     {
169       __glibcxx_requires_string_len(__str, __n);
170       for (; __pos < this->_M_len; ++__pos)
171         if (!traits_type::find(__str, __n, this->_M_str[__pos]))
172           return __pos;
173       return npos;
174     }
176   template<typename _CharT, typename _Traits>
177     typename basic_string_view<_CharT, _Traits>::size_type
178     basic_string_view<_CharT, _Traits>::
179     find_first_not_of(_CharT __c, size_type __pos) const noexcept
180     {
181       for (; __pos < this->_M_len; ++__pos)
182         if (!traits_type::eq(this->_M_str[__pos], __c))
183           return __pos;
184       return npos;
185     }
187   template<typename _CharT, typename _Traits>
188     typename basic_string_view<_CharT, _Traits>::size_type
189     basic_string_view<_CharT, _Traits>::
190     find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const
191     {
192       __glibcxx_requires_string_len(__str, __n);
193       size_type __size = this->_M_len;
194       if (__size)
195         {
196           if (--__size > __pos)
197             __size = __pos;
198           do
199             {
200               if (!traits_type::find(__str, __n, this->_M_str[__size]))
201                 return __size;
202             }
203           while (__size--);
204         }
205       return npos;
206     }
208   template<typename _CharT, typename _Traits>
209     typename basic_string_view<_CharT, _Traits>::size_type
210     basic_string_view<_CharT, _Traits>::
211     find_last_not_of(_CharT __c, size_type __pos) const noexcept
212     {
213       size_type __size = this->_M_len;
214       if (__size)
215         {
216           if (--__size > __pos)
217             __size = __pos;
218           do
219             {
220               if (!traits_type::eq(this->_M_str[__size], __c))
221                 return __size;
222             }
223           while (__size--);
224         }
225       return npos;
226     }
228 _GLIBCXX_END_NAMESPACE_VERSION
229 } // namespace experimental
230 } // namespace std
232 #endif // __cplusplus <= 201103L
234 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC