string_view 22 KB

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