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