string_view 25 KB

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