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