]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | /* |
b32b8144 FG |
2 | Copyright (c) Marshall Clow 2012-2015. |
3 | Copyright (c) Beman Dawes 2015 | |
7c673cae FG |
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 | ||
38 | namespace 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> | |
b32b8144 FG |
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 | |
7c673cae FG |
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 { | |
b32b8144 | 130 | return pos >= len_ ? BOOST_THROW_EXCEPTION(std::out_of_range("boost::string_view::at")), ptr_[0] : ptr_[pos]; |
7c673cae FG |
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); | |
b32b8144 | 186 | traits_type::copy(s, data() + pos, rlen); |
7c673cae FG |
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" ) ); | |
b32b8144 | 193 | return basic_string_view(data() + pos, (std::min)(size() - pos, n)); |
7c673cae FG |
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 | const_iterator iter = std::search(this->cbegin() + pos, this->cend(), | |
249 | s.cbegin (), s.cend (), traits::eq); | |
250 | return iter == this->cend () ? npos : std::distance(this->cbegin (), iter); | |
251 | } | |
252 | BOOST_CXX14_CONSTEXPR size_type find(charT c, size_type pos = 0) const BOOST_NOEXCEPT | |
253 | { return find(basic_string_view(&c, 1), pos); } | |
254 | BOOST_CXX14_CONSTEXPR size_type find(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT | |
255 | { return find(basic_string_view(s, n), pos); } | |
256 | BOOST_CXX14_CONSTEXPR size_type find(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT | |
257 | { return find(basic_string_view(s), pos); } | |
258 | ||
259 | // rfind | |
260 | BOOST_CXX14_CONSTEXPR size_type rfind(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT { | |
261 | if (len_ < s.len_) | |
262 | return npos; | |
263 | if (pos > len_ - s.len_) | |
264 | pos = len_ - s.len_; | |
265 | if (s.len_ == 0u) // an empty string is always found | |
266 | return pos; | |
267 | for (const charT* cur = ptr_ + pos; ; --cur) { | |
268 | if (traits::compare(cur, s.ptr_, s.len_) == 0) | |
269 | return cur - ptr_; | |
270 | if (cur == ptr_) | |
271 | return npos; | |
272 | }; | |
273 | } | |
274 | BOOST_CXX14_CONSTEXPR size_type rfind(charT c, size_type pos = npos) const BOOST_NOEXCEPT | |
275 | { return rfind(basic_string_view(&c, 1), pos); } | |
276 | BOOST_CXX14_CONSTEXPR size_type rfind(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT | |
277 | { return rfind(basic_string_view(s, n), pos); } | |
278 | BOOST_CXX14_CONSTEXPR size_type rfind(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT | |
279 | { return rfind(basic_string_view(s), pos); } | |
280 | ||
281 | // find_first_of | |
282 | BOOST_CXX14_CONSTEXPR size_type find_first_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT { | |
283 | if (pos >= len_ || s.len_ == 0) | |
284 | return npos; | |
285 | const_iterator iter = std::find_first_of | |
286 | (this->cbegin () + pos, this->cend (), s.cbegin (), s.cend (), traits::eq); | |
287 | return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); | |
288 | } | |
289 | BOOST_CXX14_CONSTEXPR size_type find_first_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT | |
290 | { return find_first_of(basic_string_view(&c, 1), pos); } | |
291 | BOOST_CXX14_CONSTEXPR size_type find_first_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT | |
292 | { return find_first_of(basic_string_view(s, n), pos); } | |
293 | BOOST_CXX14_CONSTEXPR size_type find_first_of(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT | |
294 | { return find_first_of(basic_string_view(s), pos); } | |
295 | ||
296 | // find_last_of | |
297 | BOOST_CXX14_CONSTEXPR size_type find_last_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT { | |
298 | if (s.len_ == 0u) | |
299 | return npos; | |
300 | if (pos >= len_) | |
301 | pos = 0; | |
302 | else | |
303 | pos = len_ - (pos+1); | |
304 | const_reverse_iterator iter = std::find_first_of | |
305 | ( this->crbegin () + pos, this->crend (), s.cbegin (), s.cend (), traits::eq ); | |
306 | return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter); | |
307 | } | |
308 | BOOST_CXX14_CONSTEXPR size_type find_last_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT | |
309 | { return find_last_of(basic_string_view(&c, 1), pos); } | |
310 | BOOST_CXX14_CONSTEXPR size_type find_last_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT | |
311 | { return find_last_of(basic_string_view(s, n), pos); } | |
312 | BOOST_CXX14_CONSTEXPR size_type find_last_of(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT | |
313 | { return find_last_of(basic_string_view(s), pos); } | |
314 | ||
315 | // find_first_not_of | |
316 | BOOST_CXX14_CONSTEXPR size_type find_first_not_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT { | |
317 | if (pos >= len_) | |
318 | return npos; | |
319 | if (s.len_ == 0) | |
320 | return pos; | |
321 | const_iterator iter = find_not_of ( this->cbegin () + pos, this->cend (), s ); | |
322 | return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); | |
323 | } | |
324 | BOOST_CXX14_CONSTEXPR size_type find_first_not_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT | |
325 | { return find_first_not_of(basic_string_view(&c, 1), pos); } | |
326 | BOOST_CXX14_CONSTEXPR size_type find_first_not_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT | |
327 | { return find_first_not_of(basic_string_view(s, n), pos); } | |
328 | BOOST_CXX14_CONSTEXPR size_type find_first_not_of(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT | |
329 | { return find_first_not_of(basic_string_view(s), pos); } | |
330 | ||
331 | // find_last_not_of | |
332 | BOOST_CXX14_CONSTEXPR size_type find_last_not_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT { | |
333 | if (pos >= len_) | |
334 | pos = len_ - 1; | |
335 | if (s.len_ == 0u) | |
336 | return pos; | |
337 | pos = len_ - (pos+1); | |
338 | const_reverse_iterator iter = find_not_of ( this->crbegin () + pos, this->crend (), s ); | |
339 | return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); | |
340 | } | |
341 | BOOST_CXX14_CONSTEXPR size_type find_last_not_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT | |
342 | { return find_last_not_of(basic_string_view(&c, 1), pos); } | |
343 | BOOST_CXX14_CONSTEXPR size_type find_last_not_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT | |
344 | { return find_last_not_of(basic_string_view(s, n), pos); } | |
345 | BOOST_CXX14_CONSTEXPR size_type find_last_not_of(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT | |
346 | { return find_last_not_of(basic_string_view(s), pos); } | |
347 | ||
348 | private: | |
349 | template <typename r_iter> | |
350 | size_type reverse_distance(r_iter first, r_iter last) const BOOST_NOEXCEPT { | |
351 | // Portability note here: std::distance is not NOEXCEPT, but calling it with a string_view::reverse_iterator will not throw. | |
352 | return len_ - 1 - std::distance ( first, last ); | |
353 | } | |
354 | ||
355 | template <typename Iterator> | |
356 | Iterator find_not_of(Iterator first, Iterator last, basic_string_view s) const BOOST_NOEXCEPT { | |
357 | for (; first != last ; ++first) | |
358 | if ( 0 == traits::find(s.ptr_, s.len_, *first)) | |
359 | return first; | |
360 | return last; | |
361 | } | |
362 | ||
363 | const charT *ptr_; | |
364 | std::size_t len_; | |
365 | }; | |
366 | ||
367 | ||
368 | // Comparison operators | |
369 | // Equality | |
370 | template<typename charT, typename traits> | |
b32b8144 | 371 | inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x, |
7c673cae FG |
372 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
373 | if (x.size () != y.size ()) return false; | |
374 | return x.compare(y) == 0; | |
375 | } | |
376 | ||
377 | // Inequality | |
378 | template<typename charT, typename traits> | |
b32b8144 | 379 | inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x, |
7c673cae FG |
380 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
381 | if ( x.size () != y.size ()) return true; | |
382 | return x.compare(y) != 0; | |
383 | } | |
384 | ||
385 | // Less than | |
386 | template<typename charT, typename traits> | |
b32b8144 | 387 | inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x, |
7c673cae FG |
388 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
389 | return x.compare(y) < 0; | |
390 | } | |
391 | ||
392 | // Greater than | |
393 | template<typename charT, typename traits> | |
b32b8144 | 394 | inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x, |
7c673cae FG |
395 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
396 | return x.compare(y) > 0; | |
397 | } | |
398 | ||
399 | // Less than or equal to | |
400 | template<typename charT, typename traits> | |
b32b8144 | 401 | inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x, |
7c673cae FG |
402 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
403 | return x.compare(y) <= 0; | |
404 | } | |
405 | ||
406 | // Greater than or equal to | |
407 | template<typename charT, typename traits> | |
b32b8144 | 408 | inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x, |
7c673cae FG |
409 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
410 | return x.compare(y) >= 0; | |
411 | } | |
412 | ||
413 | // "sufficient additional overloads of comparison functions" | |
414 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 415 | inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x, |
7c673cae FG |
416 | const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { |
417 | return x == basic_string_view<charT, traits>(y); | |
418 | } | |
419 | ||
420 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 421 | inline BOOST_CXX14_CONSTEXPR bool operator==(const std::basic_string<charT, traits, Allocator> & x, |
7c673cae FG |
422 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
423 | return basic_string_view<charT, traits>(x) == y; | |
424 | } | |
425 | ||
426 | template<typename charT, typename traits> | |
b32b8144 | 427 | inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x, |
7c673cae FG |
428 | const charT * y) BOOST_NOEXCEPT { |
429 | return x == basic_string_view<charT, traits>(y); | |
430 | } | |
431 | ||
432 | template<typename charT, typename traits> | |
b32b8144 | 433 | inline BOOST_CXX14_CONSTEXPR bool operator==(const charT * x, |
7c673cae FG |
434 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
435 | return basic_string_view<charT, traits>(x) == y; | |
436 | } | |
437 | ||
438 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 439 | inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x, |
7c673cae FG |
440 | const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { |
441 | return x != basic_string_view<charT, traits>(y); | |
442 | } | |
443 | ||
444 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 445 | inline BOOST_CXX14_CONSTEXPR bool operator!=(const std::basic_string<charT, traits, Allocator> & x, |
7c673cae FG |
446 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
447 | return basic_string_view<charT, traits>(x) != y; | |
448 | } | |
449 | ||
450 | template<typename charT, typename traits> | |
b32b8144 | 451 | inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x, |
7c673cae FG |
452 | const charT * y) BOOST_NOEXCEPT { |
453 | return x != basic_string_view<charT, traits>(y); | |
454 | } | |
455 | ||
456 | template<typename charT, typename traits> | |
b32b8144 | 457 | inline BOOST_CXX14_CONSTEXPR bool operator!=(const charT * x, |
7c673cae FG |
458 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
459 | return basic_string_view<charT, traits>(x) != y; | |
460 | } | |
461 | ||
462 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 463 | inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x, |
7c673cae FG |
464 | const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { |
465 | return x < basic_string_view<charT, traits>(y); | |
466 | } | |
467 | ||
468 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 469 | inline BOOST_CXX14_CONSTEXPR bool operator<(const std::basic_string<charT, traits, Allocator> & x, |
7c673cae FG |
470 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
471 | return basic_string_view<charT, traits>(x) < y; | |
472 | } | |
473 | ||
474 | template<typename charT, typename traits> | |
b32b8144 | 475 | inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x, |
7c673cae FG |
476 | const charT * y) BOOST_NOEXCEPT { |
477 | return x < basic_string_view<charT, traits>(y); | |
478 | } | |
479 | ||
480 | template<typename charT, typename traits> | |
b32b8144 | 481 | inline BOOST_CXX14_CONSTEXPR bool operator<(const charT * x, |
7c673cae FG |
482 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
483 | return basic_string_view<charT, traits>(x) < y; | |
484 | } | |
485 | ||
486 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 487 | inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x, |
7c673cae FG |
488 | const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { |
489 | return x > basic_string_view<charT, traits>(y); | |
490 | } | |
491 | ||
492 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 493 | inline BOOST_CXX14_CONSTEXPR bool operator>(const std::basic_string<charT, traits, Allocator> & x, |
7c673cae FG |
494 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
495 | return basic_string_view<charT, traits>(x) > y; | |
496 | } | |
497 | ||
498 | template<typename charT, typename traits> | |
b32b8144 | 499 | inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x, |
7c673cae FG |
500 | const charT * y) BOOST_NOEXCEPT { |
501 | return x > basic_string_view<charT, traits>(y); | |
502 | } | |
503 | ||
504 | template<typename charT, typename traits> | |
b32b8144 | 505 | inline BOOST_CXX14_CONSTEXPR bool operator>(const charT * x, |
7c673cae FG |
506 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
507 | return basic_string_view<charT, traits>(x) > y; | |
508 | } | |
509 | ||
510 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 511 | inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x, |
7c673cae FG |
512 | const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { |
513 | return x <= basic_string_view<charT, traits>(y); | |
514 | } | |
515 | ||
516 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 517 | inline BOOST_CXX14_CONSTEXPR bool operator<=(const std::basic_string<charT, traits, Allocator> & x, |
7c673cae FG |
518 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
519 | return basic_string_view<charT, traits>(x) <= y; | |
520 | } | |
521 | ||
522 | template<typename charT, typename traits> | |
b32b8144 | 523 | inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x, |
7c673cae FG |
524 | const charT * y) BOOST_NOEXCEPT { |
525 | return x <= basic_string_view<charT, traits>(y); | |
526 | } | |
527 | ||
528 | template<typename charT, typename traits> | |
b32b8144 | 529 | inline BOOST_CXX14_CONSTEXPR bool operator<=(const charT * x, |
7c673cae FG |
530 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
531 | return basic_string_view<charT, traits>(x) <= y; | |
532 | } | |
533 | ||
534 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 535 | inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x, |
7c673cae FG |
536 | const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { |
537 | return x >= basic_string_view<charT, traits>(y); | |
538 | } | |
539 | ||
540 | template<typename charT, typename traits, typename Allocator> | |
b32b8144 | 541 | inline BOOST_CXX14_CONSTEXPR bool operator>=(const std::basic_string<charT, traits, Allocator> & x, |
7c673cae FG |
542 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
543 | return basic_string_view<charT, traits>(x) >= y; | |
544 | } | |
545 | ||
546 | template<typename charT, typename traits> | |
b32b8144 | 547 | inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x, |
7c673cae FG |
548 | const charT * y) BOOST_NOEXCEPT { |
549 | return x >= basic_string_view<charT, traits>(y); | |
550 | } | |
551 | ||
552 | template<typename charT, typename traits> | |
b32b8144 | 553 | inline BOOST_CXX14_CONSTEXPR bool operator>=(const charT * x, |
7c673cae FG |
554 | basic_string_view<charT, traits> y) BOOST_NOEXCEPT { |
555 | return basic_string_view<charT, traits>(x) >= y; | |
556 | } | |
557 | ||
558 | namespace detail { | |
559 | ||
560 | template<class charT, class traits> | |
561 | inline void sv_insert_fill_chars(std::basic_ostream<charT, traits>& os, std::size_t n) { | |
562 | enum { chunk_size = 8 }; | |
563 | charT fill_chars[chunk_size]; | |
564 | std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill()); | |
565 | for (; n >= chunk_size && os.good(); n -= chunk_size) | |
566 | os.write(fill_chars, static_cast< std::size_t >(chunk_size)); | |
567 | if (n > 0 && os.good()) | |
568 | os.write(fill_chars, n); | |
569 | } | |
570 | ||
571 | template<class charT, class traits> | |
572 | void sv_insert_aligned(std::basic_ostream<charT, traits>& os, const basic_string_view<charT,traits>& str) { | |
573 | const std::size_t size = str.size(); | |
574 | const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size; | |
575 | const bool align_left = (os.flags() & std::basic_ostream<charT, traits>::adjustfield) == std::basic_ostream<charT, traits>::left; | |
576 | if (!align_left) { | |
577 | detail::sv_insert_fill_chars(os, alignment_size); | |
578 | if (os.good()) | |
579 | os.write(str.data(), size); | |
580 | } | |
581 | else { | |
582 | os.write(str.data(), size); | |
583 | if (os.good()) | |
584 | detail::sv_insert_fill_chars(os, alignment_size); | |
585 | } | |
586 | } | |
587 | ||
588 | } // namespace detail | |
589 | ||
590 | // Inserter | |
591 | template<class charT, class traits> | |
592 | inline std::basic_ostream<charT, traits>& | |
593 | operator<<(std::basic_ostream<charT, traits>& os, | |
594 | const basic_string_view<charT,traits>& str) { | |
595 | if (os.good()) { | |
596 | const std::size_t size = str.size(); | |
597 | const std::size_t w = static_cast< std::size_t >(os.width()); | |
598 | if (w <= size) | |
599 | os.write(str.data(), size); | |
600 | else | |
601 | detail::sv_insert_aligned(os, str); | |
602 | os.width(0); | |
603 | } | |
604 | return os; | |
605 | } | |
606 | ||
607 | #if 0 | |
608 | // numeric conversions | |
609 | // | |
610 | // These are short-term implementations. | |
611 | // In a production environment, I would rather avoid the copying. | |
612 | // | |
613 | inline int stoi (string_view str, size_t* idx=0, int base=10) { | |
614 | return std::stoi ( std::string(str), idx, base ); | |
615 | } | |
616 | ||
617 | inline long stol (string_view str, size_t* idx=0, int base=10) { | |
618 | return std::stol ( std::string(str), idx, base ); | |
619 | } | |
620 | ||
621 | inline unsigned long stoul (string_view str, size_t* idx=0, int base=10) { | |
622 | return std::stoul ( std::string(str), idx, base ); | |
623 | } | |
624 | ||
625 | inline long long stoll (string_view str, size_t* idx=0, int base=10) { | |
626 | return std::stoll ( std::string(str), idx, base ); | |
627 | } | |
628 | ||
629 | inline unsigned long long stoull (string_view str, size_t* idx=0, int base=10) { | |
630 | return std::stoull ( std::string(str), idx, base ); | |
631 | } | |
632 | ||
633 | inline float stof (string_view str, size_t* idx=0) { | |
634 | return std::stof ( std::string(str), idx ); | |
635 | } | |
636 | ||
637 | inline double stod (string_view str, size_t* idx=0) { | |
638 | return std::stod ( std::string(str), idx ); | |
639 | } | |
640 | ||
641 | inline long double stold (string_view str, size_t* idx=0) { | |
642 | return std::stold ( std::string(str), idx ); | |
643 | } | |
644 | ||
645 | inline int stoi (wstring_view str, size_t* idx=0, int base=10) { | |
646 | return std::stoi ( std::wstring(str), idx, base ); | |
647 | } | |
648 | ||
649 | inline long stol (wstring_view str, size_t* idx=0, int base=10) { | |
650 | return std::stol ( std::wstring(str), idx, base ); | |
651 | } | |
652 | ||
653 | inline unsigned long stoul (wstring_view str, size_t* idx=0, int base=10) { | |
654 | return std::stoul ( std::wstring(str), idx, base ); | |
655 | } | |
656 | ||
657 | inline long long stoll (wstring_view str, size_t* idx=0, int base=10) { | |
658 | return std::stoll ( std::wstring(str), idx, base ); | |
659 | } | |
660 | ||
661 | inline unsigned long long stoull (wstring_view str, size_t* idx=0, int base=10) { | |
662 | return std::stoull ( std::wstring(str), idx, base ); | |
663 | } | |
664 | ||
665 | inline float stof (wstring_view str, size_t* idx=0) { | |
666 | return std::stof ( std::wstring(str), idx ); | |
667 | } | |
668 | ||
669 | inline double stod (wstring_view str, size_t* idx=0) { | |
670 | return std::stod ( std::wstring(str), idx ); | |
671 | } | |
672 | ||
673 | inline long double stold (wstring_view str, size_t* idx=0) { | |
674 | return std::stold ( std::wstring(str), idx ); | |
675 | } | |
676 | #endif | |
677 | ||
678 | } | |
679 | ||
680 | #if 0 | |
681 | namespace std { | |
682 | // Hashing | |
683 | template<> struct hash<boost::string_view>; | |
684 | template<> struct hash<boost::u16string_view>; | |
685 | template<> struct hash<boost::u32string_view>; | |
686 | template<> struct hash<boost::wstring_view>; | |
687 | } | |
688 | #endif | |
689 | ||
690 | #endif |