string_view.tcc 7.0 KB

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