15 #ifndef INCG_IRIS_IUTEST_STRING_VIEW_HPP_46AEE8A4_996C_4925_ACBA_2A511909B38F_
16 #define INCG_IRIS_IUTEST_STRING_VIEW_HPP_46AEE8A4_996C_4925_ACBA_2A511909B38F_
18 #include "../iutest_defs.hpp"
20 #if IUTEST_HAS_CXX_HDR_STRING_VIEW
21 # include <string_view>
24 #if IUTEST_HAS_EXCEPTIONS
33 #if !IUTEST_USE_OWN_STRING_VIEW
35 template<
typename CharT,
typename Traits = ::std::
char_traits<CharT> >
36 using iu_basic_string_view = ::std::basic_string_view<CharT, Traits>;
40 template <
class CharT,
class Traits = ::std::
char_traits<CharT> >
41 class iu_basic_string_view;
43 template<
typename CharT,
typename Traits>
44 class iu_basic_string_view
47 typedef Traits traits_type;
48 typedef CharT value_type;
49 typedef value_type* pointer;
50 typedef const value_type* const_pointer;
51 typedef value_type& reference;
52 typedef const value_type& const_reference;
57 typedef size_t size_type;
58 typedef ::std::ptrdiff_t difference_type;
61 static const size_type npos =
static_cast<size_type
>(-1);
64 static const size_type size_type_max =
static_cast<size_type
>(
65 #if defined(PTRDIFF_MAX)
74 : m_data(IUTEST_NULLPTR)
80 , m_size(traits_type::length(str))
88 #if IUTEST_HAS_DEFAULT_FUNCTIONS
99 iu_basic_string_view(value_type(&str)[N])
105 template<
typename Allocator>
106 iu_basic_string_view(const ::std::basic_string<value_type, traits_type, Allocator>& str)
108 , m_size(str.length())
115 #if IUTEST_HAS_DEFAULT_FUNCTIONS
116 iu_basic_string_view& operator=(
const iu_basic_string_view&) =
default;
117 #if IUTEST_HAS_MOVE_ASSIGNMENT_DEFAULT_FUNCTION
118 iu_basic_string_view& operator=(iu_basic_string_view&&) =
default;
121 iu_basic_string_view& operator=(
const iu_basic_string_view& rhs)
140 return (::std::min)(size_type_max,
static_cast<size_type
>(-1) /
sizeof(value_type));
152 IUTEST_CXX14_CONSTEXPR const_reference at(size_type pos)
const
154 offset_exclusive(pos);
171 IUTEST_PRAGMA_WARN_PUSH()
172 IUTEST_PRAGMA_WARN_CXX14_CONSTEXPR_NOT_IMPLY_CONST()
187 const iu_basic_string_view tmp = { other };
192 IUTEST_PRAGMA_WARN_POP()
195 size_type copy(pointer s, size_type n, size_type pos = 0)
const
197 offset_exclusive(pos);
198 const size_type count = clamp_suffix_size(pos, n);
199 traits_type::copy(s, m_data + pos, count);
203 IUTEST_CXX14_CONSTEXPR iu_basic_string_view substr(size_type pos = 0, size_type n = npos)
const
205 offset_exclusive(pos);
206 const size_type count = clamp_suffix_size(pos, n);
207 return iu_basic_string_view(m_data + pos, count);
214 return m_size == sv.m_size && (traits_type::compare(m_data, sv.m_data, m_size) == 0);
221 const size_type count = (::std::min)(m_size, sv.m_size);
222 const int result = traits_type::compare(m_data, sv.m_data, count);
228 if( m_size < sv.m_size )
232 if( m_size > sv.m_size )
241 return substr(pos1, n1).compare(sv);
244 IUTEST_CXX_CONSTEXPR int compare(size_type pos1, size_type n1, iu_basic_string_view sv, size_type pos2, size_type n2)
const
246 return substr(pos1, n1).compare(sv.substr(pos2, n2));
251 return compare(basic_string_view(s));
256 return substr(pos1, n1).compare(basic_string_view(s));
259 IUTEST_CXX_CONSTEXPR int compare(size_type pos1, size_type n1, const_pointer s, size_type n2)
const
261 return substr(pos1, n1).compare(basic_string_view(s, n2));
267 return compare(0, npos, x) == 0;
271 return starts_with(iu_basic_string_view(&x, 1));
275 return starts_with(iu_basic_string_view(x));
280 return m_size >= x.m_size && compare(m_size - x.m_size, npos, x) == 0;
284 return ends_with(iu_basic_string_view(&x, 1));
288 return ends_with(iu_basic_string_view(x));
294 if( (sv.m_size > m_size) || (pos > m_size - sv.m_size) )
302 const_pointer end = m_data + (m_size - sv.m_size) + 1;
303 for( const_pointer top = m_data + pos; ; ++top )
305 top = traits_type::find(top,
static_cast<size_type
>(end - top), sv[0]);
311 if( traits_type::compare(top, sv.m_data, sv.m_size) == 0 )
313 return static_cast<size_type
>(top - m_data);
321 const_pointer find = traits_type::find(m_data + pos, m_size, c);
322 if( find != IUTEST_NULLPTR )
324 return static_cast<size_type
>(find - m_data);
332 return find(iu_basic_string_view(s, n), pos);
336 return find(iu_basic_string_view(s), pos);
362 size_type clamp_suffix_size(size_type pos, size_type n)
const
364 return (::std::min)(n, m_size - pos);
367 void offset_exclusive(size_type pos)
const
375 void out_of_range()
const
377 #if IUTEST_HAS_EXCEPTIONS
378 throw new ::std::out_of_range(
"invalid string_view position");
383 friend bool operator == (
const iu_basic_string_view lhs,
const iu_basic_string_view& rhs)
385 return lhs.equal(rhs);
387 friend bool operator != (
const iu_basic_string_view lhs,
const iu_basic_string_view& rhs)
389 return !lhs.equal(rhs);
393 const_pointer m_data;
399 template<
typename CharT,
typename Traits = ::std::
char_traits<CharT> >
400 class iu_nullable_basic_string_view :
public iu_basic_string_view<CharT, Traits>
403 typedef iu_basic_string_view<CharT, Traits> _Mybase;
404 typedef Traits traits_type;
405 typedef CharT value_type;
406 typedef value_type* pointer;
407 typedef const value_type* const_pointer;
408 typedef size_t size_type;
411 #if IUTEST_HAS_NULLPTR
413 : _Mybase(IUTEST_NULLPTR, 0)
418 : _Mybase(str, str ? traits_type::length(str) : 0)
425 #if IUTEST_HAS_DEFAULT_FUNCTIONS
435 iu_nullable_basic_string_view(value_type(&str)[N])
440 iu_nullable_basic_string_view(
const _Mybase& str_view)
445 template<
typename Allocator>
446 iu_nullable_basic_string_view(const ::std::basic_string<value_type, traits_type, Allocator>& str)
447 : _Mybase(str.data(), str.length())
453 typedef iu_basic_string_view<char> iu_string_view;
454 typedef iu_basic_string_view<wchar_t> iu_wstring_view;
460 #endif // INCG_IRIS_IUTEST_STRING_VIEW_HPP_46AEE8A4_996C_4925_ACBA_2A511909B38F_