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