31 using value_type = char;
32 using traits_type = std::char_traits<char>;
33 BOOST_CONSTEXPR
cstring_ref() noexcept : view_(
"") {}
34 BOOST_CONSTEXPR
cstring_ref(std::nullptr_t) =
delete;
36 BOOST_CONSTEXPR
cstring_ref(
const value_type* s ) : view_(s) {}
38 template<
typename Source,
40 typename std::enable_if<
41 std::is_same<
const value_type,
42 typename std::remove_pointer<decltype(std::declval<Source>().c_str())>::type
44 BOOST_CONSTEXPR
cstring_ref(Source && src) : view_(src.c_str()) {}
46 BOOST_CONSTEXPR
const char * c_str()
const BOOST_NOEXCEPT
51 using string_view_type = core::string_view;
52 constexpr operator string_view_type()
const {
return view_;}
54 using pointer =
char *;
55 using const_pointer =
const char *;
56 using reference =
char &;
57 using const_reference =
const char &;
58 using const_iterator = const_pointer;
59 using iterator = const_iterator;
60 using const_reverse_iterator =
typename std::reverse_iterator<const_iterator>;
61 using reverse_iterator =
typename std::reverse_iterator<iterator>;
62 using size_type = std::size_t;
63 using difference_type = std::ptrdiff_t;
65 static BOOST_CONSTEXPR size_type npos = -1;
67 BOOST_CONSTEXPR const_iterator begin()
const BOOST_NOEXCEPT {
return view_;};
68 BOOST_CONSTEXPR const_iterator end()
const BOOST_NOEXCEPT {
return view_ + length();};
69 BOOST_CONSTEXPR const_iterator cbegin()
const BOOST_NOEXCEPT {
return view_;};
70 BOOST_CONSTEXPR const_iterator cend()
const BOOST_NOEXCEPT {
return view_ + length();};
72#if defined(BOOST_NO_CXX17)
73 const_reverse_iterator rbegin()
const BOOST_NOEXCEPT {
return reverse_iterator(end());};
74 const_reverse_iterator rend()
const BOOST_NOEXCEPT {
return reverse_iterator(begin());};
75 const_reverse_iterator crbegin()
const BOOST_NOEXCEPT {
return reverse_iterator(end());};
76 const_reverse_iterator crend()
const BOOST_NOEXCEPT {
return reverse_iterator(begin());};
78 BOOST_CONSTEXPR const_reverse_iterator rbegin()
const BOOST_NOEXCEPT {
return reverse_iterator(end());};
79 BOOST_CONSTEXPR const_reverse_iterator rend()
const BOOST_NOEXCEPT {
return reverse_iterator(begin());};
80 BOOST_CONSTEXPR const_reverse_iterator crbegin()
const BOOST_NOEXCEPT {
return reverse_iterator(end());};
81 BOOST_CONSTEXPR const_reverse_iterator crend()
const BOOST_NOEXCEPT {
return reverse_iterator(begin());};
84 BOOST_CONSTEXPR size_type size()
const BOOST_NOEXCEPT {
return length(); }
85 BOOST_CONSTEXPR size_type length()
const BOOST_NOEXCEPT {
return length_impl_(); }
86 BOOST_CONSTEXPR size_type max_size()
const BOOST_NOEXCEPT {
return static_cast<std::size_t
>(-1); }
87 BOOST_ATTRIBUTE_NODISCARD BOOST_CONSTEXPR
bool empty()
const BOOST_NOEXCEPT {
return *view_ ==
'\0'; }
89 BOOST_CONSTEXPR const_reference operator[](size_type pos)
const {
return view_[pos] ;}
90 BOOST_CXX14_CONSTEXPR const_reference at(size_type pos)
const
93 throw_exception(std::out_of_range(
"cstring-view out of range"));
96 BOOST_CONSTEXPR const_reference front()
const {
return *view_;}
97 BOOST_CONSTEXPR const_reference back()
const {
return view_[length() - 1];}
98 BOOST_CONSTEXPR const_pointer data()
const BOOST_NOEXCEPT {
return view_;}
99 BOOST_CXX14_CONSTEXPR
void remove_prefix(size_type n) {view_ = view_ + n;}
100 void swap(
cstring_ref& s) BOOST_NOEXCEPT {std::swap(view_, s.view_);}
102 size_type copy(value_type* s, size_type n, size_type pos = 0)
const
104 return traits_type::copy(s, view_ + pos, n) - view_;
106 BOOST_CONSTEXPR
cstring_ref substr(size_type pos = 0)
const
111 BOOST_CXX14_CONSTEXPR string_view_type substr(size_type pos, size_type length)
const
113 return string_view_type(view_).substr(pos, length);
116 BOOST_CXX14_CONSTEXPR
int compare(
cstring_ref x)
const BOOST_NOEXCEPT
119 for (; view_[idx] != null_char_()[0] && x[idx] != null_char_()[0]; idx++)
120 if (!traits_type::eq(view_[idx], x[idx]))
121 return traits_type::lt(view_[idx], x[idx]) ? -1 : 1;
123 return traits_type::to_int_type(view_[idx]) -
124 traits_type::to_int_type(x[idx]);
127 BOOST_CXX14_CONSTEXPR
bool starts_with(string_view_type x)
const BOOST_NOEXCEPT
133 for (; view_[idx] != null_char_()[0] && idx < x.size(); idx++)
134 if (!traits_type::eq(view_[idx], x[idx]))
137 return idx == x.size() || view_[idx] != null_char_()[0];
139 BOOST_CONSTEXPR
bool starts_with(value_type x)
const BOOST_NOEXCEPT
141 return traits_type::eq(view_[0], x);
144 BOOST_CXX14_CONSTEXPR size_type find(
char ch, size_type pos = 0 )
const BOOST_NOEXCEPT
146 for (
auto p = view_ + pos; *p != *null_char_(); p++)
147 if (traits_type::eq(*p, ch))
155 std::size_t idx = 0u;
156 for (idx = 0u; x[idx] != null_char_()[0] && y[idx] != null_char_()[0]; idx++)
157 if (!traits_type::eq(x[idx], y[idx]))
159 return x[idx] == y[idx];
163 std::size_t idx = 0u;
164 for (idx = 0u; x[idx] != null_char_()[0] &&
165 y[idx] != null_char_()[0]; idx++)
166 if (!traits_type::eq(x[idx], y[idx]))
168 return x[idx] != y[idx];
170 friend BOOST_CXX14_CONSTEXPR
bool operator< (
cstring_ref x,
cstring_ref y) BOOST_NOEXCEPT {
return x.compare(y) < 0;}
171 friend BOOST_CXX14_CONSTEXPR
bool operator> (
cstring_ref x,
cstring_ref y) BOOST_NOEXCEPT {
return x.compare(y) > 0;}
172 friend BOOST_CXX14_CONSTEXPR
bool operator<=(
cstring_ref x,
cstring_ref y) BOOST_NOEXCEPT {
return x.compare(y) <= 0;}
173 friend BOOST_CXX14_CONSTEXPR
bool operator>=(
cstring_ref x,
cstring_ref y) BOOST_NOEXCEPT {
return x.compare(y) >= 0;}
176 void clear() BOOST_NOEXCEPT { view_ = null_char_(); }
178 std::basic_string<value_type, traits_type> to_string()
const
180 return std::basic_string<char, traits_type>(begin(), end());
183 template<
typename Allocator>
184 std::basic_string<value_type, traits_type, Allocator> to_string(
const Allocator& a)
const
186 return std::basic_string<value_type, traits_type, Allocator>(begin(), end(), a);
189 template<
class A>
operator std::basic_string<char, std::char_traits<char>, A>()
const
191 return std::basic_string<char, std::char_traits<char>, A>( view_ );
195 BOOST_CONSTEXPR
static const_pointer null_char_() {
return "\0";}
196 constexpr std::size_t length_impl_(std::size_t n = 0)
const BOOST_NOEXCEPT
198 return view_[n] == null_char_()[0] ? n : length_impl_(n+1);