string_view 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // -*- C++ -*-
  2. //===------------------------ string_view ---------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is distributed under the University of Illinois Open Source
  7. // License. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_LFTS_STRING_VIEW
  11. #define _LIBCPP_LFTS_STRING_VIEW
  12. /*
  13. string_view synopsis
  14. namespace std {
  15. namespace experimental {
  16. inline namespace library_fundamentals_v1 {
  17. // 7.2, Class template basic_string_view
  18. template<class charT, class traits = char_traits<charT>>
  19. class basic_string_view;
  20. // 7.9, basic_string_view non-member comparison functions
  21. template<class charT, class traits>
  22. constexpr bool operator==(basic_string_view<charT, traits> x,
  23. basic_string_view<charT, traits> y) noexcept;
  24. template<class charT, class traits>
  25. constexpr bool operator!=(basic_string_view<charT, traits> x,
  26. basic_string_view<charT, traits> y) noexcept;
  27. template<class charT, class traits>
  28. constexpr bool operator< (basic_string_view<charT, traits> x,
  29. basic_string_view<charT, traits> y) noexcept;
  30. template<class charT, class traits>
  31. constexpr bool operator> (basic_string_view<charT, traits> x,
  32. basic_string_view<charT, traits> y) noexcept;
  33. template<class charT, class traits>
  34. constexpr bool operator<=(basic_string_view<charT, traits> x,
  35. basic_string_view<charT, traits> y) noexcept;
  36. template<class charT, class traits>
  37. constexpr bool operator>=(basic_string_view<charT, traits> x,
  38. basic_string_view<charT, traits> y) noexcept;
  39. // see below, sufficient additional overloads of comparison functions
  40. // 7.10, Inserters and extractors
  41. template<class charT, class traits>
  42. basic_ostream<charT, traits>&
  43. operator<<(basic_ostream<charT, traits>& os,
  44. basic_string_view<charT, traits> str);
  45. // basic_string_view typedef names
  46. typedef basic_string_view<char> string_view;
  47. typedef basic_string_view<char16_t> u16string_view;
  48. typedef basic_string_view<char32_t> u32string_view;
  49. typedef basic_string_view<wchar_t> wstring_view;
  50. template<class charT, class traits = char_traits<charT>>
  51. class basic_string_view {
  52. public:
  53. // types
  54. typedef traits traits_type;
  55. typedef charT value_type;
  56. typedef charT* pointer;
  57. typedef const charT* const_pointer;
  58. typedef charT& reference;
  59. typedef const charT& const_reference;
  60. typedef implementation-defined const_iterator;
  61. typedef const_iterator iterator;
  62. typedef reverse_iterator<const_iterator> const_reverse_iterator;
  63. typedef const_reverse_iterator reverse_iterator;
  64. typedef size_t size_type;
  65. typedef ptrdiff_t difference_type;
  66. static constexpr size_type npos = size_type(-1);
  67. // 7.3, basic_string_view constructors and assignment operators
  68. constexpr basic_string_view() noexcept;
  69. constexpr basic_string_view(const basic_string_view&) noexcept = default;
  70. basic_string_view& operator=(const basic_string_view&) noexcept = default;
  71. template<class Allocator>
  72. basic_string_view(const basic_string<charT, traits, Allocator>& str) noexcept;
  73. constexpr basic_string_view(const charT* str);
  74. constexpr basic_string_view(const charT* str, size_type len);
  75. // 7.4, basic_string_view iterator support
  76. constexpr const_iterator begin() const noexcept;
  77. constexpr const_iterator end() const noexcept;
  78. constexpr const_iterator cbegin() const noexcept;
  79. constexpr const_iterator cend() const noexcept;
  80. const_reverse_iterator rbegin() const noexcept;
  81. const_reverse_iterator rend() const noexcept;
  82. const_reverse_iterator crbegin() const noexcept;
  83. const_reverse_iterator crend() const noexcept;
  84. // 7.5, basic_string_view capacity
  85. constexpr size_type size() const noexcept;
  86. constexpr size_type length() const noexcept;
  87. constexpr size_type max_size() const noexcept;
  88. constexpr bool empty() const noexcept;
  89. // 7.6, basic_string_view element access
  90. constexpr const_reference operator[](size_type pos) const;
  91. constexpr const_reference at(size_type pos) const;
  92. constexpr const_reference front() const;
  93. constexpr const_reference back() const;
  94. constexpr const_pointer data() const noexcept;
  95. // 7.7, basic_string_view modifiers
  96. constexpr void clear() noexcept;
  97. constexpr void remove_prefix(size_type n);
  98. constexpr void remove_suffix(size_type n);
  99. constexpr void swap(basic_string_view& s) noexcept;
  100. // 7.8, basic_string_view string operations
  101. template<class Allocator>
  102. explicit operator basic_string<charT, traits, Allocator>() const;
  103. template<class Allocator = allocator<charT>>
  104. basic_string<charT, traits, Allocator> to_string(
  105. const Allocator& a = Allocator()) const;
  106. size_type copy(charT* s, size_type n, size_type pos = 0) const;
  107. constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
  108. constexpr int compare(basic_string_view s) const noexcept;
  109. constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
  110. constexpr int compare(size_type pos1, size_type n1,
  111. basic_string_view s, size_type pos2, size_type n2) const;
  112. constexpr int compare(const charT* s) const;
  113. constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
  114. constexpr int compare(size_type pos1, size_type n1,
  115. const charT* s, size_type n2) const;
  116. constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
  117. constexpr size_type find(charT c, size_type pos = 0) const noexcept;
  118. constexpr size_type find(const charT* s, size_type pos, size_type n) const;
  119. constexpr size_type find(const charT* s, size_type pos = 0) const;
  120. constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
  121. constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
  122. constexpr size_type rfind(const charT* s, size_type pos, size_type n) const;
  123. constexpr size_type rfind(const charT* s, size_type pos = npos) const;
  124. constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
  125. constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
  126. constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const;
  127. constexpr size_type find_first_of(const charT* s, size_type pos = 0) const;
  128. constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
  129. constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
  130. constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const;
  131. constexpr size_type find_last_of(const charT* s, size_type pos = npos) const;
  132. constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
  133. constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
  134. constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;
  135. constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const;
  136. constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
  137. constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
  138. constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;
  139. constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const;
  140. private:
  141. const_pointer data_; // exposition only
  142. size_type size_; // exposition only
  143. };
  144. } // namespace fundamentals_v1
  145. } // namespace experimental
  146. // 7.11, Hash support
  147. template <class T> struct hash;
  148. template <> struct hash<experimental::string_view>;
  149. template <> struct hash<experimental::u16string_view>;
  150. template <> struct hash<experimental::u32string_view>;
  151. template <> struct hash<experimental::wstring_view>;
  152. } // namespace std
  153. */
  154. #include <experimental/__config>
  155. #include <string>
  156. #include <algorithm>
  157. #include <iterator>
  158. #include <ostream>
  159. #include <stdexcept>
  160. #include <iomanip>
  161. #include <__debug>
  162. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  163. #pragma GCC system_header
  164. #endif
  165. _LIBCPP_BEGIN_NAMESPACE_LFTS
  166. template<class _CharT, class _Traits = _VSTD::char_traits<_CharT> >
  167. class _LIBCPP_TYPE_VIS_ONLY basic_string_view {
  168. public:
  169. // types
  170. typedef _Traits traits_type;
  171. typedef _CharT value_type;
  172. typedef const _CharT* pointer;
  173. typedef const _CharT* const_pointer;
  174. typedef const _CharT& reference;
  175. typedef const _CharT& const_reference;
  176. typedef const_pointer const_iterator; // See [string.view.iterators]
  177. typedef const_iterator iterator;
  178. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  179. typedef const_reverse_iterator reverse_iterator;
  180. typedef size_t size_type;
  181. typedef ptrdiff_t difference_type;
  182. static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
  183. // [string.view.cons], construct/copy
  184. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  185. basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
  186. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  187. basic_string_view(const basic_string_view&) _NOEXCEPT = default;
  188. _LIBCPP_INLINE_VISIBILITY
  189. basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
  190. template<class _Allocator>
  191. _LIBCPP_INLINE_VISIBILITY
  192. basic_string_view(const basic_string<_CharT, _Traits, _Allocator>& __str) _NOEXCEPT
  193. : __data (__str.data()), __size(__str.size()) {}
  194. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  195. basic_string_view(const _CharT* __s, size_type __len)
  196. : __data(__s), __size(__len)
  197. {
  198. // _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
  199. }
  200. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  201. basic_string_view(const _CharT* __s)
  202. : __data(__s), __size(_Traits::length(__s)) {}
  203. // [string.view.iterators], iterators
  204. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  205. const_iterator begin() const _NOEXCEPT { return cbegin(); }
  206. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  207. const_iterator end() const _NOEXCEPT { return cend(); }
  208. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  209. const_iterator cbegin() const _NOEXCEPT { return __data; }
  210. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  211. const_iterator cend() const _NOEXCEPT { return __data + __size; }
  212. _LIBCPP_INLINE_VISIBILITY
  213. const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
  214. _LIBCPP_INLINE_VISIBILITY
  215. const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
  216. _LIBCPP_INLINE_VISIBILITY
  217. const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
  218. _LIBCPP_INLINE_VISIBILITY
  219. const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
  220. // [string.view.capacity], capacity
  221. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  222. size_type size() const _NOEXCEPT { return __size; }
  223. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  224. size_type length() const _NOEXCEPT { return __size; }
  225. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  226. size_type max_size() const _NOEXCEPT { return _VSTD::numeric_limits<size_type>::max(); }
  227. _LIBCPP_CONSTEXPR bool _LIBCPP_INLINE_VISIBILITY
  228. empty() const _NOEXCEPT { return __size == 0; }
  229. // [string.view.access], element access
  230. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  231. const_reference operator[](size_type __pos) const { return __data[__pos]; }
  232. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  233. const_reference at(size_type __pos) const
  234. {
  235. return __pos >= size()
  236. ? (__libcpp_throw(out_of_range("string_view::at")), __data[0])
  237. : __data[__pos];
  238. }
  239. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  240. const_reference front() const
  241. {
  242. return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
  243. }
  244. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  245. const_reference back() const
  246. {
  247. return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
  248. }
  249. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  250. const_pointer data() const _NOEXCEPT { return __data; }
  251. // [string.view.modifiers], modifiers:
  252. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  253. void clear() _NOEXCEPT
  254. {
  255. __data = nullptr;
  256. __size = 0;
  257. }
  258. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  259. void remove_prefix(size_type __n) _NOEXCEPT
  260. {
  261. _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
  262. __data += __n;
  263. __size -= __n;
  264. }
  265. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  266. void remove_suffix(size_type __n) _NOEXCEPT
  267. {
  268. _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
  269. __size -= __n;
  270. }
  271. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  272. void swap(basic_string_view& __other) _NOEXCEPT
  273. {
  274. const value_type *__p = __data;
  275. __data = __other.__data;
  276. __other.__data = __p;
  277. size_type __sz = __size;
  278. __size = __other.__size;
  279. __other.__size = __sz;
  280. // _VSTD::swap( __data, __other.__data );
  281. // _VSTD::swap( __size, __other.__size );
  282. }
  283. // [string.view.ops], string operations:
  284. template<class _Allocator>
  285. _LIBCPP_INLINE_VISIBILITY
  286. _LIBCPP_EXPLICIT operator basic_string<_CharT, _Traits, _Allocator>() const
  287. { return basic_string<_CharT, _Traits, _Allocator>( begin(), end()); }
  288. template<class _Allocator = allocator<_CharT> >
  289. _LIBCPP_INLINE_VISIBILITY
  290. basic_string<_CharT, _Traits, _Allocator>
  291. to_string( const _Allocator& __a = _Allocator()) const
  292. { return basic_string<_CharT, _Traits, _Allocator> ( begin(), end(), __a ); }
  293. size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
  294. {
  295. if ( __pos > size())
  296. __libcpp_throw(out_of_range("string_view::copy"));
  297. size_type __rlen = _VSTD::min( __n, size() - __pos );
  298. _VSTD::copy_n(begin() + __pos, __rlen, __s );
  299. return __rlen;
  300. }
  301. _LIBCPP_CONSTEXPR
  302. basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
  303. {
  304. // if (__pos > size())
  305. // throw out_of_range("string_view::substr");
  306. // size_type __rlen = _VSTD::min( __n, size() - __pos );
  307. // return basic_string_view(data() + __pos, __rlen);
  308. return __pos > size()
  309. ? (__libcpp_throw((out_of_range("string_view::substr"))), basic_string_view())
  310. : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
  311. }
  312. _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
  313. {
  314. size_type __rlen = _VSTD::min( size(), __sv.size());
  315. int __retval = _Traits::compare(data(), __sv.data(), __rlen);
  316. if ( __retval == 0 ) // first __rlen chars matched
  317. __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
  318. return __retval;
  319. }
  320. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  321. int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
  322. {
  323. return substr(__pos1, __n1).compare(__sv);
  324. }
  325. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  326. int compare( size_type __pos1, size_type __n1,
  327. basic_string_view _sv, size_type __pos2, size_type __n2) const
  328. {
  329. return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2));
  330. }
  331. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  332. int compare(const _CharT* __s) const
  333. {
  334. return compare(basic_string_view(__s));
  335. }
  336. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  337. int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
  338. {
  339. return substr(__pos1, __n1).compare(basic_string_view(__s));
  340. }
  341. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  342. int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
  343. {
  344. return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
  345. }
  346. // find
  347. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  348. size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
  349. {
  350. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
  351. return _VSTD::__str_find<value_type, size_type, traits_type, npos>
  352. (data(), size(), __s.data(), __pos, __s.size());
  353. }
  354. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  355. size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
  356. {
  357. return _VSTD::__str_find<value_type, size_type, traits_type, npos>
  358. (data(), size(), __c, __pos);
  359. }
  360. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  361. size_type find(const _CharT* __s, size_type __pos, size_type __n) const
  362. {
  363. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
  364. return _VSTD::__str_find<value_type, size_type, traits_type, npos>
  365. (data(), size(), __s, __pos, __n);
  366. }
  367. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  368. size_type find(const _CharT* __s, size_type __pos = 0) const
  369. {
  370. _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
  371. return _VSTD::__str_find<value_type, size_type, traits_type, npos>
  372. (data(), size(), __s, __pos, traits_type::length(__s));
  373. }
  374. // rfind
  375. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  376. size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
  377. {
  378. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
  379. return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
  380. (data(), size(), __s.data(), __pos, __s.size());
  381. }
  382. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  383. size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
  384. {
  385. return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
  386. (data(), size(), __c, __pos);
  387. }
  388. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  389. size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const
  390. {
  391. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
  392. return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
  393. (data(), size(), __s, __pos, __n);
  394. }
  395. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  396. size_type rfind(const _CharT* __s, size_type __pos=npos) const
  397. {
  398. _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
  399. return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
  400. (data(), size(), __s, __pos, traits_type::length(__s));
  401. }
  402. // find_first_of
  403. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  404. size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
  405. {
  406. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
  407. return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
  408. (data(), size(), __s.data(), __pos, __s.size());
  409. }
  410. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  411. size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
  412. { return find(__c, __pos); }
  413. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  414. size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  415. {
  416. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
  417. return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
  418. (data(), size(), __s, __pos, __n);
  419. }
  420. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  421. size_type find_first_of(const _CharT* __s, size_type __pos=0) const
  422. {
  423. _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
  424. return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
  425. (data(), size(), __s, __pos, traits_type::length(__s));
  426. }
  427. // find_last_of
  428. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  429. size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
  430. {
  431. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
  432. return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
  433. (data(), size(), __s.data(), __pos, __s.size());
  434. }
  435. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  436. size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
  437. { return rfind(__c, __pos); }
  438. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  439. size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  440. {
  441. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
  442. return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
  443. (data(), size(), __s, __pos, __n);
  444. }
  445. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  446. size_type find_last_of(const _CharT* __s, size_type __pos=npos) const
  447. {
  448. _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
  449. return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
  450. (data(), size(), __s, __pos, traits_type::length(__s));
  451. }
  452. // find_first_not_of
  453. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  454. size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
  455. {
  456. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
  457. return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  458. (data(), size(), __s.data(), __pos, __s.size());
  459. }
  460. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  461. size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
  462. {
  463. return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  464. (data(), size(), __c, __pos);
  465. }
  466. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  467. size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  468. {
  469. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
  470. return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  471. (data(), size(), __s, __pos, __n);
  472. }
  473. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  474. size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const
  475. {
  476. _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
  477. return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  478. (data(), size(), __s, __pos, traits_type::length(__s));
  479. }
  480. // find_last_not_of
  481. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  482. size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
  483. {
  484. _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
  485. return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  486. (data(), size(), __s.data(), __pos, __s.size());
  487. }
  488. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  489. size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
  490. {
  491. return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  492. (data(), size(), __c, __pos);
  493. }
  494. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  495. size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  496. {
  497. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
  498. return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  499. (data(), size(), __s, __pos, __n);
  500. }
  501. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  502. size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const
  503. {
  504. _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
  505. return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  506. (data(), size(), __s, __pos, traits_type::length(__s));
  507. }
  508. private:
  509. const value_type* __data;
  510. size_type __size;
  511. };
  512. // [string.view.comparison]
  513. // operator ==
  514. template<class _CharT, class _Traits>
  515. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  516. bool operator==(basic_string_view<_CharT, _Traits> __lhs,
  517. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  518. {
  519. if ( __lhs.size() != __rhs.size()) return false;
  520. return __lhs.compare(__rhs) == 0;
  521. }
  522. template<class _CharT, class _Traits>
  523. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  524. bool operator==(basic_string_view<_CharT, _Traits> __lhs,
  525. typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
  526. {
  527. if ( __lhs.size() != __rhs.size()) return false;
  528. return __lhs.compare(__rhs) == 0;
  529. }
  530. template<class _CharT, class _Traits>
  531. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  532. bool operator==(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
  533. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  534. {
  535. if ( __lhs.size() != __rhs.size()) return false;
  536. return __lhs.compare(__rhs) == 0;
  537. }
  538. // operator !=
  539. template<class _CharT, class _Traits>
  540. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  541. bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  542. {
  543. if ( __lhs.size() != __rhs.size())
  544. return true;
  545. return __lhs.compare(__rhs) != 0;
  546. }
  547. template<class _CharT, class _Traits>
  548. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  549. bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
  550. typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
  551. {
  552. if ( __lhs.size() != __rhs.size())
  553. return true;
  554. return __lhs.compare(__rhs) != 0;
  555. }
  556. template<class _CharT, class _Traits>
  557. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  558. bool operator!=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
  559. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  560. {
  561. if ( __lhs.size() != __rhs.size())
  562. return true;
  563. return __lhs.compare(__rhs) != 0;
  564. }
  565. // operator <
  566. template<class _CharT, class _Traits>
  567. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  568. bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  569. {
  570. return __lhs.compare(__rhs) < 0;
  571. }
  572. template<class _CharT, class _Traits>
  573. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  574. bool operator<(basic_string_view<_CharT, _Traits> __lhs,
  575. typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
  576. {
  577. return __lhs.compare(__rhs) < 0;
  578. }
  579. template<class _CharT, class _Traits>
  580. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  581. bool operator<(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
  582. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  583. {
  584. return __lhs.compare(__rhs) < 0;
  585. }
  586. // operator >
  587. template<class _CharT, class _Traits>
  588. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  589. bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  590. {
  591. return __lhs.compare(__rhs) > 0;
  592. }
  593. template<class _CharT, class _Traits>
  594. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  595. bool operator>(basic_string_view<_CharT, _Traits> __lhs,
  596. typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
  597. {
  598. return __lhs.compare(__rhs) > 0;
  599. }
  600. template<class _CharT, class _Traits>
  601. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  602. bool operator>(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
  603. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  604. {
  605. return __lhs.compare(__rhs) > 0;
  606. }
  607. // operator <=
  608. template<class _CharT, class _Traits>
  609. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  610. bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  611. {
  612. return __lhs.compare(__rhs) <= 0;
  613. }
  614. template<class _CharT, class _Traits>
  615. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  616. bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
  617. typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
  618. {
  619. return __lhs.compare(__rhs) <= 0;
  620. }
  621. template<class _CharT, class _Traits>
  622. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  623. bool operator<=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
  624. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  625. {
  626. return __lhs.compare(__rhs) <= 0;
  627. }
  628. // operator >=
  629. template<class _CharT, class _Traits>
  630. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  631. bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  632. {
  633. return __lhs.compare(__rhs) >= 0;
  634. }
  635. template<class _CharT, class _Traits>
  636. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  637. bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
  638. typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
  639. {
  640. return __lhs.compare(__rhs) >= 0;
  641. }
  642. template<class _CharT, class _Traits>
  643. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  644. bool operator>=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
  645. basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
  646. {
  647. return __lhs.compare(__rhs) >= 0;
  648. }
  649. // [string.view.io]
  650. template<class _CharT, class _Traits>
  651. basic_ostream<_CharT, _Traits>&
  652. operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv)
  653. {
  654. return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
  655. }
  656. typedef basic_string_view<char> string_view;
  657. typedef basic_string_view<char16_t> u16string_view;
  658. typedef basic_string_view<char32_t> u32string_view;
  659. typedef basic_string_view<wchar_t> wstring_view;
  660. _LIBCPP_END_NAMESPACE_LFTS
  661. _LIBCPP_BEGIN_NAMESPACE_STD
  662. // [string.view.hash]
  663. // Shamelessly stolen from <string>
  664. template<class _CharT, class _Traits>
  665. struct _LIBCPP_TYPE_VIS_ONLY hash<std::experimental::basic_string_view<_CharT, _Traits> >
  666. : public unary_function<std::experimental::basic_string_view<_CharT, _Traits>, size_t>
  667. {
  668. size_t operator()(const std::experimental::basic_string_view<_CharT, _Traits>& __val) const _NOEXCEPT;
  669. };
  670. template<class _CharT, class _Traits>
  671. size_t
  672. hash<std::experimental::basic_string_view<_CharT, _Traits> >::operator()(
  673. const std::experimental::basic_string_view<_CharT, _Traits>& __val) const _NOEXCEPT
  674. {
  675. return __do_string_hash(__val.data(), __val.data() + __val.size());
  676. }
  677. #if _LIBCPP_STD_VER > 11
  678. template <class _CharT, class _Traits>
  679. __quoted_output_proxy<_CharT, const _CharT *, _Traits>
  680. quoted ( std::experimental::basic_string_view <_CharT, _Traits> __sv,
  681. _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
  682. {
  683. return __quoted_output_proxy<_CharT, const _CharT *, _Traits>
  684. ( __sv.data(), __sv.data() + __sv.size(), __delim, __escape );
  685. }
  686. #endif
  687. _LIBCPP_END_NAMESPACE_STD
  688. #endif // _LIBCPP_LFTS_STRING_VIEW