2 Copyright (c) Marshall Clow 2012-2015.
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)
7 For more information, see http://www.boost.org
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
15 #ifndef BOOST_STRING_REF_HPP
16 #define BOOST_STRING_REF_HPP
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>
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
38 // A helper functor because sometimes we don't have lambdas
39 template <typename charT, typename traits>
40 class string_ref_traits_eq {
42 string_ref_traits_eq ( charT ch ) : ch_(ch) {}
43 bool operator () ( charT val ) const { return traits::eq ( ch_, val ); }
48 template<typename charT, typename traits>
49 class basic_string_ref {
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);
65 BOOST_CONSTEXPR basic_string_ref () BOOST_NOEXCEPT
66 : ptr_(NULL), len_(0) {}
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
74 : ptr_(rhs.ptr_), len_(rhs.len_) {}
77 basic_string_ref& operator=(const basic_string_ref &rhs) BOOST_NOEXCEPT
78 #ifndef BOOST_STRING_REF_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
88 basic_string_ref(const charT* str) BOOST_NOEXCEPT
89 : ptr_(str), len_(traits::length(str)) {}
91 template<typename Allocator>
92 basic_string_ref(const std::basic_string<charT, traits, Allocator>& str)
93 : ptr_(str.data()), len_(str.length()) {}
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>&&)
102 BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) BOOST_NOEXCEPT
103 : ptr_(str), len_(len) {}
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());
112 std::basic_string<charT, traits> to_string () const {
113 return std::basic_string<charT, traits> ( begin(), end());
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()); }
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; }
133 BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; }
135 const charT& at(size_t pos) const {
137 BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) );
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_; }
146 void clear() { len_ = 0; }
147 void remove_prefix(size_type n) {
154 void remove_suffix(size_type n) {
161 // basic_string_ref string operations
162 basic_string_ref substr(size_type pos, size_type n=npos) const {
164 BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
165 return basic_string_ref(data() + pos, (std::min)(size() - pos, n));
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 );
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;
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;
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 );
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 );
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());
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));
207 size_type find_first_of(charT c) const { return find (c); }
208 size_type find_last_of (charT c) const { return rfind (c); }
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 );
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));
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 );
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 );
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));
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);
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 ))
263 // Comparison operators
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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);
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;
441 detail::sr_insert_fill_chars(os, alignment_size);
443 os.write(str.data(), size);
446 os.write(str.data(), size);
448 detail::sr_insert_fill_chars(os, alignment_size);
452 } // namespace detail
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) {
459 const std::size_t size = str.size();
460 const std::size_t w = static_cast< std::size_t >(os.width());
462 os.write(str.data(), size);
464 detail::sr_insert_aligned(os, str);
471 // numeric conversions
473 // These are short-term implementations.
474 // In a production environment, I would rather avoid the copying.
476 inline int stoi (string_ref str, size_t* idx=0, int base=10) {
477 return std::stoi ( std::string(str), idx, base );
480 inline long stol (string_ref str, size_t* idx=0, int base=10) {
481 return std::stol ( std::string(str), idx, base );
484 inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) {
485 return std::stoul ( std::string(str), idx, base );
488 inline long long stoll (string_ref str, size_t* idx=0, int base=10) {
489 return std::stoll ( std::string(str), idx, base );
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 );
496 inline float stof (string_ref str, size_t* idx=0) {
497 return std::stof ( std::string(str), idx );
500 inline double stod (string_ref str, size_t* idx=0) {
501 return std::stod ( std::string(str), idx );
504 inline long double stold (string_ref str, size_t* idx=0) {
505 return std::stold ( std::string(str), idx );
508 inline int stoi (wstring_ref str, size_t* idx=0, int base=10) {
509 return std::stoi ( std::wstring(str), idx, base );
512 inline long stol (wstring_ref str, size_t* idx=0, int base=10) {
513 return std::stol ( std::wstring(str), idx, base );
516 inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) {
517 return std::stoul ( std::wstring(str), idx, base );
520 inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) {
521 return std::stoll ( std::wstring(str), idx, base );
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 );
528 inline float stof (wstring_ref str, size_t* idx=0) {
529 return std::stof ( std::wstring(str), idx );
532 inline double stod (wstring_ref str, size_t* idx=0) {
533 return std::stod ( std::wstring(str), idx );
536 inline long double stold (wstring_ref str, size_t* idx=0) {
537 return std::stold ( std::wstring(str), idx );
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>;