17 #ifndef BOOST_STRING_VIEW_HPP 18 #define BOOST_STRING_VIEW_HPP 20 #include <boost/config.hpp> 21 #include <boost/detail/workaround.hpp> 22 #include <boost/version.hpp> 23 #include <boost/throw_exception.hpp> 25 #if BOOST_VERSION < 106100 27 #include "mlpack/core/boost_backport/string_view_fwd.hpp" 30 #include <boost/utility/string_view_fwd.hpp> 41 #if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || (defined(BOOST_GCC) && ((BOOST_GCC+0) / 100) <= 406) 43 #define BOOST_STRING_VIEW_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS 50 template <
typename charT,
typename traits>
54 bool operator()( charT val )
const {
return traits::eq (ch_, val); }
59 template<
typename charT,
typename traits>
63 typedef traits traits_type;
64 typedef charT value_type;
65 typedef charT* pointer;
66 typedef const charT* const_pointer;
67 typedef charT& reference;
68 typedef const charT& const_reference;
69 typedef const_pointer const_iterator;
70 typedef const_iterator iterator;
71 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
72 typedef const_reverse_iterator reverse_iterator;
73 typedef std::size_t size_type;
74 typedef std::ptrdiff_t difference_type;
75 static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1);
79 : ptr_(NULL), len_(0) {}
84 #ifndef BOOST_STRING_VIEW_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS 87 : ptr_(rhs.ptr_), len_(rhs.len_) {}
91 #ifndef BOOST_STRING_VIEW_NO_CXX11_DEFAULTED_NOEXCEPT_FUNCTIONS 101 template<
typename Allocator>
102 basic_string_view(
const std::basic_string<charT, traits, Allocator>& str) BOOST_NOEXCEPT
103 : ptr_(str.data()), len_(str.length()) {}
113 : ptr_(str), len_(traits::length(str)) {}
115 BOOST_CONSTEXPR basic_string_view(
const charT* str, size_type len)
116 : ptr_(str), len_(len) {}
119 BOOST_CONSTEXPR const_iterator begin()
const BOOST_NOEXCEPT {
return ptr_; }
120 BOOST_CONSTEXPR const_iterator cbegin()
const BOOST_NOEXCEPT {
return ptr_; }
121 BOOST_CONSTEXPR const_iterator end()
const BOOST_NOEXCEPT {
return ptr_ + len_; }
122 BOOST_CONSTEXPR const_iterator cend()
const BOOST_NOEXCEPT {
return ptr_ + len_; }
123 const_reverse_iterator rbegin()
const BOOST_NOEXCEPT {
return const_reverse_iterator(end()); }
124 const_reverse_iterator crbegin()
const BOOST_NOEXCEPT {
return const_reverse_iterator(end()); }
125 const_reverse_iterator rend()
const BOOST_NOEXCEPT {
return const_reverse_iterator(begin()); }
126 const_reverse_iterator crend()
const BOOST_NOEXCEPT {
return const_reverse_iterator(begin()); }
129 BOOST_CONSTEXPR size_type size()
const BOOST_NOEXCEPT {
return len_; }
130 BOOST_CONSTEXPR size_type length()
const BOOST_NOEXCEPT {
return len_; }
131 BOOST_CONSTEXPR size_type max_size()
const BOOST_NOEXCEPT {
return len_; }
132 BOOST_CONSTEXPR
bool empty()
const BOOST_NOEXCEPT {
return len_ == 0; }
135 BOOST_CONSTEXPR const_reference operator[](size_type pos)
const BOOST_NOEXCEPT {
return ptr_[pos]; }
137 BOOST_CONSTEXPR const_reference at(
size_t pos)
const {
138 return pos >= len_ ? BOOST_THROW_EXCEPTION(std::out_of_range(
"boost::string_view::at")), ptr_[0] : ptr_[pos];
141 BOOST_CONSTEXPR const_reference front()
const {
return ptr_[0]; }
142 BOOST_CONSTEXPR const_reference back()
const {
return ptr_[len_-1]; }
143 BOOST_CONSTEXPR const_pointer
data()
const BOOST_NOEXCEPT {
return ptr_; }
146 void clear() BOOST_NOEXCEPT { len_ = 0; }
148 BOOST_CXX14_CONSTEXPR
void remove_prefix(size_type n) {
155 BOOST_CXX14_CONSTEXPR
void remove_suffix(size_type n) {
161 BOOST_CXX14_CONSTEXPR
void swap(basic_string_view& s) BOOST_NOEXCEPT {
162 std::swap(ptr_, s.ptr_);
163 std::swap(len_, s.len_);
167 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS 168 template<
typename Allocator>
169 explicit operator std::basic_string<charT, traits, Allocator>()
const {
170 return std::basic_string<charT, traits, Allocator>(begin(), end());
174 #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS 175 template<
typename Allocator = std::allocator<
charT> >
176 std::basic_string<charT, traits, Allocator> to_string(
const Allocator& a = Allocator())
const {
177 return std::basic_string<charT, traits, Allocator>(begin(), end(), a);
180 std::basic_string<charT, traits> to_string()
const {
181 return std::basic_string<charT, traits>(begin(), end());
184 template<
typename Allocator>
185 std::basic_string<charT, traits, Allocator> to_string(
const Allocator& a)
const {
186 return std::basic_string<charT, traits, Allocator>(begin(), end(), a);
190 size_type copy(charT* s, size_type n, size_type pos=0)
const {
192 BOOST_THROW_EXCEPTION(std::out_of_range(
"string_view::copy" ));
193 size_type rlen = (std::min)(n, len_ - pos);
194 traits_type::copy(s,
data() + pos, rlen);
198 BOOST_CXX14_CONSTEXPR basic_string_view substr(size_type pos, size_type n=npos)
const {
200 BOOST_THROW_EXCEPTION( std::out_of_range (
"string_view::substr" ) );
201 return basic_string_view(
data() + pos, (std::min)(size() - pos, n));
204 BOOST_CXX14_CONSTEXPR
int compare(basic_string_view x)
const BOOST_NOEXCEPT {
205 const int cmp = traits::compare(ptr_, x.ptr_, (std::min)(len_, x.len_));
206 return cmp != 0 ? cmp : (len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1);
209 BOOST_CXX14_CONSTEXPR
int compare(size_type pos1, size_type n1, basic_string_view x)
210 const BOOST_NOEXCEPT {
211 return substr(pos1, n1).compare(x);
214 BOOST_CXX14_CONSTEXPR
int compare(size_type pos1, size_type n1,
215 basic_string_view x, size_type pos2, size_type n2)
const {
216 return substr(pos1, n1).compare(x.substr(pos2, n2));
219 BOOST_CXX14_CONSTEXPR
int compare(
const charT* x)
const {
220 return compare(basic_string_view(x));
223 BOOST_CXX14_CONSTEXPR
int compare(size_type pos1, size_type n1,
const charT* x)
const {
224 return substr(pos1, n1).compare(basic_string_view(x));
227 BOOST_CXX14_CONSTEXPR
int compare(size_type pos1, size_type n1,
228 const charT* x, size_type n2)
const {
229 return substr(pos1, n1).compare(basic_string_view(x, n2));
233 BOOST_CONSTEXPR
bool starts_with(charT c)
const BOOST_NOEXCEPT {
234 return !empty() && traits::eq(c, front());
237 BOOST_CONSTEXPR
bool starts_with(basic_string_view x)
const BOOST_NOEXCEPT {
238 return len_ >= x.len_ && traits::compare(ptr_, x.ptr_, x.len_) == 0;
241 BOOST_CONSTEXPR
bool ends_with(charT c)
const BOOST_NOEXCEPT {
242 return !empty() && traits::eq(c, back());
245 BOOST_CONSTEXPR
bool ends_with(basic_string_view x)
const BOOST_NOEXCEPT {
246 return len_ >= x.len_ &&
247 traits::compare(ptr_ + len_ - x.len_, x.ptr_, x.len_) == 0;
251 BOOST_CXX14_CONSTEXPR size_type find(basic_string_view s, size_type pos = 0)
const BOOST_NOEXCEPT {
256 const_iterator iter = std::search(this->cbegin() + pos, this->cend(),
257 s.cbegin (), s.cend (), traits::eq);
258 return iter == this->cend () ? npos : std::distance(this->cbegin (), iter);
260 BOOST_CXX14_CONSTEXPR size_type find(charT c, size_type pos = 0)
const BOOST_NOEXCEPT
261 {
return find(basic_string_view(&c, 1), pos); }
262 BOOST_CXX14_CONSTEXPR size_type find(
const charT* s, size_type pos, size_type n)
const BOOST_NOEXCEPT
263 {
return find(basic_string_view(s, n), pos); }
264 BOOST_CXX14_CONSTEXPR size_type find(
const charT* s, size_type pos = 0)
const BOOST_NOEXCEPT
265 {
return find(basic_string_view(s), pos); }
268 BOOST_CXX14_CONSTEXPR size_type rfind(basic_string_view s, size_type pos = npos)
const BOOST_NOEXCEPT {
271 if (pos > len_ - s.len_)
275 for (
const charT* cur = ptr_ + pos; ; --cur) {
276 if (traits::compare(cur, s.ptr_, s.len_) == 0)
282 BOOST_CXX14_CONSTEXPR size_type rfind(charT c, size_type pos = npos)
const BOOST_NOEXCEPT
283 {
return rfind(basic_string_view(&c, 1), pos); }
284 BOOST_CXX14_CONSTEXPR size_type rfind(
const charT* s, size_type pos, size_type n)
const BOOST_NOEXCEPT
285 {
return rfind(basic_string_view(s, n), pos); }
286 BOOST_CXX14_CONSTEXPR size_type rfind(
const charT* s, size_type pos = npos)
const BOOST_NOEXCEPT
287 {
return rfind(basic_string_view(s), pos); }
290 BOOST_CXX14_CONSTEXPR size_type find_first_of(basic_string_view s, size_type pos = 0)
const BOOST_NOEXCEPT {
291 if (pos >= len_ || s.len_ == 0)
293 const_iterator iter = std::find_first_of
294 (this->cbegin () + pos, this->cend (), s.cbegin (), s.cend (), traits::eq);
295 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
297 BOOST_CXX14_CONSTEXPR size_type find_first_of(charT c, size_type pos = 0)
const BOOST_NOEXCEPT
298 {
return find_first_of(basic_string_view(&c, 1), pos); }
299 BOOST_CXX14_CONSTEXPR size_type find_first_of(
const charT* s, size_type pos, size_type n)
const BOOST_NOEXCEPT
300 {
return find_first_of(basic_string_view(s, n), pos); }
301 BOOST_CXX14_CONSTEXPR size_type find_first_of(
const charT* s, size_type pos = 0)
const BOOST_NOEXCEPT
302 {
return find_first_of(basic_string_view(s), pos); }
305 BOOST_CXX14_CONSTEXPR size_type find_last_of(basic_string_view s, size_type pos = npos)
const BOOST_NOEXCEPT {
311 pos = len_ - (pos+1);
312 const_reverse_iterator iter = std::find_first_of
313 ( this->crbegin () + pos, this->crend (), s.cbegin (), s.cend (), traits::eq );
314 return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter);
316 BOOST_CXX14_CONSTEXPR size_type find_last_of(charT c, size_type pos = npos)
const BOOST_NOEXCEPT
317 {
return find_last_of(basic_string_view(&c, 1), pos); }
318 BOOST_CXX14_CONSTEXPR size_type find_last_of(
const charT* s, size_type pos, size_type n)
const BOOST_NOEXCEPT
319 {
return find_last_of(basic_string_view(s, n), pos); }
320 BOOST_CXX14_CONSTEXPR size_type find_last_of(
const charT* s, size_type pos = npos)
const BOOST_NOEXCEPT
321 {
return find_last_of(basic_string_view(s), pos); }
324 BOOST_CXX14_CONSTEXPR size_type find_first_not_of(basic_string_view s, size_type pos = 0)
const BOOST_NOEXCEPT {
329 const_iterator iter = find_not_of ( this->cbegin () + pos, this->cend (), s );
330 return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
332 BOOST_CXX14_CONSTEXPR size_type find_first_not_of(charT c, size_type pos = 0)
const BOOST_NOEXCEPT
333 {
return find_first_not_of(basic_string_view(&c, 1), pos); }
334 BOOST_CXX14_CONSTEXPR size_type find_first_not_of(
const charT* s, size_type pos, size_type n)
const BOOST_NOEXCEPT
335 {
return find_first_not_of(basic_string_view(s, n), pos); }
336 BOOST_CXX14_CONSTEXPR size_type find_first_not_of(
const charT* s, size_type pos = 0)
const BOOST_NOEXCEPT
337 {
return find_first_not_of(basic_string_view(s), pos); }
340 BOOST_CXX14_CONSTEXPR size_type find_last_not_of(basic_string_view s, size_type pos = npos)
const BOOST_NOEXCEPT {
345 pos = len_ - (pos+1);
346 const_reverse_iterator iter = find_not_of ( this->crbegin () + pos, this->crend (), s );
347 return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
349 BOOST_CXX14_CONSTEXPR size_type find_last_not_of(charT c, size_type pos = npos)
const BOOST_NOEXCEPT
350 {
return find_last_not_of(basic_string_view(&c, 1), pos); }
351 BOOST_CXX14_CONSTEXPR size_type find_last_not_of(
const charT* s, size_type pos, size_type n)
const BOOST_NOEXCEPT
352 {
return find_last_not_of(basic_string_view(s, n), pos); }
353 BOOST_CXX14_CONSTEXPR size_type find_last_not_of(
const charT* s, size_type pos = npos)
const BOOST_NOEXCEPT
354 {
return find_last_not_of(basic_string_view(s), pos); }
357 template <
typename r_iter>
358 size_type reverse_distance(r_iter first, r_iter last)
const BOOST_NOEXCEPT {
360 return len_ - 1 - std::distance ( first, last );
363 template <
typename Iterator>
364 Iterator find_not_of(Iterator first, Iterator last, basic_string_view s)
const BOOST_NOEXCEPT {
365 for (; first != last ; ++first)
366 if ( 0 == traits::find(s.ptr_, s.len_, *first))
378 template<
typename charT,
typename traits>
381 if (x.size () != y.size ())
return false;
382 return x.compare(y) == 0;
386 template<
typename charT,
typename traits>
389 if ( x.size () != y.size ())
return true;
390 return x.compare(y) != 0;
394 template<
typename charT,
typename traits>
395 inline bool operator<(basic_string_view<charT, traits> x,
397 return x.compare(y) < 0;
401 template<
typename charT,
typename traits>
404 return x.compare(y) > 0;
408 template<
typename charT,
typename traits>
409 inline bool operator<=(basic_string_view<charT, traits> x,
411 return x.compare(y) <= 0;
415 template<
typename charT,
typename traits>
418 return x.compare(y) >= 0;
422 template<
typename charT,
typename traits,
typename Allocator>
424 const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
428 template<
typename charT,
typename traits,
typename Allocator>
429 inline bool operator==(
const std::basic_string<charT, traits, Allocator> & x,
434 template<
typename charT,
typename traits>
436 const charT * y) BOOST_NOEXCEPT {
440 template<
typename charT,
typename traits>
441 inline bool operator==(
const charT * x,
446 template<
typename charT,
typename traits,
typename Allocator>
448 const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
452 template<
typename charT,
typename traits,
typename Allocator>
453 inline bool operator!=(
const std::basic_string<charT, traits, Allocator> & x,
458 template<
typename charT,
typename traits>
460 const charT * y) BOOST_NOEXCEPT {
464 template<
typename charT,
typename traits>
465 inline bool operator!=(
const charT * x,
470 template<
typename charT,
typename traits,
typename Allocator>
471 inline bool operator<(basic_string_view<charT, traits> x,
472 const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
473 return x < basic_string_view<charT, traits>(y);
476 template<
typename charT,
typename traits,
typename Allocator>
477 inline bool operator<(const std::basic_string<charT, traits, Allocator> & x,
482 template<
typename charT,
typename traits>
483 inline bool operator<(basic_string_view<charT, traits> x,
484 const charT * y) BOOST_NOEXCEPT {
485 return x < basic_string_view<charT, traits>(y);
488 template<
typename charT,
typename traits>
489 inline bool operator<(
const charT * x,
494 template<
typename charT,
typename traits,
typename Allocator>
496 const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
500 template<
typename charT,
typename traits,
typename Allocator>
501 inline bool operator>(
const std::basic_string<charT, traits, Allocator> & x,
506 template<
typename charT,
typename traits>
508 const charT * y) BOOST_NOEXCEPT {
512 template<
typename charT,
typename traits>
513 inline bool operator>(
const charT * x,
518 template<
typename charT,
typename traits,
typename Allocator>
519 inline bool operator<=(basic_string_view<charT, traits> x,
520 const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
521 return x <= basic_string_view<charT, traits>(y);
524 template<
typename charT,
typename traits,
typename Allocator>
525 inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x,
530 template<
typename charT,
typename traits>
531 inline bool operator<=(basic_string_view<charT, traits> x,
532 const charT * y) BOOST_NOEXCEPT {
533 return x <= basic_string_view<charT, traits>(y);
536 template<
typename charT,
typename traits>
537 inline bool operator<=(
const charT * x,
542 template<
typename charT,
typename traits,
typename Allocator>
544 const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
548 template<
typename charT,
typename traits,
typename Allocator>
549 inline bool operator>=(
const std::basic_string<charT, traits, Allocator> & x,
554 template<
typename charT,
typename traits>
556 const charT * y) BOOST_NOEXCEPT {
560 template<
typename charT,
typename traits>
561 inline bool operator>=(
const charT * x,
568 template<
class charT,
class traits>
569 inline void sv_insert_fill_chars(std::basic_ostream<charT, traits>& os, std::size_t n) {
570 enum { chunk_size = 8 };
571 charT fill_chars[chunk_size];
572 std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill());
573 for (; n >= chunk_size && os.good(); n -= chunk_size)
574 os.write(fill_chars, static_cast< std::size_t >(chunk_size));
575 if (n > 0 && os.good())
576 os.write(fill_chars, n);
579 template<
class charT,
class traits>
581 const std::size_t size = str.size();
582 const std::size_t alignment_size =
static_cast< std::size_t
>(os.width()) - size;
583 const bool align_left = (os.flags() & std::basic_ostream<charT, traits>::adjustfield) == std::basic_ostream<charT, traits>::left;
585 detail::sv_insert_fill_chars(os, alignment_size);
587 os.write(str.data(), size);
590 os.write(str.data(), size);
592 detail::sv_insert_fill_chars(os, alignment_size);
599 template<
class charT,
class traits>
600 inline std::basic_ostream<charT, traits>&
601 operator<<(std::basic_ostream<charT, traits>& os,
604 const std::size_t size = str.size();
605 const std::size_t w =
static_cast< std::size_t
>(os.width());
607 os.write(str.data(), size);
609 detail::sv_insert_aligned(os, str);
621 inline int stoi (
string_view str,
size_t* idx=0,
int base=10) {
622 return std::stoi ( std::string(str), idx, base );
625 inline long stol (
string_view str,
size_t* idx=0,
int base=10) {
626 return std::stol ( std::string(str), idx, base );
629 inline unsigned long stoul (
string_view str,
size_t* idx=0,
int base=10) {
630 return std::stoul ( std::string(str), idx, base );
633 inline long long stoll (
string_view str,
size_t* idx=0,
int base=10) {
634 return std::stoll ( std::string(str), idx, base );
637 inline unsigned long long stoull (
string_view str,
size_t* idx=0,
int base=10) {
638 return std::stoull ( std::string(str), idx, base );
641 inline float stof (
string_view str,
size_t* idx=0) {
642 return std::stof ( std::string(str), idx );
645 inline double stod (
string_view str,
size_t* idx=0) {
646 return std::stod ( std::string(str), idx );
649 inline long double stold (
string_view str,
size_t* idx=0) {
650 return std::stold ( std::string(str), idx );
653 inline int stoi (
wstring_view str,
size_t* idx=0,
int base=10) {
654 return std::stoi ( std::wstring(str), idx, base );
657 inline long stol (
wstring_view str,
size_t* idx=0,
int base=10) {
658 return std::stol ( std::wstring(str), idx, base );
661 inline unsigned long stoul (
wstring_view str,
size_t* idx=0,
int base=10) {
662 return std::stoul ( std::wstring(str), idx, base );
665 inline long long stoll (
wstring_view str,
size_t* idx=0,
int base=10) {
666 return std::stoll ( std::wstring(str), idx, base );
669 inline unsigned long long stoull (
wstring_view str,
size_t* idx=0,
int base=10) {
670 return std::stoull ( std::wstring(str), idx, base );
674 return std::stof ( std::wstring(str), idx );
678 return std::stod ( std::wstring(str), idx );
681 inline long double stold (
wstring_view str,
size_t* idx=0) {
682 return std::stold ( std::wstring(str), idx );
692 template<> struct hash<boost::u16string_view>;
693 template<> struct hash<boost::u32string_view>;
694 template<> struct hash<boost::wstring_view>;
Definition: bernoulli.hpp:17
Definition: string_view.hpp:51
Definition: pointer_wrapper.hpp:23
Definition: string_view.hpp:60