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.

string_view 24KB

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