]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/utility/string_ref.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / 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 // #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
96 // // Constructing a string_ref from a temporary string is a bad idea
97 // template<typename Allocator>
98 // basic_string_ref( std::basic_string<charT, traits, Allocator>&&)
99 // = delete;
100 // #endif
101
102 BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) BOOST_NOEXCEPT
103 : ptr_(str), len_(len) {}
104
105 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
106 template<typename Allocator>
107 explicit operator std::basic_string<charT, traits, Allocator>() const {
108 return std::basic_string<charT, traits, Allocator> ( begin(), end());
109 }
110 #endif
111
112 std::basic_string<charT, traits> to_string () const {
113 return std::basic_string<charT, traits> ( begin(), end());
114 }
115
116 // iterators
117 BOOST_CONSTEXPR const_iterator begin() const { return ptr_; }
118 BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; }
119 BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; }
120 BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; }
121 const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); }
122 const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); }
123 const_reverse_iterator rend() const { return const_reverse_iterator (begin()); }
124 const_reverse_iterator crend() const { return const_reverse_iterator (begin()); }
125
126 // capacity
127 BOOST_CONSTEXPR size_type size() const { return len_; }
128 BOOST_CONSTEXPR size_type length() const { return len_; }
129 BOOST_CONSTEXPR size_type max_size() const { return len_; }
130 BOOST_CONSTEXPR bool empty() const { return len_ == 0; }
131
132 // element access
133 BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; }
134
135 const charT& at(size_t pos) const {
136 if ( pos >= len_ )
137 BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) );
138 return ptr_[pos];
139 }
140
141 BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; }
142 BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; }
143 BOOST_CONSTEXPR const charT* data() const { return ptr_; }
144
145 // modifiers
146 void clear() { len_ = 0; }
147 void remove_prefix(size_type n) {
148 if ( n > len_ )
149 n = len_;
150 ptr_ += n;
151 len_ -= n;
152 }
153
154 void remove_suffix(size_type n) {
155 if ( n > len_ )
156 n = len_;
157 len_ -= n;
158 }
159
160
161 // basic_string_ref string operations
162 basic_string_ref substr(size_type pos, size_type n=npos) const {
163 if ( pos > size())
164 BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
165 return basic_string_ref(data() + pos, (std::min)(size() - pos, n));
166 }
167
168 int compare(basic_string_ref x) const {
169 const int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_));
170 return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 );
171 }
172
173 bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); }
174 bool starts_with(basic_string_ref x) const {
175 return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0;
176 }
177
178 bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); }
179 bool ends_with(basic_string_ref x) const {
180 return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0;
181 }
182
183 size_type find(basic_string_ref s) const {
184 const_iterator iter = std::search ( this->cbegin (), this->cend (),
185 s.cbegin (), s.cend (), traits::eq );
186 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
187 }
188
189 size_type find(charT c) const {
190 const_iterator iter = std::find_if ( this->cbegin (), this->cend (),
191 detail::string_ref_traits_eq<charT, traits> ( c ));
192 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
193 }
194
195 size_type rfind(basic_string_ref s) const {
196 const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (),
197 s.crbegin (), s.crend (), traits::eq );
198 return iter == this->crend () ? npos : (std::distance(iter, this->crend()) - s.size());
199 }
200
201 size_type rfind(charT c) const {
202 const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (),
203 detail::string_ref_traits_eq<charT, traits> ( c ));
204 return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
205 }
206
207 size_type find_first_of(charT c) const { return find (c); }
208 size_type find_last_of (charT c) const { return rfind (c); }
209
210 size_type find_first_of(basic_string_ref s) const {
211 const_iterator iter = std::find_first_of
212 ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq );
213 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
214 }
215
216 size_type find_last_of(basic_string_ref s) const {
217 const_reverse_iterator iter = std::find_first_of
218 ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq );
219 return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
220 }
221
222 size_type find_first_not_of(basic_string_ref s) const {
223 const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s );
224 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
225 }
226
227 size_type find_first_not_of(charT c) const {
228 for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter )
229 if ( !traits::eq ( c, *iter ))
230 return std::distance ( this->cbegin (), iter );
231 return npos;
232 }
233
234 size_type find_last_not_of(basic_string_ref s) const {
235 const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s );
236 return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
237 }
238
239 size_type find_last_not_of(charT c) const {
240 for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter )
241 if ( !traits::eq ( c, *iter ))
242 return this->size() - 1 - std::distance(this->crbegin(), iter);
243 return npos;
244 }
245
246 private:
247
248 template <typename Iterator>
249 Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const {
250 for ( ; first != last ; ++first )
251 if ( 0 == traits::find ( s.ptr_, s.len_, *first ))
252 return first;
253 return last;
254 }
255
256
257
258 const charT *ptr_;
259 std::size_t len_;
260 };
261
262
263 // Comparison operators
264 // Equality
265 template<typename charT, typename traits>
266 inline bool operator==(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
267 if ( x.size () != y.size ()) return false;
268 return x.compare(y) == 0;
269 }
270
271 template<typename charT, typename traits, typename Allocator>
272 inline bool operator==(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
273 return x == basic_string_ref<charT, traits>(y);
274 }
275
276 template<typename charT, typename traits, typename Allocator>
277 inline bool operator==(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
278 return basic_string_ref<charT, traits>(x) == y;
279 }
280
281 template<typename charT, typename traits>
282 inline bool operator==(basic_string_ref<charT, traits> x, const charT * y) {
283 return x == basic_string_ref<charT, traits>(y);
284 }
285
286 template<typename charT, typename traits>
287 inline bool operator==(const charT * x, basic_string_ref<charT, traits> y) {
288 return basic_string_ref<charT, traits>(x) == y;
289 }
290
291 // Inequality
292 template<typename charT, typename traits>
293 inline bool operator!=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
294 if ( x.size () != y.size ()) return true;
295 return x.compare(y) != 0;
296 }
297
298 template<typename charT, typename traits, typename Allocator>
299 inline bool operator!=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
300 return x != basic_string_ref<charT, traits>(y);
301 }
302
303 template<typename charT, typename traits, typename Allocator>
304 inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
305 return basic_string_ref<charT, traits>(x) != y;
306 }
307
308 template<typename charT, typename traits>
309 inline bool operator!=(basic_string_ref<charT, traits> x, const charT * y) {
310 return x != basic_string_ref<charT, traits>(y);
311 }
312
313 template<typename charT, typename traits>
314 inline bool operator!=(const charT * x, basic_string_ref<charT, traits> y) {
315 return basic_string_ref<charT, traits>(x) != y;
316 }
317
318 // Less than
319 template<typename charT, typename traits>
320 inline bool operator<(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
321 return x.compare(y) < 0;
322 }
323
324 template<typename charT, typename traits, typename Allocator>
325 inline bool operator<(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
326 return x < basic_string_ref<charT, traits>(y);
327 }
328
329 template<typename charT, typename traits, typename Allocator>
330 inline bool operator<(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
331 return basic_string_ref<charT, traits>(x) < y;
332 }
333
334 template<typename charT, typename traits>
335 inline bool operator<(basic_string_ref<charT, traits> x, const charT * y) {
336 return x < basic_string_ref<charT, traits>(y);
337 }
338
339 template<typename charT, typename traits>
340 inline bool operator<(const charT * x, basic_string_ref<charT, traits> y) {
341 return basic_string_ref<charT, traits>(x) < y;
342 }
343
344 // Greater than
345 template<typename charT, typename traits>
346 inline bool operator>(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
347 return x.compare(y) > 0;
348 }
349
350 template<typename charT, typename traits, typename Allocator>
351 inline bool operator>(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
352 return x > basic_string_ref<charT, traits>(y);
353 }
354
355 template<typename charT, typename traits, typename Allocator>
356 inline bool operator>(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
357 return basic_string_ref<charT, traits>(x) > y;
358 }
359
360 template<typename charT, typename traits>
361 inline bool operator>(basic_string_ref<charT, traits> x, const charT * y) {
362 return x > basic_string_ref<charT, traits>(y);
363 }
364
365 template<typename charT, typename traits>
366 inline bool operator>(const charT * x, basic_string_ref<charT, traits> y) {
367 return basic_string_ref<charT, traits>(x) > y;
368 }
369
370 // Less than or equal to
371 template<typename charT, typename traits>
372 inline bool operator<=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
373 return x.compare(y) <= 0;
374 }
375
376 template<typename charT, typename traits, typename Allocator>
377 inline bool operator<=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
378 return x <= basic_string_ref<charT, traits>(y);
379 }
380
381 template<typename charT, typename traits, typename Allocator>
382 inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
383 return basic_string_ref<charT, traits>(x) <= y;
384 }
385
386 template<typename charT, typename traits>
387 inline bool operator<=(basic_string_ref<charT, traits> x, const charT * y) {
388 return x <= basic_string_ref<charT, traits>(y);
389 }
390
391 template<typename charT, typename traits>
392 inline bool operator<=(const charT * x, basic_string_ref<charT, traits> y) {
393 return basic_string_ref<charT, traits>(x) <= y;
394 }
395
396 // Greater than or equal to
397 template<typename charT, typename traits>
398 inline bool operator>=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
399 return x.compare(y) >= 0;
400 }
401
402 template<typename charT, typename traits, typename Allocator>
403 inline bool operator>=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
404 return x >= basic_string_ref<charT, traits>(y);
405 }
406
407 template<typename charT, typename traits, typename Allocator>
408 inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
409 return basic_string_ref<charT, traits>(x) >= y;
410 }
411
412 template<typename charT, typename traits>
413 inline bool operator>=(basic_string_ref<charT, traits> x, const charT * y) {
414 return x >= basic_string_ref<charT, traits>(y);
415 }
416
417 template<typename charT, typename traits>
418 inline bool operator>=(const charT * x, basic_string_ref<charT, traits> y) {
419 return basic_string_ref<charT, traits>(x) >= y;
420 }
421
422 namespace detail {
423
424 template<class charT, class traits>
425 inline void sr_insert_fill_chars(std::basic_ostream<charT, traits>& os, std::size_t n) {
426 enum { chunk_size = 8 };
427 charT fill_chars[chunk_size];
428 std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill());
429 for (; n >= chunk_size && os.good(); n -= chunk_size)
430 os.write(fill_chars, static_cast< std::size_t >(chunk_size));
431 if (n > 0 && os.good())
432 os.write(fill_chars, n);
433 }
434
435 template<class charT, class traits>
436 void sr_insert_aligned(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
437 const std::size_t size = str.size();
438 const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size;
439 const bool align_left = (os.flags() & std::basic_ostream<charT, traits>::adjustfield) == std::basic_ostream<charT, traits>::left;
440 if (!align_left) {
441 detail::sr_insert_fill_chars(os, alignment_size);
442 if (os.good())
443 os.write(str.data(), size);
444 }
445 else {
446 os.write(str.data(), size);
447 if (os.good())
448 detail::sr_insert_fill_chars(os, alignment_size);
449 }
450 }
451
452 } // namespace detail
453
454 // Inserter
455 template<class charT, class traits>
456 inline std::basic_ostream<charT, traits>&
457 operator<<(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
458 if (os.good()) {
459 const std::size_t size = str.size();
460 const std::size_t w = static_cast< std::size_t >(os.width());
461 if (w <= size)
462 os.write(str.data(), size);
463 else
464 detail::sr_insert_aligned(os, str);
465 os.width(0);
466 }
467 return os;
468 }
469
470 #if 0
471 // numeric conversions
472 //
473 // These are short-term implementations.
474 // In a production environment, I would rather avoid the copying.
475 //
476 inline int stoi (string_ref str, size_t* idx=0, int base=10) {
477 return std::stoi ( std::string(str), idx, base );
478 }
479
480 inline long stol (string_ref str, size_t* idx=0, int base=10) {
481 return std::stol ( std::string(str), idx, base );
482 }
483
484 inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) {
485 return std::stoul ( std::string(str), idx, base );
486 }
487
488 inline long long stoll (string_ref str, size_t* idx=0, int base=10) {
489 return std::stoll ( std::string(str), idx, base );
490 }
491
492 inline unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) {
493 return std::stoull ( std::string(str), idx, base );
494 }
495
496 inline float stof (string_ref str, size_t* idx=0) {
497 return std::stof ( std::string(str), idx );
498 }
499
500 inline double stod (string_ref str, size_t* idx=0) {
501 return std::stod ( std::string(str), idx );
502 }
503
504 inline long double stold (string_ref str, size_t* idx=0) {
505 return std::stold ( std::string(str), idx );
506 }
507
508 inline int stoi (wstring_ref str, size_t* idx=0, int base=10) {
509 return std::stoi ( std::wstring(str), idx, base );
510 }
511
512 inline long stol (wstring_ref str, size_t* idx=0, int base=10) {
513 return std::stol ( std::wstring(str), idx, base );
514 }
515
516 inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) {
517 return std::stoul ( std::wstring(str), idx, base );
518 }
519
520 inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) {
521 return std::stoll ( std::wstring(str), idx, base );
522 }
523
524 inline unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) {
525 return std::stoull ( std::wstring(str), idx, base );
526 }
527
528 inline float stof (wstring_ref str, size_t* idx=0) {
529 return std::stof ( std::wstring(str), idx );
530 }
531
532 inline double stod (wstring_ref str, size_t* idx=0) {
533 return std::stod ( std::wstring(str), idx );
534 }
535
536 inline long double stold (wstring_ref str, size_t* idx=0) {
537 return std::stold ( std::wstring(str), idx );
538 }
539 #endif
540
541 }
542
543 #if 0
544 namespace std {
545 // Hashing
546 template<> struct hash<boost::string_ref>;
547 template<> struct hash<boost::u16string_ref>;
548 template<> struct hash<boost::u32string_ref>;
549 template<> struct hash<boost::wstring_ref>;
550 }
551 #endif
552
553 #endif