gdb_string_view.tcc 6.5 KB

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