You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
6.5KB

  1. // Components for manipulating non-owning sequences of characters -*- C++ -*-
  2. // Copyright (C) 2013-2020 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 <= 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. __glibcxx_requires_string_len(__str, __n);
  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,
  105. size_type __n) const noexcept
  106. {
  107. __glibcxx_requires_string_len(__str, __n);
  108. for (; __n && __pos < this->_M_len; ++__pos)
  109. {
  110. const _CharT* __p = traits_type::find(__str, __n,
  111. this->_M_str[__pos]);
  112. if (__p)
  113. return __pos;
  114. }
  115. return npos;
  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_last_of(const _CharT* __str, size_type __pos,
  121. size_type __n) const noexcept
  122. {
  123. __glibcxx_requires_string_len(__str, __n);
  124. size_type __size = this->size();
  125. if (__size && __n)
  126. {
  127. if (--__size > __pos)
  128. __size = __pos;
  129. do
  130. {
  131. if (traits_type::find(__str, __n, this->_M_str[__size]))
  132. return __size;
  133. }
  134. while (__size-- != 0);
  135. }
  136. return npos;
  137. }
  138. template<typename _CharT, typename _Traits>
  139. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  140. basic_string_view<_CharT, _Traits>::
  141. find_first_not_of(const _CharT* __str, size_type __pos,
  142. size_type __n) const noexcept
  143. {
  144. __glibcxx_requires_string_len(__str, __n);
  145. for (; __pos < this->_M_len; ++__pos)
  146. if (!traits_type::find(__str, __n, this->_M_str[__pos]))
  147. return __pos;
  148. return npos;
  149. }
  150. template<typename _CharT, typename _Traits>
  151. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  152. basic_string_view<_CharT, _Traits>::
  153. find_first_not_of(_CharT __c, size_type __pos) const noexcept
  154. {
  155. for (; __pos < this->_M_len; ++__pos)
  156. if (!traits_type::eq(this->_M_str[__pos], __c))
  157. return __pos;
  158. return npos;
  159. }
  160. template<typename _CharT, typename _Traits>
  161. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  162. basic_string_view<_CharT, _Traits>::
  163. find_last_not_of(const _CharT* __str, size_type __pos,
  164. size_type __n) const noexcept
  165. {
  166. __glibcxx_requires_string_len(__str, __n);
  167. size_type __size = this->_M_len;
  168. if (__size)
  169. {
  170. if (--__size > __pos)
  171. __size = __pos;
  172. do
  173. {
  174. if (!traits_type::find(__str, __n, this->_M_str[__size]))
  175. return __size;
  176. }
  177. while (__size--);
  178. }
  179. return npos;
  180. }
  181. template<typename _CharT, typename _Traits>
  182. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  183. basic_string_view<_CharT, _Traits>::
  184. find_last_not_of(_CharT __c, size_type __pos) const noexcept
  185. {
  186. size_type __size = this->_M_len;
  187. if (__size)
  188. {
  189. if (--__size > __pos)
  190. __size = __pos;
  191. do
  192. {
  193. if (!traits_type::eq(this->_M_str[__size], __c))
  194. return __size;
  195. }
  196. while (__size--);
  197. }
  198. return npos;
  199. }
  200. _GLIBCXX_END_NAMESPACE_VERSION
  201. } // namespace std
  202. #endif // __cplusplus <= 201402L
  203. #endif // _GLIBCXX_STRING_VIEW_TCC