string_view.hpp 30 KB

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