47 typedef _Char value_type;
48 typedef const _Char* pointer;
49 typedef const _Char& reference;
50 typedef const _Char& const_reference;
51 typedef const _Char* const_iterator;
52 typedef const _Char* iterator;
53 typedef typename std::reverse_iterator<const_iterator> const_reverse_iterator;
54 typedef typename std::reverse_iterator<iterator> reverse_iterator;
55 typedef size_t size_type;
56 typedef ptrdiff_t difference_type;
58 static const size_type npos =
static_cast<size_type
>(-1);
67 : m_data(
nullptr), m_size(0)
71 : m_data(str), m_size(str ? traits_type::length(str) : 0)
75 : m_data(str), m_size(len)
78 template<
typename _Allocator>
79 StringViewBasic(
const std::basic_string<_Char, std::char_traits<_Char>, _Allocator>& str) noexcept
80 : m_data(str.data()), m_size(str.size())
89 constexpr size_type size()
const noexcept
93 constexpr size_type length()
const noexcept
97 constexpr bool empty()
const noexcept
103 constexpr const _Char& operator[](size_type pos)
const noexcept
108 constexpr const _Char& front()
const noexcept
113 constexpr const _Char& back()
const noexcept
115 return m_data[m_size - 1];
118 constexpr const _Char* data()
const noexcept
124 constexpr const_iterator begin()
const noexcept
128 constexpr const_iterator end()
const noexcept
130 return m_data + m_size;
132 constexpr const_iterator cbegin()
const noexcept
136 constexpr const_iterator cend()
const noexcept
138 return m_data + m_size;
141 const_reverse_iterator rbegin()
const noexcept
143 return const_reverse_iterator(end());
145 const_reverse_iterator rend()
const noexcept
147 return const_reverse_iterator(begin());
149 const_reverse_iterator crbegin()
const noexcept
151 return const_reverse_iterator(end());
153 const_reverse_iterator crend()
const noexcept
155 return const_reverse_iterator(begin());
159 void remove_prefix(size_type n)
noexcept
161 if (n > m_size) n = m_size;
166 void remove_suffix(size_type n)
noexcept
168 if (n > m_size) n = m_size;
174 std::swap(m_data, other.m_data);
175 std::swap(m_size, other.m_size);
179 StringViewBasic substr(size_type pos = 0, size_type count = npos)
const
185 size_type rlen = (std::min)(count, m_size - pos);
192 size_type len = (std::min)(m_size, other.m_size);
193 int result = traits_type::compare(m_data, other.m_data, len);
194 if (result != 0)
return result;
195 if (m_size < other.m_size)
return -1;
196 if (m_size > other.m_size)
return 1;
202 return m_size == other.m_size &&
203 (m_data == other.m_data ||
204 traits_type::compare(m_data, other.m_data, m_size) == 0);
209 return !(*
this == other);
214 return compare(other) < 0;
219 return compare(other) <= 0;
224 return compare(other) > 0;
229 return compare(other) >= 0;
233 size_type find(_Char ch, size_type pos = 0)
const noexcept
235 if (pos >= m_size)
return npos;
236 const _Char* result = traits_type::find(m_data + pos, m_size - pos, ch);
237 return result ?
static_cast<size_type
>(result - m_data) : npos;
242 if (pos > m_size || str.size() + pos> m_size)
245 const _Char* start = m_data + pos;
246 const _Char* end = m_data + m_size - str.size() + 1;
248 for (
const _Char* p = start; p < end; ++p) {
249 if (traits_type::compare(p, str.data(), str.size()) == 0) {
250 return static_cast<size_type
>(p - m_data);
257 size_type rfind(_Char ch, size_type pos = npos)
const noexcept
259 if (m_size == 0)
return npos;
260 if (pos >= m_size) pos = m_size - 1;
262 for (size_type i = pos + 1; i > 0; --i) {
263 if (m_data[i - 1] == ch) {
272 size_type find_first_of(_Char ch, size_type pos = 0)
const noexcept
274 return find(ch, pos);
277 size_type find_first_of(
StringViewBasic str, size_type pos = 0)
const noexcept
279 if (pos >= m_size || str.empty())
return npos;
281 for (size_type i = pos; i < m_size; ++i) {
282 for (size_type j = 0; j < str.size(); ++j) {
283 if (m_data[i] == str[j]) {
293 size_type find_last_of(_Char ch, size_type pos = npos)
const noexcept
295 return rfind(ch, pos);
298 size_type find_last_of(
StringViewBasic str, size_type pos = npos)
const noexcept
300 if (m_size == 0 || str.empty())
return npos;
301 if (pos >= m_size) pos = m_size - 1;
303 for (size_type i = pos + 1; i > 0; --i) {
304 for (size_type j = 0; j < str.size(); ++j) {
305 if (m_data[i - 1] == str[j]) {
317 return m_size >= str.size() &&
318 traits_type::compare(m_data, str.data(), str.size()) == 0;
321 bool starts_with(_Char ch)
const noexcept
323 return !empty() && front() == ch;
328 return m_size >= str.size() &&
329 traits_type::compare(m_data + m_size - str.size(), str.data(), str.size()) == 0;
332 bool ends_with(_Char ch)
const noexcept
334 return !empty() && back() == ch;
345 operator std::basic_string<_Char>()
const noexcept
347 return std::basic_string<_Char>(m_data, m_size);
350 typedef std::char_traits<_Char> traits_type;