string_view 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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/string_view
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // N3762 basic_string_view library
  25. //
  26. #ifndef _GLIBCXX_STRING_VIEW
  27. #define _GLIBCXX_STRING_VIEW 1
  28. #pragma GCC system_header
  29. #if __cplusplus >= 201703L
  30. #include <iosfwd>
  31. #include <bits/char_traits.h>
  32. #include <bits/functexcept.h>
  33. #include <bits/functional_hash.h>
  34. #include <bits/range_access.h>
  35. #include <bits/ostream_insert.h>
  36. #include <bits/stl_algobase.h>
  37. #include <ext/numeric_traits.h>
  38. #if __cplusplus >= 202002L
  39. # include <bits/ranges_base.h>
  40. #endif
  41. namespace std _GLIBCXX_VISIBILITY(default)
  42. {
  43. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  44. # define __cpp_lib_string_view 201803L
  45. #if __cplusplus > 201703L
  46. # define __cpp_lib_constexpr_string_view 201811L
  47. #endif
  48. // Helper for basic_string and basic_string_view members.
  49. constexpr size_t
  50. __sv_check(size_t __size, size_t __pos, const char* __s)
  51. {
  52. if (__pos > __size)
  53. __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > __size "
  54. "(which is %zu)"), __s, __pos, __size);
  55. return __pos;
  56. }
  57. // Helper for basic_string members.
  58. // NB: __sv_limit doesn't check for a bad __pos value.
  59. constexpr size_t
  60. __sv_limit(size_t __size, size_t __pos, size_t __off) noexcept
  61. {
  62. const bool __testoff = __off < __size - __pos;
  63. return __testoff ? __off : __size - __pos;
  64. }
  65. /**
  66. * @class basic_string_view <string_view>
  67. * @brief A non-owning reference to a string.
  68. *
  69. * @ingroup strings
  70. * @ingroup sequences
  71. *
  72. * @tparam _CharT Type of character
  73. * @tparam _Traits Traits for character type, defaults to
  74. * char_traits<_CharT>.
  75. *
  76. * A basic_string_view looks like this:
  77. *
  78. * @code
  79. * _CharT* _M_str
  80. * size_t _M_len
  81. * @endcode
  82. */
  83. template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
  84. class basic_string_view
  85. {
  86. static_assert(!is_array_v<_CharT>);
  87. static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
  88. static_assert(is_same_v<_CharT, typename _Traits::char_type>);
  89. public:
  90. // types
  91. using traits_type = _Traits;
  92. using value_type = _CharT;
  93. using pointer = value_type*;
  94. using const_pointer = const value_type*;
  95. using reference = value_type&;
  96. using const_reference = const value_type&;
  97. using const_iterator = const value_type*;
  98. using iterator = const_iterator;
  99. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  100. using reverse_iterator = const_reverse_iterator;
  101. using size_type = size_t;
  102. using difference_type = ptrdiff_t;
  103. static constexpr size_type npos = size_type(-1);
  104. // [string.view.cons], construction and assignment
  105. constexpr
  106. basic_string_view() noexcept
  107. : _M_len{0}, _M_str{nullptr}
  108. { }
  109. constexpr basic_string_view(const basic_string_view&) noexcept = default;
  110. __attribute__((__nonnull__)) constexpr
  111. basic_string_view(const _CharT* __str) noexcept
  112. : _M_len{traits_type::length(__str)},
  113. _M_str{__str}
  114. { }
  115. constexpr
  116. basic_string_view(const _CharT* __str, size_type __len) noexcept
  117. : _M_len{__len}, _M_str{__str}
  118. { }
  119. #if __cplusplus >= 202002L && __cpp_lib_concepts
  120. template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
  121. requires same_as<iter_value_t<_It>, _CharT>
  122. && (!convertible_to<_End, size_type>)
  123. constexpr
  124. basic_string_view(_It __first, _End __last)
  125. noexcept(noexcept(__last - __first))
  126. : _M_len(__last - __first), _M_str(std::to_address(__first))
  127. { }
  128. #if __cplusplus > 202002L
  129. template<typename _Range, typename _DRange = remove_cvref_t<_Range>>
  130. requires (!is_same_v<_DRange, basic_string_view>)
  131. && ranges::contiguous_range<_Range>
  132. && ranges::sized_range<_Range>
  133. && is_same_v<ranges::range_value_t<_Range>, _CharT>
  134. && (!is_convertible_v<_Range, const _CharT*>)
  135. && (!requires (_DRange& __d) {
  136. __d.operator ::std::basic_string_view<_CharT, _Traits>();
  137. })
  138. && (!requires { typename _DRange::traits_type; }
  139. || is_same_v<typename _DRange::traits_type, _Traits>)
  140. constexpr explicit
  141. basic_string_view(_Range&& __r)
  142. noexcept(noexcept(ranges::size(__r)) && noexcept(ranges::data(__r)))
  143. : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
  144. { }
  145. basic_string_view(nullptr_t) = delete;
  146. #endif // C++23
  147. #endif // C++20
  148. constexpr basic_string_view&
  149. operator=(const basic_string_view&) noexcept = default;
  150. // [string.view.iterators], iterator support
  151. constexpr const_iterator
  152. begin() const noexcept
  153. { return this->_M_str; }
  154. constexpr const_iterator
  155. end() const noexcept
  156. { return this->_M_str + this->_M_len; }
  157. constexpr const_iterator
  158. cbegin() const noexcept
  159. { return this->_M_str; }
  160. constexpr const_iterator
  161. cend() const noexcept
  162. { return this->_M_str + this->_M_len; }
  163. constexpr const_reverse_iterator
  164. rbegin() const noexcept
  165. { return const_reverse_iterator(this->end()); }
  166. constexpr const_reverse_iterator
  167. rend() const noexcept
  168. { return const_reverse_iterator(this->begin()); }
  169. constexpr const_reverse_iterator
  170. crbegin() const noexcept
  171. { return const_reverse_iterator(this->end()); }
  172. constexpr const_reverse_iterator
  173. crend() const noexcept
  174. { return const_reverse_iterator(this->begin()); }
  175. // [string.view.capacity], capacity
  176. constexpr size_type
  177. size() const noexcept
  178. { return this->_M_len; }
  179. constexpr size_type
  180. length() const noexcept
  181. { return _M_len; }
  182. constexpr size_type
  183. max_size() const noexcept
  184. {
  185. return (npos - sizeof(size_type) - sizeof(void*))
  186. / sizeof(value_type) / 4;
  187. }
  188. [[nodiscard]] constexpr bool
  189. empty() const noexcept
  190. { return this->_M_len == 0; }
  191. // [string.view.access], element access
  192. constexpr const_reference
  193. operator[](size_type __pos) const noexcept
  194. {
  195. __glibcxx_assert(__pos < this->_M_len);
  196. return *(this->_M_str + __pos);
  197. }
  198. constexpr const_reference
  199. at(size_type __pos) const
  200. {
  201. if (__pos >= _M_len)
  202. __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
  203. "(which is %zu) >= this->size() "
  204. "(which is %zu)"), __pos, this->size());
  205. return *(this->_M_str + __pos);
  206. }
  207. constexpr const_reference
  208. front() const noexcept
  209. {
  210. __glibcxx_assert(this->_M_len > 0);
  211. return *this->_M_str;
  212. }
  213. constexpr const_reference
  214. back() const noexcept
  215. {
  216. __glibcxx_assert(this->_M_len > 0);
  217. return *(this->_M_str + this->_M_len - 1);
  218. }
  219. constexpr const_pointer
  220. data() const noexcept
  221. { return this->_M_str; }
  222. // [string.view.modifiers], modifiers:
  223. constexpr void
  224. remove_prefix(size_type __n) noexcept
  225. {
  226. __glibcxx_assert(this->_M_len >= __n);
  227. this->_M_str += __n;
  228. this->_M_len -= __n;
  229. }
  230. constexpr void
  231. remove_suffix(size_type __n) noexcept
  232. { this->_M_len -= __n; }
  233. constexpr void
  234. swap(basic_string_view& __sv) noexcept
  235. {
  236. auto __tmp = *this;
  237. *this = __sv;
  238. __sv = __tmp;
  239. }
  240. // [string.view.ops], string operations:
  241. _GLIBCXX20_CONSTEXPR
  242. size_type
  243. copy(_CharT* __str, size_type __n, size_type __pos = 0) const
  244. {
  245. __glibcxx_requires_string_len(__str, __n);
  246. __pos = std::__sv_check(size(), __pos, "basic_string_view::copy");
  247. const size_type __rlen = std::min(__n, _M_len - __pos);
  248. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  249. // 2777. basic_string_view::copy should use char_traits::copy
  250. traits_type::copy(__str, data() + __pos, __rlen);
  251. return __rlen;
  252. }
  253. constexpr basic_string_view
  254. substr(size_type __pos = 0, size_type __n = npos) const noexcept(false)
  255. {
  256. __pos = std::__sv_check(size(), __pos, "basic_string_view::substr");
  257. const size_type __rlen = std::min(__n, _M_len - __pos);
  258. return basic_string_view{_M_str + __pos, __rlen};
  259. }
  260. constexpr int
  261. compare(basic_string_view __str) const noexcept
  262. {
  263. const size_type __rlen = std::min(this->_M_len, __str._M_len);
  264. int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
  265. if (__ret == 0)
  266. __ret = _S_compare(this->_M_len, __str._M_len);
  267. return __ret;
  268. }
  269. constexpr int
  270. compare(size_type __pos1, size_type __n1, basic_string_view __str) const
  271. { return this->substr(__pos1, __n1).compare(__str); }
  272. constexpr int
  273. compare(size_type __pos1, size_type __n1,
  274. basic_string_view __str, size_type __pos2, size_type __n2) const
  275. {
  276. return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
  277. }
  278. __attribute__((__nonnull__)) constexpr int
  279. compare(const _CharT* __str) const noexcept
  280. { return this->compare(basic_string_view{__str}); }
  281. __attribute__((__nonnull__)) constexpr int
  282. compare(size_type __pos1, size_type __n1, const _CharT* __str) const
  283. { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
  284. constexpr int
  285. compare(size_type __pos1, size_type __n1,
  286. const _CharT* __str, size_type __n2) const noexcept(false)
  287. {
  288. return this->substr(__pos1, __n1)
  289. .compare(basic_string_view(__str, __n2));
  290. }
  291. #if __cplusplus > 201703L
  292. #define __cpp_lib_starts_ends_with 201711L
  293. constexpr bool
  294. starts_with(basic_string_view __x) const noexcept
  295. { return this->substr(0, __x.size()) == __x; }
  296. constexpr bool
  297. starts_with(_CharT __x) const noexcept
  298. { return !this->empty() && traits_type::eq(this->front(), __x); }
  299. constexpr bool
  300. starts_with(const _CharT* __x) const noexcept
  301. { return this->starts_with(basic_string_view(__x)); }
  302. constexpr bool
  303. ends_with(basic_string_view __x) const noexcept
  304. {
  305. const auto __len = this->size();
  306. const auto __xlen = __x.size();
  307. return __len >= __xlen
  308. && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
  309. }
  310. constexpr bool
  311. ends_with(_CharT __x) const noexcept
  312. { return !this->empty() && traits_type::eq(this->back(), __x); }
  313. constexpr bool
  314. ends_with(const _CharT* __x) const noexcept
  315. { return this->ends_with(basic_string_view(__x)); }
  316. #endif // C++20
  317. #if __cplusplus > 202002L
  318. #define __cpp_lib_string_contains 202011L
  319. constexpr bool
  320. contains(basic_string_view __x) const noexcept
  321. { return this->find(__x) != npos; }
  322. constexpr bool
  323. contains(_CharT __x) const noexcept
  324. { return this->find(__x) != npos; }
  325. constexpr bool
  326. contains(const _CharT* __x) const noexcept
  327. { return this->find(__x) != npos; }
  328. #endif // C++23
  329. // [string.view.find], searching
  330. constexpr size_type
  331. find(basic_string_view __str, size_type __pos = 0) const noexcept
  332. { return this->find(__str._M_str, __pos, __str._M_len); }
  333. constexpr size_type
  334. find(_CharT __c, size_type __pos = 0) const noexcept;
  335. constexpr size_type
  336. find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
  337. __attribute__((__nonnull__)) constexpr size_type
  338. find(const _CharT* __str, size_type __pos = 0) const noexcept
  339. { return this->find(__str, __pos, traits_type::length(__str)); }
  340. constexpr size_type
  341. rfind(basic_string_view __str, size_type __pos = npos) const noexcept
  342. { return this->rfind(__str._M_str, __pos, __str._M_len); }
  343. constexpr size_type
  344. rfind(_CharT __c, size_type __pos = npos) const noexcept;
  345. constexpr size_type
  346. rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
  347. __attribute__((__nonnull__)) constexpr size_type
  348. rfind(const _CharT* __str, size_type __pos = npos) const noexcept
  349. { return this->rfind(__str, __pos, traits_type::length(__str)); }
  350. constexpr size_type
  351. find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
  352. { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
  353. constexpr size_type
  354. find_first_of(_CharT __c, size_type __pos = 0) const noexcept
  355. { return this->find(__c, __pos); }
  356. constexpr size_type
  357. find_first_of(const _CharT* __str, size_type __pos,
  358. size_type __n) const noexcept;
  359. __attribute__((__nonnull__)) constexpr size_type
  360. find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
  361. { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
  362. constexpr size_type
  363. find_last_of(basic_string_view __str,
  364. size_type __pos = npos) const noexcept
  365. { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
  366. constexpr size_type
  367. find_last_of(_CharT __c, size_type __pos=npos) const noexcept
  368. { return this->rfind(__c, __pos); }
  369. constexpr size_type
  370. find_last_of(const _CharT* __str, size_type __pos,
  371. size_type __n) const noexcept;
  372. __attribute__((__nonnull__)) constexpr size_type
  373. find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
  374. { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
  375. constexpr size_type
  376. find_first_not_of(basic_string_view __str,
  377. size_type __pos = 0) const noexcept
  378. { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
  379. constexpr size_type
  380. find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
  381. constexpr size_type
  382. find_first_not_of(const _CharT* __str,
  383. size_type __pos, size_type __n) const noexcept;
  384. __attribute__((__nonnull__)) constexpr size_type
  385. find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
  386. {
  387. return this->find_first_not_of(__str, __pos,
  388. traits_type::length(__str));
  389. }
  390. constexpr size_type
  391. find_last_not_of(basic_string_view __str,
  392. size_type __pos = npos) const noexcept
  393. { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
  394. constexpr size_type
  395. find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
  396. constexpr size_type
  397. find_last_not_of(const _CharT* __str,
  398. size_type __pos, size_type __n) const noexcept;
  399. __attribute__((__nonnull__)) constexpr size_type
  400. find_last_not_of(const _CharT* __str,
  401. size_type __pos = npos) const noexcept
  402. {
  403. return this->find_last_not_of(__str, __pos,
  404. traits_type::length(__str));
  405. }
  406. private:
  407. static constexpr int
  408. _S_compare(size_type __n1, size_type __n2) noexcept
  409. {
  410. using __limits = __gnu_cxx::__int_traits<int>;
  411. const difference_type __diff = __n1 - __n2;
  412. if (__diff > __limits::__max)
  413. return __limits::__max;
  414. if (__diff < __limits::__min)
  415. return __limits::__min;
  416. return static_cast<int>(__diff);
  417. }
  418. size_t _M_len;
  419. const _CharT* _M_str;
  420. };
  421. #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
  422. template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
  423. basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
  424. #if __cplusplus > 202002L
  425. template<ranges::contiguous_range _Range>
  426. basic_string_view(_Range&&)
  427. -> basic_string_view<ranges::range_value_t<_Range>>;
  428. #endif
  429. #endif
  430. // [string.view.comparison], non-member basic_string_view comparison function
  431. // Several of these functions use type_identity_t to create a non-deduced
  432. // context, so that only one argument participates in template argument
  433. // deduction and the other argument gets implicitly converted to the deduced
  434. // type (see N3766).
  435. template<typename _CharT, typename _Traits>
  436. constexpr bool
  437. operator==(basic_string_view<_CharT, _Traits> __x,
  438. basic_string_view<_CharT, _Traits> __y) noexcept
  439. { return __x.size() == __y.size() && __x.compare(__y) == 0; }
  440. template<typename _CharT, typename _Traits>
  441. constexpr bool
  442. operator==(basic_string_view<_CharT, _Traits> __x,
  443. __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
  444. noexcept
  445. { return __x.size() == __y.size() && __x.compare(__y) == 0; }
  446. #if __cpp_lib_three_way_comparison
  447. template<typename _CharT, typename _Traits>
  448. constexpr auto
  449. operator<=>(basic_string_view<_CharT, _Traits> __x,
  450. basic_string_view<_CharT, _Traits> __y) noexcept
  451. -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
  452. { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
  453. template<typename _CharT, typename _Traits>
  454. constexpr auto
  455. operator<=>(basic_string_view<_CharT, _Traits> __x,
  456. __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
  457. noexcept
  458. -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
  459. { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
  460. #else
  461. template<typename _CharT, typename _Traits>
  462. constexpr bool
  463. operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
  464. basic_string_view<_CharT, _Traits> __y) noexcept
  465. { return __x.size() == __y.size() && __x.compare(__y) == 0; }
  466. template<typename _CharT, typename _Traits>
  467. constexpr bool
  468. operator!=(basic_string_view<_CharT, _Traits> __x,
  469. basic_string_view<_CharT, _Traits> __y) noexcept
  470. { return !(__x == __y); }
  471. template<typename _CharT, typename _Traits>
  472. constexpr bool
  473. operator!=(basic_string_view<_CharT, _Traits> __x,
  474. __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
  475. noexcept
  476. { return !(__x == __y); }
  477. template<typename _CharT, typename _Traits>
  478. constexpr bool
  479. operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
  480. basic_string_view<_CharT, _Traits> __y) noexcept
  481. { return !(__x == __y); }
  482. template<typename _CharT, typename _Traits>
  483. constexpr bool
  484. operator< (basic_string_view<_CharT, _Traits> __x,
  485. basic_string_view<_CharT, _Traits> __y) noexcept
  486. { return __x.compare(__y) < 0; }
  487. template<typename _CharT, typename _Traits>
  488. constexpr bool
  489. operator< (basic_string_view<_CharT, _Traits> __x,
  490. __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
  491. noexcept
  492. { return __x.compare(__y) < 0; }
  493. template<typename _CharT, typename _Traits>
  494. constexpr bool
  495. operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
  496. basic_string_view<_CharT, _Traits> __y) noexcept
  497. { return __x.compare(__y) < 0; }
  498. template<typename _CharT, typename _Traits>
  499. constexpr bool
  500. operator> (basic_string_view<_CharT, _Traits> __x,
  501. basic_string_view<_CharT, _Traits> __y) noexcept
  502. { return __x.compare(__y) > 0; }
  503. template<typename _CharT, typename _Traits>
  504. constexpr bool
  505. operator> (basic_string_view<_CharT, _Traits> __x,
  506. __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
  507. noexcept
  508. { return __x.compare(__y) > 0; }
  509. template<typename _CharT, typename _Traits>
  510. constexpr bool
  511. operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
  512. basic_string_view<_CharT, _Traits> __y) noexcept
  513. { return __x.compare(__y) > 0; }
  514. template<typename _CharT, typename _Traits>
  515. constexpr bool
  516. operator<=(basic_string_view<_CharT, _Traits> __x,
  517. basic_string_view<_CharT, _Traits> __y) noexcept
  518. { return __x.compare(__y) <= 0; }
  519. template<typename _CharT, typename _Traits>
  520. constexpr bool
  521. operator<=(basic_string_view<_CharT, _Traits> __x,
  522. __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
  523. noexcept
  524. { return __x.compare(__y) <= 0; }
  525. template<typename _CharT, typename _Traits>
  526. constexpr bool
  527. operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
  528. basic_string_view<_CharT, _Traits> __y) noexcept
  529. { return __x.compare(__y) <= 0; }
  530. template<typename _CharT, typename _Traits>
  531. constexpr bool
  532. operator>=(basic_string_view<_CharT, _Traits> __x,
  533. basic_string_view<_CharT, _Traits> __y) noexcept
  534. { return __x.compare(__y) >= 0; }
  535. template<typename _CharT, typename _Traits>
  536. constexpr bool
  537. operator>=(basic_string_view<_CharT, _Traits> __x,
  538. __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
  539. noexcept
  540. { return __x.compare(__y) >= 0; }
  541. template<typename _CharT, typename _Traits>
  542. constexpr bool
  543. operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
  544. basic_string_view<_CharT, _Traits> __y) noexcept
  545. { return __x.compare(__y) >= 0; }
  546. #endif // three-way comparison
  547. // [string.view.io], Inserters and extractors
  548. template<typename _CharT, typename _Traits>
  549. inline basic_ostream<_CharT, _Traits>&
  550. operator<<(basic_ostream<_CharT, _Traits>& __os,
  551. basic_string_view<_CharT,_Traits> __str)
  552. { return __ostream_insert(__os, __str.data(), __str.size()); }
  553. // basic_string_view typedef names
  554. using string_view = basic_string_view<char>;
  555. using wstring_view = basic_string_view<wchar_t>;
  556. #ifdef _GLIBCXX_USE_CHAR8_T
  557. using u8string_view = basic_string_view<char8_t>;
  558. #endif
  559. using u16string_view = basic_string_view<char16_t>;
  560. using u32string_view = basic_string_view<char32_t>;
  561. // [string.view.hash], hash support:
  562. template<typename _Tp>
  563. struct hash;
  564. template<>
  565. struct hash<string_view>
  566. : public __hash_base<size_t, string_view>
  567. {
  568. size_t
  569. operator()(const string_view& __str) const noexcept
  570. { return std::_Hash_impl::hash(__str.data(), __str.length()); }
  571. };
  572. template<>
  573. struct __is_fast_hash<hash<string_view>> : std::false_type
  574. { };
  575. template<>
  576. struct hash<wstring_view>
  577. : public __hash_base<size_t, wstring_view>
  578. {
  579. size_t
  580. operator()(const wstring_view& __s) const noexcept
  581. { return std::_Hash_impl::hash(__s.data(),
  582. __s.length() * sizeof(wchar_t)); }
  583. };
  584. template<>
  585. struct __is_fast_hash<hash<wstring_view>> : std::false_type
  586. { };
  587. #ifdef _GLIBCXX_USE_CHAR8_T
  588. template<>
  589. struct hash<u8string_view>
  590. : public __hash_base<size_t, u8string_view>
  591. {
  592. size_t
  593. operator()(const u8string_view& __str) const noexcept
  594. { return std::_Hash_impl::hash(__str.data(), __str.length()); }
  595. };
  596. template<>
  597. struct __is_fast_hash<hash<u8string_view>> : std::false_type
  598. { };
  599. #endif
  600. template<>
  601. struct hash<u16string_view>
  602. : public __hash_base<size_t, u16string_view>
  603. {
  604. size_t
  605. operator()(const u16string_view& __s) const noexcept
  606. { return std::_Hash_impl::hash(__s.data(),
  607. __s.length() * sizeof(char16_t)); }
  608. };
  609. template<>
  610. struct __is_fast_hash<hash<u16string_view>> : std::false_type
  611. { };
  612. template<>
  613. struct hash<u32string_view>
  614. : public __hash_base<size_t, u32string_view>
  615. {
  616. size_t
  617. operator()(const u32string_view& __s) const noexcept
  618. { return std::_Hash_impl::hash(__s.data(),
  619. __s.length() * sizeof(char32_t)); }
  620. };
  621. template<>
  622. struct __is_fast_hash<hash<u32string_view>> : std::false_type
  623. { };
  624. inline namespace literals
  625. {
  626. inline namespace string_view_literals
  627. {
  628. #pragma GCC diagnostic push
  629. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  630. inline constexpr basic_string_view<char>
  631. operator""sv(const char* __str, size_t __len) noexcept
  632. { return basic_string_view<char>{__str, __len}; }
  633. inline constexpr basic_string_view<wchar_t>
  634. operator""sv(const wchar_t* __str, size_t __len) noexcept
  635. { return basic_string_view<wchar_t>{__str, __len}; }
  636. #ifdef _GLIBCXX_USE_CHAR8_T
  637. inline constexpr basic_string_view<char8_t>
  638. operator""sv(const char8_t* __str, size_t __len) noexcept
  639. { return basic_string_view<char8_t>{__str, __len}; }
  640. #endif
  641. inline constexpr basic_string_view<char16_t>
  642. operator""sv(const char16_t* __str, size_t __len) noexcept
  643. { return basic_string_view<char16_t>{__str, __len}; }
  644. inline constexpr basic_string_view<char32_t>
  645. operator""sv(const char32_t* __str, size_t __len) noexcept
  646. { return basic_string_view<char32_t>{__str, __len}; }
  647. #pragma GCC diagnostic pop
  648. } // namespace string_literals
  649. } // namespace literals
  650. #if __cpp_lib_concepts
  651. namespace ranges
  652. {
  653. // Opt-in to borrowed_range concept
  654. template<typename _CharT, typename _Traits>
  655. inline constexpr bool
  656. enable_borrowed_range<basic_string_view<_CharT, _Traits>> = true;
  657. // Opt-in to view concept
  658. template<typename _CharT, typename _Traits>
  659. inline constexpr bool
  660. enable_view<basic_string_view<_CharT, _Traits>> = true;
  661. }
  662. #endif
  663. _GLIBCXX_END_NAMESPACE_VERSION
  664. } // namespace std
  665. #include <bits/string_view.tcc>
  666. #endif // __cplusplus <= 201402L
  667. #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW