string_view 22 KB

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