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