string_view.hpp 29 KB

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