string_view.tcc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Components for manipulating non-owning sequences of characters -*- C++ -*-
  2. // Copyright (C) 2013-2018 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 experimental/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{experimental/string_view}
  23. */
  24. //
  25. // N3762 basic_string_view library
  26. //
  27. #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC
  28. #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC 1
  29. #pragma GCC system_header
  30. #if __cplusplus >= 201402L
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. namespace experimental
  35. {
  36. inline namespace fundamentals_v1
  37. {
  38. template<typename _CharT, typename _Traits>
  39. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  40. basic_string_view<_CharT, _Traits>::
  41. find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
  42. {
  43. __glibcxx_requires_string_len(__str, __n);
  44. if (__n == 0)
  45. return __pos <= this->_M_len ? __pos : npos;
  46. if (__n <= this->_M_len)
  47. {
  48. for (; __pos <= this->_M_len - __n; ++__pos)
  49. if (traits_type::eq(this->_M_str[__pos], __str[0])
  50. && traits_type::compare(this->_M_str + __pos + 1,
  51. __str + 1, __n - 1) == 0)
  52. return __pos;
  53. }
  54. return npos;
  55. }
  56. template<typename _CharT, typename _Traits>
  57. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  58. basic_string_view<_CharT, _Traits>::
  59. find(_CharT __c, size_type __pos) const noexcept
  60. {
  61. size_type __ret = npos;
  62. if (__pos < this->_M_len)
  63. {
  64. const size_type __n = this->_M_len - __pos;
  65. const _CharT* __p = traits_type::find(this->_M_str + __pos, __n, __c);
  66. if (__p)
  67. __ret = __p - this->_M_str;
  68. }
  69. return __ret;
  70. }
  71. template<typename _CharT, typename _Traits>
  72. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  73. basic_string_view<_CharT, _Traits>::
  74. rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
  75. {
  76. __glibcxx_requires_string_len(__str, __n);
  77. if (__n <= this->_M_len)
  78. {
  79. __pos = std::min(size_type(this->_M_len - __n), __pos);
  80. do
  81. {
  82. if (traits_type::compare(this->_M_str + __pos, __str, __n) == 0)
  83. return __pos;
  84. }
  85. while (__pos-- > 0);
  86. }
  87. return npos;
  88. }
  89. template<typename _CharT, typename _Traits>
  90. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  91. basic_string_view<_CharT, _Traits>::
  92. rfind(_CharT __c, size_type __pos) const noexcept
  93. {
  94. size_type __size = this->_M_len;
  95. if (__size > 0)
  96. {
  97. if (--__size > __pos)
  98. __size = __pos;
  99. for (++__size; __size-- > 0; )
  100. if (traits_type::eq(this->_M_str[__size], __c))
  101. return __size;
  102. }
  103. return npos;
  104. }
  105. template<typename _CharT, typename _Traits>
  106. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  107. basic_string_view<_CharT, _Traits>::
  108. find_first_of(const _CharT* __str, size_type __pos, size_type __n) const
  109. {
  110. __glibcxx_requires_string_len(__str, __n);
  111. for (; __n && __pos < this->_M_len; ++__pos)
  112. {
  113. const _CharT* __p = traits_type::find(__str, __n,
  114. this->_M_str[__pos]);
  115. if (__p)
  116. return __pos;
  117. }
  118. return npos;
  119. }
  120. template<typename _CharT, typename _Traits>
  121. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  122. basic_string_view<_CharT, _Traits>::
  123. find_last_of(const _CharT* __str, size_type __pos, size_type __n) const
  124. {
  125. __glibcxx_requires_string_len(__str, __n);
  126. size_type __size = this->size();
  127. if (__size && __n)
  128. {
  129. if (--__size > __pos)
  130. __size = __pos;
  131. do
  132. {
  133. if (traits_type::find(__str, __n, this->_M_str[__size]))
  134. return __size;
  135. }
  136. while (__size-- != 0);
  137. }
  138. return npos;
  139. }
  140. template<typename _CharT, typename _Traits>
  141. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  142. basic_string_view<_CharT, _Traits>::
  143. find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const
  144. {
  145. __glibcxx_requires_string_len(__str, __n);
  146. for (; __pos < this->_M_len; ++__pos)
  147. if (!traits_type::find(__str, __n, this->_M_str[__pos]))
  148. return __pos;
  149. return npos;
  150. }
  151. template<typename _CharT, typename _Traits>
  152. constexpr typename basic_string_view<_CharT, _Traits>::size_type
  153. basic_string_view<_CharT, _Traits>::
  154. find_first_not_of(_CharT __c, size_type __pos) const noexcept
  155. {
  156. for (; __pos < this->_M_len; ++__pos)
  157. if (!traits_type::eq(this->_M_str[__pos], __c))
  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_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const
  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. } // namespace fundamentals_v1
  201. } // namespace experimental
  202. _GLIBCXX_END_NAMESPACE_VERSION
  203. } // namespace std
  204. #endif // __cplusplus <= 201103L
  205. #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC