string_view.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. Copyright (c) Marshall Clow 2012-2015.
  3. Copyright (c) Beman Dawes 2015
  4. Copyright (c) Glen Joseph Fernandes 2019 (glenjofe@gmail.com)
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. For more information, see http://www.boost.org
  8. Based on the StringRef implementation in LLVM (http://llvm.org) and
  9. N3422 by Jeffrey Yasskin
  10. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
  11. Updated July 2015 to reflect the Library Fundamentals TS
  12. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4480.html
  13. */
  14. #ifndef BOOST_STRING_VIEW_HPP
  15. #define BOOST_STRING_VIEW_HPP
  16. #include <boost/config.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/utility/ostream_string.hpp>
  19. #include <boost/utility/string_view_fwd.hpp>
  20. #include <boost/throw_exception.hpp>
  21. #include <boost/container_hash/hash_fwd.hpp>
  22. #include <cstddef>
  23. #include <stdexcept>
  24. #include <algorithm>
  25. #include <iterator>
  26. #include <string>
  27. #include <cstring>
  28. #include <iosfwd>
  29. #if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || (defined(BOOST_GCC) && ((BOOST_GCC+0) / 100) <= 406)
  30. // GCC 4.6 cannot handle a defaulted function with noexcept specifier
  31. #define BOOST_STRING_VIEW_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
  32. #endif
  33. namespace boost {
  34. namespace detail {
  35. // A helper functor because sometimes we don't have lambdas
  36. template <typename charT, typename traits>
  37. class string_view_traits_eq {
  38. public:
  39. string_view_traits_eq ( charT ch ) : ch_(ch) {}
  40. bool operator()( charT val ) const { return traits::eq (ch_, val); }
  41. charT ch_;
  42. };
  43. }
  44. template<typename charT, typename traits> // traits defaulted in string_view_fwd.hpp
  45. class basic_string_view {
  46. public:
  47. // types
  48. typedef traits traits_type;
  49. typedef charT value_type;
  50. typedef charT* pointer;
  51. typedef const charT* const_pointer;
  52. typedef charT& reference;
  53. typedef const charT& const_reference;
  54. typedef const_pointer const_iterator; // impl-defined
  55. typedef const_iterator iterator;
  56. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  57. typedef const_reverse_iterator reverse_iterator;
  58. typedef std::size_t size_type;
  59. typedef std::ptrdiff_t difference_type;
  60. static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1);
  61. // construct/copy
  62. BOOST_CONSTEXPR basic_string_view() BOOST_NOEXCEPT
  63. : ptr_(NULL), len_(0) {}
  64. // by defaulting these functions, basic_string_ref becomes
  65. // trivially copy/move constructible.
  66. BOOST_CONSTEXPR basic_string_view(const basic_string_view &rhs) BOOST_NOEXCEPT
  67. #ifndef BOOST_STRING_VIEW_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
  68. = default;
  69. #else
  70. : ptr_(rhs.ptr_), len_(rhs.len_) {}
  71. #endif
  72. basic_string_view& operator=(const basic_string_view &rhs) BOOST_NOEXCEPT
  73. #ifndef BOOST_STRING_VIEW_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
  74. = default;
  75. #else
  76. {
  77. ptr_ = rhs.ptr_;
  78. len_ = rhs.len_;
  79. return *this;
  80. }
  81. #endif
  82. template<typename Allocator>
  83. basic_string_view(const std::basic_string<charT, traits, Allocator>& str) BOOST_NOEXCEPT
  84. : ptr_(str.data()), len_(str.length()) {}
  85. // #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
  86. // // Constructing a string_view from a temporary string is a bad idea
  87. // template<typename Allocator>
  88. // basic_string_view( std::basic_string<charT, traits, Allocator>&&)
  89. // = delete;
  90. // #endif
  91. BOOST_CONSTEXPR basic_string_view(const charT* str)
  92. : ptr_(str), len_(traits::length(str)) {}
  93. BOOST_CONSTEXPR basic_string_view(const charT* str, size_type len)
  94. : ptr_(str), len_(len) {}
  95. // iterators
  96. BOOST_CONSTEXPR const_iterator begin() const BOOST_NOEXCEPT { return ptr_; }
  97. BOOST_CONSTEXPR const_iterator cbegin() const BOOST_NOEXCEPT { return ptr_; }
  98. BOOST_CONSTEXPR const_iterator end() const BOOST_NOEXCEPT { return ptr_ + len_; }
  99. BOOST_CONSTEXPR const_iterator cend() const BOOST_NOEXCEPT { return ptr_ + len_; }
  100. const_reverse_iterator rbegin() const BOOST_NOEXCEPT { return const_reverse_iterator(end()); }
  101. const_reverse_iterator crbegin() const BOOST_NOEXCEPT { return const_reverse_iterator(end()); }
  102. const_reverse_iterator rend() const BOOST_NOEXCEPT { return const_reverse_iterator(begin()); }
  103. const_reverse_iterator crend() const BOOST_NOEXCEPT { return const_reverse_iterator(begin()); }
  104. // capacity
  105. BOOST_CONSTEXPR size_type size() const BOOST_NOEXCEPT { return len_; }
  106. BOOST_CONSTEXPR size_type length() const BOOST_NOEXCEPT { return len_; }
  107. BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT { return len_; }
  108. BOOST_CONSTEXPR bool empty() const BOOST_NOEXCEPT { return len_ == 0; }
  109. // element access
  110. BOOST_CONSTEXPR const_reference operator[](size_type pos) const BOOST_NOEXCEPT { return ptr_[pos]; }
  111. BOOST_CONSTEXPR const_reference at(size_t pos) const {
  112. return pos >= len_ ? BOOST_THROW_EXCEPTION(std::out_of_range("boost::string_view::at")), ptr_[0] : ptr_[pos];
  113. }
  114. BOOST_CONSTEXPR const_reference front() const { return ptr_[0]; }
  115. BOOST_CONSTEXPR const_reference back() const { return ptr_[len_-1]; }
  116. BOOST_CONSTEXPR const_pointer data() const BOOST_NOEXCEPT { return ptr_; }
  117. // modifiers
  118. void clear() BOOST_NOEXCEPT { len_ = 0; } // Boost extension
  119. BOOST_CXX14_CONSTEXPR void remove_prefix(size_type n) {
  120. if ( n > len_ )
  121. n = len_;
  122. ptr_ += n;
  123. len_ -= n;
  124. }
  125. BOOST_CXX14_CONSTEXPR void remove_suffix(size_type n) {
  126. if ( n > len_ )
  127. n = len_;
  128. len_ -= n;
  129. }
  130. BOOST_CXX14_CONSTEXPR void swap(basic_string_view& s) BOOST_NOEXCEPT {
  131. std::swap(ptr_, s.ptr_);
  132. std::swap(len_, s.len_);
  133. }
  134. // basic_string_view string operations
  135. #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  136. template<typename Allocator>
  137. explicit operator std::basic_string<charT, traits, Allocator>() const {
  138. return std::basic_string<charT, traits, Allocator>(begin(), end());
  139. }
  140. #endif
  141. #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
  142. template<typename Allocator = std::allocator<charT> >
  143. std::basic_string<charT, traits, Allocator> to_string(const Allocator& a = Allocator()) const {
  144. return std::basic_string<charT, traits, Allocator>(begin(), end(), a);
  145. }
  146. #else
  147. std::basic_string<charT, traits> to_string() const {
  148. return std::basic_string<charT, traits>(begin(), end());
  149. }
  150. template<typename Allocator>
  151. std::basic_string<charT, traits, Allocator> to_string(const Allocator& a) const {
  152. return std::basic_string<charT, traits, Allocator>(begin(), end(), a);
  153. }
  154. #endif
  155. size_type copy(charT* s, size_type n, size_type pos=0) const {
  156. if (pos > size())
  157. BOOST_THROW_EXCEPTION(std::out_of_range("string_view::copy" ));
  158. size_type rlen = (std::min)(n, len_ - pos);
  159. traits_type::copy(s, data() + pos, rlen);
  160. return rlen;
  161. }
  162. BOOST_CXX14_CONSTEXPR basic_string_view substr(size_type pos, size_type n=npos) const {
  163. if ( pos > size())
  164. BOOST_THROW_EXCEPTION( std::out_of_range ( "string_view::substr" ) );
  165. return basic_string_view(data() + pos, (std::min)(size() - pos, n));
  166. }
  167. BOOST_CXX14_CONSTEXPR int compare(basic_string_view x) const BOOST_NOEXCEPT {
  168. const int cmp = traits::compare(ptr_, x.ptr_, (std::min)(len_, x.len_));
  169. return cmp != 0 ? cmp : (len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1);
  170. }
  171. BOOST_CXX14_CONSTEXPR int compare(size_type pos1, size_type n1, basic_string_view x)
  172. const BOOST_NOEXCEPT {
  173. return substr(pos1, n1).compare(x);
  174. }
  175. BOOST_CXX14_CONSTEXPR int compare(size_type pos1, size_type n1,
  176. basic_string_view x, size_type pos2, size_type n2) const {
  177. return substr(pos1, n1).compare(x.substr(pos2, n2));
  178. }
  179. BOOST_CXX14_CONSTEXPR int compare(const charT* x) const {
  180. return compare(basic_string_view(x));
  181. }
  182. BOOST_CXX14_CONSTEXPR int compare(size_type pos1, size_type n1, const charT* x) const {
  183. return substr(pos1, n1).compare(basic_string_view(x));
  184. }
  185. BOOST_CXX14_CONSTEXPR int compare(size_type pos1, size_type n1,
  186. const charT* x, size_type n2) const {
  187. return substr(pos1, n1).compare(basic_string_view(x, n2));
  188. }
  189. // Searches
  190. BOOST_CONSTEXPR bool starts_with(charT c) const BOOST_NOEXCEPT { // Boost extension
  191. return !empty() && traits::eq(c, front());
  192. }
  193. BOOST_CONSTEXPR bool starts_with(basic_string_view x) const BOOST_NOEXCEPT { // Boost extension
  194. return len_ >= x.len_ && traits::compare(ptr_, x.ptr_, x.len_) == 0;
  195. }
  196. BOOST_CONSTEXPR bool ends_with(charT c) const BOOST_NOEXCEPT { // Boost extension
  197. return !empty() && traits::eq(c, back());
  198. }
  199. BOOST_CONSTEXPR bool ends_with(basic_string_view x) const BOOST_NOEXCEPT { // Boost extension
  200. return len_ >= x.len_ &&
  201. traits::compare(ptr_ + len_ - x.len_, x.ptr_, x.len_) == 0;
  202. }
  203. // find
  204. BOOST_CXX14_CONSTEXPR size_type find(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT {
  205. if (pos > size())
  206. return npos;
  207. if (s.empty())
  208. return pos;
  209. if (s.size() > size() - pos)
  210. return npos;
  211. const charT* cur = ptr_ + pos;
  212. const charT* last = cend() - s.size() + 1;
  213. for (; cur != last ; ++cur) {
  214. cur = traits::find(cur, last - cur, s[0]);
  215. if (!cur)
  216. return npos;
  217. if (traits::compare(cur, s.cbegin(), s.size()) == 0)
  218. return cur - ptr_;
  219. }
  220. return npos;
  221. }
  222. BOOST_CXX14_CONSTEXPR size_type find(charT c, size_type pos = 0) const BOOST_NOEXCEPT {
  223. if (pos > size())
  224. return npos;
  225. const charT* ret_ptr = traits::find(ptr_ + pos, len_ - pos, c);
  226. if (ret_ptr)
  227. return ret_ptr - ptr_;
  228. return npos;
  229. }
  230. BOOST_CXX14_CONSTEXPR size_type find(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
  231. { return find(basic_string_view(s, n), pos); }
  232. BOOST_CXX14_CONSTEXPR size_type find(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT
  233. { return find(basic_string_view(s), pos); }
  234. // rfind
  235. BOOST_CXX14_CONSTEXPR size_type rfind(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
  236. if (len_ < s.len_)
  237. return npos;
  238. if (pos > len_ - s.len_)
  239. pos = len_ - s.len_;
  240. if (s.len_ == 0u) // an empty string is always found
  241. return pos;
  242. for (const charT* cur = ptr_ + pos; ; --cur) {
  243. if (traits::compare(cur, s.ptr_, s.len_) == 0)
  244. return cur - ptr_;
  245. if (cur == ptr_)
  246. return npos;
  247. };
  248. }
  249. BOOST_CXX14_CONSTEXPR size_type rfind(charT c, size_type pos = npos) const BOOST_NOEXCEPT
  250. { return rfind(basic_string_view(&c, 1), pos); }
  251. BOOST_CXX14_CONSTEXPR size_type rfind(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
  252. { return rfind(basic_string_view(s, n), pos); }
  253. BOOST_CXX14_CONSTEXPR size_type rfind(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT
  254. { return rfind(basic_string_view(s), pos); }
  255. // find_first_of
  256. BOOST_CXX14_CONSTEXPR size_type find_first_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT {
  257. if (pos >= len_ || s.len_ == 0)
  258. return npos;
  259. const_iterator iter = std::find_first_of
  260. (this->cbegin () + pos, this->cend (), s.cbegin (), s.cend (), traits::eq);
  261. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  262. }
  263. BOOST_CXX14_CONSTEXPR size_type find_first_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT
  264. { return find(c, pos); }
  265. BOOST_CXX14_CONSTEXPR size_type find_first_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
  266. { return find_first_of(basic_string_view(s, n), pos); }
  267. BOOST_CXX14_CONSTEXPR size_type find_first_of(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT
  268. { return find_first_of(basic_string_view(s), pos); }
  269. // find_last_of
  270. BOOST_CXX14_CONSTEXPR size_type find_last_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
  271. if (s.len_ == 0u)
  272. return npos;
  273. if (pos >= len_)
  274. pos = 0;
  275. else
  276. pos = len_ - (pos+1);
  277. const_reverse_iterator iter = std::find_first_of
  278. ( this->crbegin () + pos, this->crend (), s.cbegin (), s.cend (), traits::eq );
  279. return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter);
  280. }
  281. BOOST_CXX14_CONSTEXPR size_type find_last_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT
  282. { return find_last_of(basic_string_view(&c, 1), pos); }
  283. BOOST_CXX14_CONSTEXPR size_type find_last_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
  284. { return find_last_of(basic_string_view(s, n), pos); }
  285. BOOST_CXX14_CONSTEXPR size_type find_last_of(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT
  286. { return find_last_of(basic_string_view(s), pos); }
  287. // find_first_not_of
  288. BOOST_CXX14_CONSTEXPR size_type find_first_not_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT {
  289. if (pos >= len_)
  290. return npos;
  291. if (s.len_ == 0)
  292. return pos;
  293. const_iterator iter = find_not_of ( this->cbegin () + pos, this->cend (), s );
  294. return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
  295. }
  296. BOOST_CXX14_CONSTEXPR size_type find_first_not_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT
  297. { return find_first_not_of(basic_string_view(&c, 1), pos); }
  298. BOOST_CXX14_CONSTEXPR size_type find_first_not_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
  299. { return find_first_not_of(basic_string_view(s, n), pos); }
  300. BOOST_CXX14_CONSTEXPR size_type find_first_not_of(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT
  301. { return find_first_not_of(basic_string_view(s), pos); }
  302. // find_last_not_of
  303. BOOST_CXX14_CONSTEXPR size_type find_last_not_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
  304. if (pos >= len_)
  305. pos = len_ - 1;
  306. if (s.len_ == 0u)
  307. return pos;
  308. pos = len_ - (pos+1);
  309. const_reverse_iterator iter = find_not_of ( this->crbegin () + pos, this->crend (), s );
  310. return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
  311. }
  312. BOOST_CXX14_CONSTEXPR size_type find_last_not_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT
  313. { return find_last_not_of(basic_string_view(&c, 1), pos); }
  314. BOOST_CXX14_CONSTEXPR size_type find_last_not_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
  315. { return find_last_not_of(basic_string_view(s, n), pos); }
  316. BOOST_CXX14_CONSTEXPR size_type find_last_not_of(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT
  317. { return find_last_not_of(basic_string_view(s), pos); }
  318. private:
  319. template <typename r_iter>
  320. size_type reverse_distance(r_iter first, r_iter last) const BOOST_NOEXCEPT {
  321. // Portability note here: std::distance is not NOEXCEPT, but calling it with a string_view::reverse_iterator will not throw.
  322. return len_ - 1 - std::distance ( first, last );
  323. }
  324. template <typename Iterator>
  325. Iterator find_not_of(Iterator first, Iterator last, basic_string_view s) const BOOST_NOEXCEPT {
  326. for (; first != last ; ++first)
  327. if ( 0 == traits::find(s.ptr_, s.len_, *first))
  328. return first;
  329. return last;
  330. }
  331. const charT *ptr_;
  332. std::size_t len_;
  333. };
  334. // Comparison operators
  335. // Equality
  336. template<typename charT, typename traits>
  337. inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x,
  338. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  339. if (x.size () != y.size ()) return false;
  340. return x.compare(y) == 0;
  341. }
  342. // Inequality
  343. template<typename charT, typename traits>
  344. inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x,
  345. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  346. if ( x.size () != y.size ()) return true;
  347. return x.compare(y) != 0;
  348. }
  349. // Less than
  350. template<typename charT, typename traits>
  351. inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x,
  352. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  353. return x.compare(y) < 0;
  354. }
  355. // Greater than
  356. template<typename charT, typename traits>
  357. inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x,
  358. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  359. return x.compare(y) > 0;
  360. }
  361. // Less than or equal to
  362. template<typename charT, typename traits>
  363. inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x,
  364. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  365. return x.compare(y) <= 0;
  366. }
  367. // Greater than or equal to
  368. template<typename charT, typename traits>
  369. inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x,
  370. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  371. return x.compare(y) >= 0;
  372. }
  373. // "sufficient additional overloads of comparison functions"
  374. template<typename charT, typename traits, typename Allocator>
  375. inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x,
  376. const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
  377. return x == basic_string_view<charT, traits>(y);
  378. }
  379. template<typename charT, typename traits, typename Allocator>
  380. inline BOOST_CXX14_CONSTEXPR bool operator==(const std::basic_string<charT, traits, Allocator> & x,
  381. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  382. return basic_string_view<charT, traits>(x) == y;
  383. }
  384. template<typename charT, typename traits>
  385. inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x,
  386. const charT * y) BOOST_NOEXCEPT {
  387. return x == basic_string_view<charT, traits>(y);
  388. }
  389. template<typename charT, typename traits>
  390. inline BOOST_CXX14_CONSTEXPR bool operator==(const charT * x,
  391. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  392. return basic_string_view<charT, traits>(x) == y;
  393. }
  394. template<typename charT, typename traits, typename Allocator>
  395. inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x,
  396. const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
  397. return x != basic_string_view<charT, traits>(y);
  398. }
  399. template<typename charT, typename traits, typename Allocator>
  400. inline BOOST_CXX14_CONSTEXPR bool operator!=(const std::basic_string<charT, traits, Allocator> & x,
  401. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  402. return basic_string_view<charT, traits>(x) != y;
  403. }
  404. template<typename charT, typename traits>
  405. inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x,
  406. const charT * y) BOOST_NOEXCEPT {
  407. return x != basic_string_view<charT, traits>(y);
  408. }
  409. template<typename charT, typename traits>
  410. inline BOOST_CXX14_CONSTEXPR bool operator!=(const charT * x,
  411. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  412. return basic_string_view<charT, traits>(x) != y;
  413. }
  414. template<typename charT, typename traits, typename Allocator>
  415. inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x,
  416. const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
  417. return x < basic_string_view<charT, traits>(y);
  418. }
  419. template<typename charT, typename traits, typename Allocator>
  420. inline BOOST_CXX14_CONSTEXPR bool operator<(const std::basic_string<charT, traits, Allocator> & x,
  421. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  422. return basic_string_view<charT, traits>(x) < y;
  423. }
  424. template<typename charT, typename traits>
  425. inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x,
  426. const charT * y) BOOST_NOEXCEPT {
  427. return x < basic_string_view<charT, traits>(y);
  428. }
  429. template<typename charT, typename traits>
  430. inline BOOST_CXX14_CONSTEXPR bool operator<(const charT * x,
  431. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  432. return basic_string_view<charT, traits>(x) < y;
  433. }
  434. template<typename charT, typename traits, typename Allocator>
  435. inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x,
  436. const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
  437. return x > basic_string_view<charT, traits>(y);
  438. }
  439. template<typename charT, typename traits, typename Allocator>
  440. inline BOOST_CXX14_CONSTEXPR bool operator>(const std::basic_string<charT, traits, Allocator> & x,
  441. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  442. return basic_string_view<charT, traits>(x) > y;
  443. }
  444. template<typename charT, typename traits>
  445. inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x,
  446. const charT * y) BOOST_NOEXCEPT {
  447. return x > basic_string_view<charT, traits>(y);
  448. }
  449. template<typename charT, typename traits>
  450. inline BOOST_CXX14_CONSTEXPR bool operator>(const charT * x,
  451. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  452. return basic_string_view<charT, traits>(x) > y;
  453. }
  454. template<typename charT, typename traits, typename Allocator>
  455. inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x,
  456. const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
  457. return x <= basic_string_view<charT, traits>(y);
  458. }
  459. template<typename charT, typename traits, typename Allocator>
  460. inline BOOST_CXX14_CONSTEXPR bool operator<=(const std::basic_string<charT, traits, Allocator> & x,
  461. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  462. return basic_string_view<charT, traits>(x) <= y;
  463. }
  464. template<typename charT, typename traits>
  465. inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x,
  466. const charT * y) BOOST_NOEXCEPT {
  467. return x <= basic_string_view<charT, traits>(y);
  468. }
  469. template<typename charT, typename traits>
  470. inline BOOST_CXX14_CONSTEXPR bool operator<=(const charT * x,
  471. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  472. return basic_string_view<charT, traits>(x) <= y;
  473. }
  474. template<typename charT, typename traits, typename Allocator>
  475. inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x,
  476. const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
  477. return x >= basic_string_view<charT, traits>(y);
  478. }
  479. template<typename charT, typename traits, typename Allocator>
  480. inline BOOST_CXX14_CONSTEXPR bool operator>=(const std::basic_string<charT, traits, Allocator> & x,
  481. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  482. return basic_string_view<charT, traits>(x) >= y;
  483. }
  484. template<typename charT, typename traits>
  485. inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x,
  486. const charT * y) BOOST_NOEXCEPT {
  487. return x >= basic_string_view<charT, traits>(y);
  488. }
  489. template<typename charT, typename traits>
  490. inline BOOST_CXX14_CONSTEXPR bool operator>=(const charT * x,
  491. basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
  492. return basic_string_view<charT, traits>(x) >= y;
  493. }
  494. // Inserter
  495. template<class charT, class traits>
  496. inline std::basic_ostream<charT, traits>&
  497. operator<<(std::basic_ostream<charT, traits>& os,
  498. const basic_string_view<charT,traits>& str) {
  499. return boost::ostream_string(os, str.data(), str.size());
  500. }
  501. #if 0
  502. // numeric conversions
  503. //
  504. // These are short-term implementations.
  505. // In a production environment, I would rather avoid the copying.
  506. //
  507. inline int stoi (string_view str, size_t* idx=0, int base=10) {
  508. return std::stoi ( std::string(str), idx, base );
  509. }
  510. inline long stol (string_view str, size_t* idx=0, int base=10) {
  511. return std::stol ( std::string(str), idx, base );
  512. }
  513. inline unsigned long stoul (string_view str, size_t* idx=0, int base=10) {
  514. return std::stoul ( std::string(str), idx, base );
  515. }
  516. inline long long stoll (string_view str, size_t* idx=0, int base=10) {
  517. return std::stoll ( std::string(str), idx, base );
  518. }
  519. inline unsigned long long stoull (string_view str, size_t* idx=0, int base=10) {
  520. return std::stoull ( std::string(str), idx, base );
  521. }
  522. inline float stof (string_view str, size_t* idx=0) {
  523. return std::stof ( std::string(str), idx );
  524. }
  525. inline double stod (string_view str, size_t* idx=0) {
  526. return std::stod ( std::string(str), idx );
  527. }
  528. inline long double stold (string_view str, size_t* idx=0) {
  529. return std::stold ( std::string(str), idx );
  530. }
  531. inline int stoi (wstring_view str, size_t* idx=0, int base=10) {
  532. return std::stoi ( std::wstring(str), idx, base );
  533. }
  534. inline long stol (wstring_view str, size_t* idx=0, int base=10) {
  535. return std::stol ( std::wstring(str), idx, base );
  536. }
  537. inline unsigned long stoul (wstring_view str, size_t* idx=0, int base=10) {
  538. return std::stoul ( std::wstring(str), idx, base );
  539. }
  540. inline long long stoll (wstring_view str, size_t* idx=0, int base=10) {
  541. return std::stoll ( std::wstring(str), idx, base );
  542. }
  543. inline unsigned long long stoull (wstring_view str, size_t* idx=0, int base=10) {
  544. return std::stoull ( std::wstring(str), idx, base );
  545. }
  546. inline float stof (wstring_view str, size_t* idx=0) {
  547. return std::stof ( std::wstring(str), idx );
  548. }
  549. inline double stod (wstring_view str, size_t* idx=0) {
  550. return std::stod ( std::wstring(str), idx );
  551. }
  552. inline long double stold (wstring_view str, size_t* idx=0) {
  553. return std::stold ( std::wstring(str), idx );
  554. }
  555. #endif
  556. template <class charT, class traits>
  557. std::size_t hash_value(basic_string_view<charT, traits> s) {
  558. return boost::hash_range(s.begin(), s.end());
  559. }
  560. }
  561. #if 0
  562. namespace std {
  563. // Hashing
  564. template<> struct hash<boost::string_view>;
  565. template<> struct hash<boost::u16string_view>;
  566. template<> struct hash<boost::u32string_view>;
  567. template<> struct hash<boost::wstring_view>;
  568. }
  569. #endif
  570. #endif