2 Copyright (c) Marshall Clow 2012-2015.
3 Copyright (c) Glen Joseph Fernandes 2019 (glenjofe@gmail.com)
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)
8 For more information, see http://www.boost.org
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
16 #ifndef BOOST_STRING_REF_HPP
17 #define BOOST_STRING_REF_HPP
19 #include <boost/config.hpp>
20 #include <boost/detail/workaround.hpp>
21 #include <boost/io/ostream_put.hpp>
22 #include <boost/utility/string_ref_fwd.hpp>
23 #include <boost/throw_exception.hpp>
32 #if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || (defined(BOOST_GCC) && ((BOOST_GCC+0) / 100) <= 406)
33 // GCC 4.6 cannot handle a defaulted function with noexcept specifier
34 #define BOOST_STRING_REF_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
40 // A helper functor because sometimes we don't have lambdas
41 template <typename charT, typename traits>
42 class string_ref_traits_eq {
44 string_ref_traits_eq ( charT ch ) : ch_(ch) {}
45 bool operator () ( charT val ) const { return traits::eq ( ch_, val ); }
50 template<typename charT, typename traits>
51 class basic_string_ref {
54 typedef charT value_type;
55 typedef const charT* pointer;
56 typedef const charT& reference;
57 typedef const charT& const_reference;
58 typedef pointer const_iterator; // impl-defined
59 typedef const_iterator iterator;
60 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
61 typedef const_reverse_iterator reverse_iterator;
62 typedef std::size_t size_type;
63 typedef std::ptrdiff_t difference_type;
64 static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1);
67 BOOST_CONSTEXPR basic_string_ref () BOOST_NOEXCEPT
68 : ptr_(NULL), len_(0) {}
70 // by defaulting these functions, basic_string_ref becomes
71 // trivially copy/move constructible.
72 BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs) BOOST_NOEXCEPT
73 #ifndef BOOST_STRING_REF_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
76 : ptr_(rhs.ptr_), len_(rhs.len_) {}
79 basic_string_ref& operator=(const basic_string_ref &rhs) BOOST_NOEXCEPT
80 #ifndef BOOST_STRING_REF_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS
90 basic_string_ref(const charT* str) BOOST_NOEXCEPT
91 : ptr_(str), len_(traits::length(str)) {}
93 template<typename Allocator>
94 basic_string_ref(const std::basic_string<charT, traits, Allocator>& str)
95 : ptr_(str.data()), len_(str.length()) {}
97 // #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
98 // // Constructing a string_ref from a temporary string is a bad idea
99 // template<typename Allocator>
100 // basic_string_ref( std::basic_string<charT, traits, Allocator>&&)
104 BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) BOOST_NOEXCEPT
105 : ptr_(str), len_(len) {}
107 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
108 template<typename Allocator>
109 explicit operator std::basic_string<charT, traits, Allocator>() const {
110 return std::basic_string<charT, traits, Allocator> ( begin(), end());
114 std::basic_string<charT, traits> to_string () const {
115 return std::basic_string<charT, traits> ( begin(), end());
119 BOOST_CONSTEXPR const_iterator begin() const { return ptr_; }
120 BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; }
121 BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; }
122 BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; }
123 const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); }
124 const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); }
125 const_reverse_iterator rend() const { return const_reverse_iterator (begin()); }
126 const_reverse_iterator crend() const { return const_reverse_iterator (begin()); }
129 BOOST_CONSTEXPR size_type size() const { return len_; }
130 BOOST_CONSTEXPR size_type length() const { return len_; }
131 BOOST_CONSTEXPR size_type max_size() const { return len_; }
132 BOOST_CONSTEXPR bool empty() const { return len_ == 0; }
135 BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; }
137 const charT& at(size_t pos) const {
139 BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) );
143 BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; }
144 BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; }
145 BOOST_CONSTEXPR const charT* data() const { return ptr_; }
148 void clear() { len_ = 0; }
149 void remove_prefix(size_type n) {
156 void remove_suffix(size_type n) {
163 // basic_string_ref string operations
164 basic_string_ref substr(size_type pos, size_type n=npos) const {
166 BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
167 return basic_string_ref(data() + pos, (std::min)(size() - pos, n));
170 int compare(basic_string_ref x) const {
171 const int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_));
172 return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 );
175 bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); }
176 bool starts_with(basic_string_ref x) const {
177 return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0;
180 bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); }
181 bool ends_with(basic_string_ref x) const {
182 return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0;
185 size_type find(basic_string_ref s) const {
186 if (s.empty()) return 0;
187 const_iterator iter = std::search ( this->cbegin (), this->cend (),
188 s.cbegin (), s.cend (), traits::eq );
189 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
192 size_type find(charT c) const {
193 const_iterator iter = std::find_if ( this->cbegin (), this->cend (),
194 detail::string_ref_traits_eq<charT, traits> ( c ));
195 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
198 size_type rfind(basic_string_ref s) const {
199 if (s.empty()) return 0;
200 const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (),
201 s.crbegin (), s.crend (), traits::eq );
202 return iter == this->crend () ? npos : (std::distance(iter, this->crend()) - s.size());
205 size_type rfind(charT c) const {
206 const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (),
207 detail::string_ref_traits_eq<charT, traits> ( c ));
208 return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
211 size_type find_first_of(charT c) const { return find (c); }
212 size_type find_last_of (charT c) const { return rfind (c); }
214 size_type find_first_of(basic_string_ref s) const {
215 const_iterator iter = std::find_first_of
216 ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq );
217 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
220 size_type find_last_of(basic_string_ref s) const {
221 const_reverse_iterator iter = std::find_first_of
222 ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq );
223 return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
226 size_type find_first_not_of(basic_string_ref s) const {
227 const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s );
228 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
231 size_type find_first_not_of(charT c) const {
232 for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter )
233 if ( !traits::eq ( c, *iter ))
234 return std::distance ( this->cbegin (), iter );
238 size_type find_last_not_of(basic_string_ref s) const {
239 const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s );
240 return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter));
243 size_type find_last_not_of(charT c) const {
244 for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter )
245 if ( !traits::eq ( c, *iter ))
246 return this->size() - 1 - std::distance(this->crbegin(), iter);
252 template <typename Iterator>
253 Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const {
254 for ( ; first != last ; ++first )
255 if ( 0 == traits::find ( s.ptr_, s.len_, *first ))
267 // Comparison operators
269 template<typename charT, typename traits>
270 inline bool operator==(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
271 if ( x.size () != y.size ()) return false;
272 return x.compare(y) == 0;
275 template<typename charT, typename traits, typename Allocator>
276 inline bool operator==(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
277 return x == basic_string_ref<charT, traits>(y);
280 template<typename charT, typename traits, typename Allocator>
281 inline bool operator==(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
282 return basic_string_ref<charT, traits>(x) == y;
285 template<typename charT, typename traits>
286 inline bool operator==(basic_string_ref<charT, traits> x, const charT * y) {
287 return x == basic_string_ref<charT, traits>(y);
290 template<typename charT, typename traits>
291 inline bool operator==(const charT * x, basic_string_ref<charT, traits> y) {
292 return basic_string_ref<charT, traits>(x) == y;
296 template<typename charT, typename traits>
297 inline bool operator!=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
298 if ( x.size () != y.size ()) return true;
299 return x.compare(y) != 0;
302 template<typename charT, typename traits, typename Allocator>
303 inline bool operator!=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
304 return x != basic_string_ref<charT, traits>(y);
307 template<typename charT, typename traits, typename Allocator>
308 inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
309 return basic_string_ref<charT, traits>(x) != y;
312 template<typename charT, typename traits>
313 inline bool operator!=(basic_string_ref<charT, traits> x, const charT * y) {
314 return x != basic_string_ref<charT, traits>(y);
317 template<typename charT, typename traits>
318 inline bool operator!=(const charT * x, basic_string_ref<charT, traits> y) {
319 return basic_string_ref<charT, traits>(x) != y;
323 template<typename charT, typename traits>
324 inline bool operator<(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
325 return x.compare(y) < 0;
328 template<typename charT, typename traits, typename Allocator>
329 inline bool operator<(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
330 return x < basic_string_ref<charT, traits>(y);
333 template<typename charT, typename traits, typename Allocator>
334 inline bool operator<(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
335 return basic_string_ref<charT, traits>(x) < y;
338 template<typename charT, typename traits>
339 inline bool operator<(basic_string_ref<charT, traits> x, const charT * y) {
340 return x < basic_string_ref<charT, traits>(y);
343 template<typename charT, typename traits>
344 inline bool operator<(const charT * x, basic_string_ref<charT, traits> y) {
345 return basic_string_ref<charT, traits>(x) < y;
349 template<typename charT, typename traits>
350 inline bool operator>(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
351 return x.compare(y) > 0;
354 template<typename charT, typename traits, typename Allocator>
355 inline bool operator>(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
356 return x > basic_string_ref<charT, traits>(y);
359 template<typename charT, typename traits, typename Allocator>
360 inline bool operator>(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
361 return basic_string_ref<charT, traits>(x) > y;
364 template<typename charT, typename traits>
365 inline bool operator>(basic_string_ref<charT, traits> x, const charT * y) {
366 return x > basic_string_ref<charT, traits>(y);
369 template<typename charT, typename traits>
370 inline bool operator>(const charT * x, basic_string_ref<charT, traits> y) {
371 return basic_string_ref<charT, traits>(x) > y;
374 // Less than or equal to
375 template<typename charT, typename traits>
376 inline bool operator<=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
377 return x.compare(y) <= 0;
380 template<typename charT, typename traits, typename Allocator>
381 inline bool operator<=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
382 return x <= basic_string_ref<charT, traits>(y);
385 template<typename charT, typename traits, typename Allocator>
386 inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
387 return basic_string_ref<charT, traits>(x) <= y;
390 template<typename charT, typename traits>
391 inline bool operator<=(basic_string_ref<charT, traits> x, const charT * y) {
392 return x <= basic_string_ref<charT, traits>(y);
395 template<typename charT, typename traits>
396 inline bool operator<=(const charT * x, basic_string_ref<charT, traits> y) {
397 return basic_string_ref<charT, traits>(x) <= y;
400 // Greater than or equal to
401 template<typename charT, typename traits>
402 inline bool operator>=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
403 return x.compare(y) >= 0;
406 template<typename charT, typename traits, typename Allocator>
407 inline bool operator>=(basic_string_ref<charT, traits> x, const std::basic_string<charT, traits, Allocator> & y) {
408 return x >= basic_string_ref<charT, traits>(y);
411 template<typename charT, typename traits, typename Allocator>
412 inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x, basic_string_ref<charT, traits> y) {
413 return basic_string_ref<charT, traits>(x) >= y;
416 template<typename charT, typename traits>
417 inline bool operator>=(basic_string_ref<charT, traits> x, const charT * y) {
418 return x >= basic_string_ref<charT, traits>(y);
421 template<typename charT, typename traits>
422 inline bool operator>=(const charT * x, basic_string_ref<charT, traits> y) {
423 return basic_string_ref<charT, traits>(x) >= y;
427 template<class charT, class traits>
428 inline std::basic_ostream<charT, traits>&
429 operator<<(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
430 return boost::io::ostream_put(os, str.data(), str.size());
434 // numeric conversions
436 // These are short-term implementations.
437 // In a production environment, I would rather avoid the copying.
439 inline int stoi (string_ref str, size_t* idx=0, int base=10) {
440 return std::stoi ( std::string(str), idx, base );
443 inline long stol (string_ref str, size_t* idx=0, int base=10) {
444 return std::stol ( std::string(str), idx, base );
447 inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) {
448 return std::stoul ( std::string(str), idx, base );
451 inline long long stoll (string_ref str, size_t* idx=0, int base=10) {
452 return std::stoll ( std::string(str), idx, base );
455 inline unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) {
456 return std::stoull ( std::string(str), idx, base );
459 inline float stof (string_ref str, size_t* idx=0) {
460 return std::stof ( std::string(str), idx );
463 inline double stod (string_ref str, size_t* idx=0) {
464 return std::stod ( std::string(str), idx );
467 inline long double stold (string_ref str, size_t* idx=0) {
468 return std::stold ( std::string(str), idx );
471 inline int stoi (wstring_ref str, size_t* idx=0, int base=10) {
472 return std::stoi ( std::wstring(str), idx, base );
475 inline long stol (wstring_ref str, size_t* idx=0, int base=10) {
476 return std::stol ( std::wstring(str), idx, base );
479 inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) {
480 return std::stoul ( std::wstring(str), idx, base );
483 inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) {
484 return std::stoll ( std::wstring(str), idx, base );
487 inline unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) {
488 return std::stoull ( std::wstring(str), idx, base );
491 inline float stof (wstring_ref str, size_t* idx=0) {
492 return std::stof ( std::wstring(str), idx );
495 inline double stod (wstring_ref str, size_t* idx=0) {
496 return std::stod ( std::wstring(str), idx );
499 inline long double stold (wstring_ref str, size_t* idx=0) {
500 return std::stold ( std::wstring(str), idx );
509 template<> struct hash<boost::string_ref>;
510 template<> struct hash<boost::u16string_ref>;
511 template<> struct hash<boost::u32string_ref>;
512 template<> struct hash<boost::wstring_ref>;