xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/gdb_string_view.tcc (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos // Components for manipulating non-owning sequences of characters -*- C++ -*-
27d62b00eSchristos 
37d62b00eSchristos // Note: This file has been stolen from the gcc repo
47d62b00eSchristos // (libstdc++-v3/include/experimental/bits/string_view.tcc) and has local
57d62b00eSchristos // modifications.
67d62b00eSchristos 
7*6881a400Schristos // Copyright (C) 2013-2023 Free Software Foundation, Inc.
87d62b00eSchristos //
97d62b00eSchristos // This file is part of the GNU ISO C++ Library.  This library is free
107d62b00eSchristos // software; you can redistribute it and/or modify it under the
117d62b00eSchristos // terms of the GNU General Public License as published by the
127d62b00eSchristos // Free Software Foundation; either version 3, or (at your option)
137d62b00eSchristos // any later version.
147d62b00eSchristos 
157d62b00eSchristos // This library is distributed in the hope that it will be useful,
167d62b00eSchristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
177d62b00eSchristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
187d62b00eSchristos // GNU General Public License for more details.
197d62b00eSchristos 
207d62b00eSchristos // Under Section 7 of GPL version 3, you are granted additional
217d62b00eSchristos // permissions described in the GCC Runtime Library Exception, version
227d62b00eSchristos // 3.1, as published by the Free Software Foundation.
237d62b00eSchristos 
247d62b00eSchristos // You should have received a copy of the GNU General Public License and
257d62b00eSchristos // a copy of the GCC Runtime Library Exception along with this program;
267d62b00eSchristos // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
277d62b00eSchristos // <http://www.gnu.org/licenses/>.
287d62b00eSchristos 
297d62b00eSchristos /** @file experimental/bits/string_view.tcc
307d62b00eSchristos  *  This is an internal header file, included by other library headers.
317d62b00eSchristos  *  Do not attempt to use it directly. @headername{experimental/string_view}
327d62b00eSchristos  */
337d62b00eSchristos 
347d62b00eSchristos //
357d62b00eSchristos // N3762 basic_string_view library
367d62b00eSchristos //
377d62b00eSchristos 
387d62b00eSchristos #ifndef GDB_STRING_VIEW_TCC
397d62b00eSchristos #define GDB_STRING_VIEW_TCC 1
407d62b00eSchristos 
417d62b00eSchristos namespace gdb
427d62b00eSchristos {
437d62b00eSchristos   template<typename _CharT, typename _Traits>
447d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
457d62b00eSchristos     basic_string_view<_CharT, _Traits>::
467d62b00eSchristos     find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
477d62b00eSchristos     {
487d62b00eSchristos       gdb_assert (__str != nullptr || __n == 0);
497d62b00eSchristos 
507d62b00eSchristos       if (__n == 0)
517d62b00eSchristos 	return __pos <= this->_M_len ? __pos : npos;
527d62b00eSchristos 
537d62b00eSchristos       if (__n <= this->_M_len)
547d62b00eSchristos 	{
557d62b00eSchristos 	  for (; __pos <= this->_M_len - __n; ++__pos)
567d62b00eSchristos 	    if (traits_type::eq(this->_M_str[__pos], __str[0])
577d62b00eSchristos 		&& traits_type::compare(this->_M_str + __pos + 1,
587d62b00eSchristos 					__str + 1, __n - 1) == 0)
597d62b00eSchristos 	      return __pos;
607d62b00eSchristos 	}
617d62b00eSchristos       return npos;
627d62b00eSchristos     }
637d62b00eSchristos 
647d62b00eSchristos   template<typename _CharT, typename _Traits>
657d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
667d62b00eSchristos     basic_string_view<_CharT, _Traits>::
677d62b00eSchristos     find(_CharT __c, size_type __pos) const noexcept
687d62b00eSchristos     {
697d62b00eSchristos       size_type __ret = npos;
707d62b00eSchristos       if (__pos < this->_M_len)
717d62b00eSchristos 	{
727d62b00eSchristos 	  const size_type __n = this->_M_len - __pos;
737d62b00eSchristos 	  const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
747d62b00eSchristos 	  if (__p)
757d62b00eSchristos 	    __ret = __p - this->_M_str;
767d62b00eSchristos 	}
777d62b00eSchristos       return __ret;
787d62b00eSchristos     }
797d62b00eSchristos 
807d62b00eSchristos   template<typename _CharT, typename _Traits>
817d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
827d62b00eSchristos     basic_string_view<_CharT, _Traits>::
837d62b00eSchristos     rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
847d62b00eSchristos     {
857d62b00eSchristos       gdb_assert (__str != nullptr || __n == 0);
867d62b00eSchristos 
877d62b00eSchristos       if (__n <= this->_M_len)
887d62b00eSchristos 	{
897d62b00eSchristos 	  __pos = std::min(size_type(this->_M_len - __n), __pos);
907d62b00eSchristos 	  do
917d62b00eSchristos 	    {
927d62b00eSchristos 	      if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
937d62b00eSchristos 		return __pos;
947d62b00eSchristos 	    }
957d62b00eSchristos 	  while (__pos-- > 0);
967d62b00eSchristos 	}
977d62b00eSchristos       return npos;
987d62b00eSchristos     }
997d62b00eSchristos 
1007d62b00eSchristos   template<typename _CharT, typename _Traits>
1017d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
1027d62b00eSchristos     basic_string_view<_CharT, _Traits>::
1037d62b00eSchristos     rfind(_CharT __c, size_type __pos) const noexcept
1047d62b00eSchristos     {
1057d62b00eSchristos       size_type __size = this->_M_len;
1067d62b00eSchristos       if (__size > 0)
1077d62b00eSchristos 	{
1087d62b00eSchristos 	  if (--__size > __pos)
1097d62b00eSchristos 	    __size = __pos;
1107d62b00eSchristos 	  for (++__size; __size-- > 0; )
1117d62b00eSchristos 	    if (traits_type::eq(this->_M_str[__size], __c))
1127d62b00eSchristos 	      return __size;
1137d62b00eSchristos 	}
1147d62b00eSchristos       return npos;
1157d62b00eSchristos     }
1167d62b00eSchristos 
1177d62b00eSchristos   template<typename _CharT, typename _Traits>
1187d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
1197d62b00eSchristos     basic_string_view<_CharT, _Traits>::
1207d62b00eSchristos     find_first_of(const _CharT* __str, size_type __pos, size_type __n) const
1217d62b00eSchristos     {
1227d62b00eSchristos       gdb_assert (__str != nullptr || __n == 0);
1237d62b00eSchristos       for (; __n && __pos < this->_M_len; ++__pos)
1247d62b00eSchristos 	{
1257d62b00eSchristos 	  const _CharT* __p = traits_type::find(__str, __n,
1267d62b00eSchristos 						this->_M_str[__pos]);
1277d62b00eSchristos 	  if (__p)
1287d62b00eSchristos 	    return __pos;
1297d62b00eSchristos 	}
1307d62b00eSchristos       return npos;
1317d62b00eSchristos     }
1327d62b00eSchristos 
1337d62b00eSchristos   template<typename _CharT, typename _Traits>
1347d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
1357d62b00eSchristos     basic_string_view<_CharT, _Traits>::
1367d62b00eSchristos     find_last_of(const _CharT* __str, size_type __pos, size_type __n) const
1377d62b00eSchristos     {
1387d62b00eSchristos       gdb_assert (__str != nullptr || __n == 0);
1397d62b00eSchristos       size_type __size = this->size();
1407d62b00eSchristos       if (__size && __n)
1417d62b00eSchristos 	{
1427d62b00eSchristos 	  if (--__size > __pos)
1437d62b00eSchristos 	    __size = __pos;
1447d62b00eSchristos 	  do
1457d62b00eSchristos 	    {
1467d62b00eSchristos 	      if (traits_type::find(__str, __n, this->_M_str[__size]))
1477d62b00eSchristos 		return __size;
1487d62b00eSchristos 	    }
1497d62b00eSchristos 	  while (__size-- != 0);
1507d62b00eSchristos 	}
1517d62b00eSchristos       return npos;
1527d62b00eSchristos     }
1537d62b00eSchristos 
1547d62b00eSchristos   template<typename _CharT, typename _Traits>
1557d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
1567d62b00eSchristos     basic_string_view<_CharT, _Traits>::
1577d62b00eSchristos     find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const
1587d62b00eSchristos     {
1597d62b00eSchristos       gdb_assert (__str != nullptr || __n == 0);
1607d62b00eSchristos       for (; __pos < this->_M_len; ++__pos)
1617d62b00eSchristos 	if (!traits_type::find(__str, __n, this->_M_str[__pos]))
1627d62b00eSchristos 	  return __pos;
1637d62b00eSchristos       return npos;
1647d62b00eSchristos     }
1657d62b00eSchristos 
1667d62b00eSchristos   template<typename _CharT, typename _Traits>
1677d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
1687d62b00eSchristos     basic_string_view<_CharT, _Traits>::
1697d62b00eSchristos     find_first_not_of(_CharT __c, size_type __pos) const noexcept
1707d62b00eSchristos     {
1717d62b00eSchristos       for (; __pos < this->_M_len; ++__pos)
1727d62b00eSchristos 	if (!traits_type::eq(this->_M_str[__pos], __c))
1737d62b00eSchristos 	  return __pos;
1747d62b00eSchristos       return npos;
1757d62b00eSchristos     }
1767d62b00eSchristos 
1777d62b00eSchristos   template<typename _CharT, typename _Traits>
1787d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
1797d62b00eSchristos     basic_string_view<_CharT, _Traits>::
1807d62b00eSchristos     find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const
1817d62b00eSchristos     {
1827d62b00eSchristos       gdb_assert (__str != nullptr || __n == 0);
1837d62b00eSchristos       size_type __size = this->_M_len;
1847d62b00eSchristos       if (__size)
1857d62b00eSchristos 	{
1867d62b00eSchristos 	  if (--__size > __pos)
1877d62b00eSchristos 	    __size = __pos;
1887d62b00eSchristos 	  do
1897d62b00eSchristos 	    {
1907d62b00eSchristos 	      if (!traits_type::find(__str, __n, this->_M_str[__size]))
1917d62b00eSchristos 		return __size;
1927d62b00eSchristos 	    }
1937d62b00eSchristos 	  while (__size--);
1947d62b00eSchristos 	}
1957d62b00eSchristos       return npos;
1967d62b00eSchristos     }
1977d62b00eSchristos 
1987d62b00eSchristos   template<typename _CharT, typename _Traits>
1997d62b00eSchristos     /*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
2007d62b00eSchristos     basic_string_view<_CharT, _Traits>::
2017d62b00eSchristos     find_last_not_of(_CharT __c, size_type __pos) const noexcept
2027d62b00eSchristos     {
2037d62b00eSchristos       size_type __size = this->_M_len;
2047d62b00eSchristos       if (__size)
2057d62b00eSchristos 	{
2067d62b00eSchristos 	  if (--__size > __pos)
2077d62b00eSchristos 	    __size = __pos;
2087d62b00eSchristos 	  do
2097d62b00eSchristos 	    {
2107d62b00eSchristos 	      if (!traits_type::eq(this->_M_str[__size], __c))
2117d62b00eSchristos 		return __size;
2127d62b00eSchristos 	    }
2137d62b00eSchristos 	  while (__size--);
2147d62b00eSchristos 	}
2157d62b00eSchristos       return npos;
2167d62b00eSchristos     }
2177d62b00eSchristos } // namespace gdb
2187d62b00eSchristos 
2197d62b00eSchristos #endif // GDB_STRING_VIEW_TCC
220