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 BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) BOOST_NOEXCEPT
96 : ptr_(str), len_(len) {}
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());
105 std::basic_string<charT, traits> to_string () const {
106 return std::basic_string<charT, traits> ( begin(), end());
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()); }
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; }
126 BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; }
128 const charT& at(size_t pos) const {
130 BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) );
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_; }
139 void clear() { len_ = 0; }
140 void remove_prefix(size_type n) {
147 void remove_suffix(size_type n) {
154 // basic_string_ref string operations
155 basic_string_ref substr(size_type pos, size_type n=npos) const {
157 BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
158 if ( n == npos || pos + n > size())
160 return basic_string_ref ( data() + pos, n );
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 );
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;
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;
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 );
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 );
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());
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));
202 size_type find_first_of(charT c) const { return find (c); }
203 size_type find_last_of (charT c) const { return rfind (c); }
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 );
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));
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 );
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 );
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));
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);
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 ))
258 // Comparison operators
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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;
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);
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;
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);
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;
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);
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;
436 detail::sr_insert_fill_chars(os, alignment_size);
438 os.write(str.data(), size);
441 os.write(str.data(), size);
443 detail::sr_insert_fill_chars(os, alignment_size);
447 } // namespace detail
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) {
454 const std::size_t size = str.size();
455 const std::size_t w = static_cast< std::size_t >(os.width());
457 os.write(str.data(), size);
459 detail::sr_insert_aligned(os, str);
466 // numeric conversions
468 // These are short-term implementations.
469 // In a production environment, I would rather avoid the copying.
471 inline int stoi (string_ref str, size_t* idx=0, int base=10) {
472 return std::stoi ( std::string(str), idx, base );
475 inline long stol (string_ref str, size_t* idx=0, int base=10) {
476 return std::stol ( std::string(str), idx, base );
479 inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) {
480 return std::stoul ( std::string(str), idx, base );
483 inline long long stoll (string_ref str, size_t* idx=0, int base=10) {
484 return std::stoll ( std::string(str), idx, base );
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 );
491 inline float stof (string_ref str, size_t* idx=0) {
492 return std::stof ( std::string(str), idx );
495 inline double stod (string_ref str, size_t* idx=0) {
496 return std::stod ( std::string(str), idx );
499 inline long double stold (string_ref str, size_t* idx=0) {
500 return std::stold ( std::string(str), idx );
503 inline int stoi (wstring_ref str, size_t* idx=0, int base=10) {
504 return std::stoi ( std::wstring(str), idx, base );
507 inline long stol (wstring_ref str, size_t* idx=0, int base=10) {
508 return std::stol ( std::wstring(str), idx, base );
511 inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) {
512 return std::stoul ( std::wstring(str), idx, base );
515 inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) {
516 return std::stoll ( std::wstring(str), idx, base );
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 );
523 inline float stof (wstring_ref str, size_t* idx=0) {
524 return std::stof ( std::wstring(str), idx );
527 inline double stod (wstring_ref str, size_t* idx=0) {
528 return std::stod ( std::wstring(str), idx );
531 inline long double stold (wstring_ref str, size_t* idx=0) {
532 return std::stold ( std::wstring(str), idx );
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>;