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_
21 #include "../iutest_defs.hpp"
23 #if IUTEST_HAS_CXX_HDR_STRING_VIEW
24 # include <string_view>
27 #if IUTEST_HAS_EXCEPTIONS
40 #if !IUTEST_USE_OWN_STRING_VIEW
42 template<
typename CharT,
typename Traits = ::std::
char_traits<CharT> >
43 using iu_basic_string_view = ::std::basic_string_view<CharT, Traits>;
47 template <
class CharT,
class Traits = ::std::
char_traits<CharT> >
48 class iu_basic_string_view;
50 template<
typename CharT,
typename Traits>
51 class iu_basic_string_view
54 typedef Traits traits_type;
55 typedef CharT value_type;
56 typedef value_type* pointer;
57 typedef const value_type* const_pointer;
58 typedef value_type& reference;
59 typedef const value_type& const_reference;
64 typedef size_t size_type;
65 typedef ::std::ptrdiff_t difference_type;
68 static const size_type npos =
static_cast<size_type
>(-1);
71 static const size_type size_type_max =
static_cast<size_type
>(
72 #if defined(PTRDIFF_MAX)
81 : m_data(IUTEST_NULLPTR)
87 , m_size(traits_type::length(str))
95 #if IUTEST_HAS_DEFAULT_FUNCTIONS
106 iu_basic_string_view(value_type(&str)[N])
112 template<
typename Allocator>
113 iu_basic_string_view(const ::std::basic_string<value_type, traits_type, Allocator>& str)
115 , m_size(str.length())
122 #if IUTEST_HAS_DEFAULT_FUNCTIONS
123 iu_basic_string_view& operator=(
const iu_basic_string_view&) =
default;
124 #if IUTEST_HAS_MOVE_ASSIGNMENT_DEFAULT_FUNCTION
125 iu_basic_string_view& operator=(iu_basic_string_view&&) =
default;
128 iu_basic_string_view& operator=(
const iu_basic_string_view& rhs)
147 return (::std::min)(size_type_max,
static_cast<size_type
>(-1) /
sizeof(value_type));
159 IUTEST_CXX14_CONSTEXPR const_reference at(size_type pos)
const
161 offset_exclusive(pos);
178 IUTEST_PRAGMA_WARN_PUSH()
179 IUTEST_PRAGMA_WARN_DISABLE_CXX14_CONSTEXPR_NOT_IMPLY_CONST()
194 const iu_basic_string_view tmp = { other };
199 IUTEST_PRAGMA_WARN_POP()
202 size_type copy(pointer s, size_type n, size_type pos = 0)
const
204 offset_exclusive(pos);
205 const size_type count = clamp_suffix_size(pos, n);
206 traits_type::copy(s, m_data + pos, count);
210 IUTEST_CXX14_CONSTEXPR iu_basic_string_view substr(size_type pos = 0, size_type n = npos)
const
212 offset_exclusive(pos);
213 const size_type count = clamp_suffix_size(pos, n);
214 return iu_basic_string_view(m_data + pos, count);
221 return m_size == sv.m_size && (traits_type::compare(m_data, sv.m_data, m_size) == 0);
228 const size_type count = (::std::min)(m_size, sv.m_size);
229 const int result = traits_type::compare(m_data, sv.m_data, count);
235 if( m_size < sv.m_size )
239 if( m_size > sv.m_size )
248 return substr(pos1, n1).compare(sv);
251 IUTEST_CXX_CONSTEXPR int compare(size_type pos1, size_type n1, iu_basic_string_view sv, size_type pos2, size_type n2)
const
253 return substr(pos1, n1).compare(sv.substr(pos2, n2));
258 return compare(basic_string_view(s));
263 return substr(pos1, n1).compare(basic_string_view(s));
266 IUTEST_CXX_CONSTEXPR int compare(size_type pos1, size_type n1, const_pointer s, size_type n2)
const
268 return substr(pos1, n1).compare(basic_string_view(s, n2));
274 return compare(0, npos, x) == 0;
278 return starts_with(iu_basic_string_view(&x, 1));
282 return starts_with(iu_basic_string_view(x));
287 return m_size >= x.m_size && compare(m_size - x.m_size, npos, x) == 0;
291 return ends_with(iu_basic_string_view(&x, 1));
295 return ends_with(iu_basic_string_view(x));
301 if( (sv.m_size > m_size) || (pos > m_size - sv.m_size) )
309 const_pointer end = m_data + (m_size - sv.m_size) + 1;
310 for( const_pointer top = m_data + pos; ; ++top )
312 top = traits_type::find(top,
static_cast<size_type
>(end - top), sv[0]);
318 if( traits_type::compare(top, sv.m_data, sv.m_size) == 0 )
320 return static_cast<size_type
>(top - m_data);
328 const_pointer find = traits_type::find(m_data + pos, m_size, c);
329 if( find != IUTEST_NULLPTR )
331 return static_cast<size_type
>(find - m_data);
339 return find(iu_basic_string_view(s, n), pos);
343 return find(iu_basic_string_view(s), pos);
369 size_type clamp_suffix_size(size_type pos, size_type n)
const
371 return (::std::min)(n, m_size - pos);
374 void offset_exclusive(size_type pos)
const
382 void out_of_range()
const
384 #if IUTEST_HAS_EXCEPTIONS
385 throw new ::std::out_of_range(
"invalid string_view position");
390 friend bool operator == (
const iu_basic_string_view lhs,
const iu_basic_string_view& rhs)
392 return lhs.equal(rhs);
394 friend bool operator != (
const iu_basic_string_view lhs,
const iu_basic_string_view& rhs)
396 return !lhs.equal(rhs);
400 const_pointer m_data;
406 template<
typename CharT,
typename Traits = ::std::
char_traits<CharT> >
407 class iu_nullable_basic_string_view :
public iu_basic_string_view<CharT, Traits>
410 typedef iu_basic_string_view<CharT, Traits> _Mybase;
411 typedef Traits traits_type;
412 typedef CharT value_type;
413 typedef value_type* pointer;
414 typedef const value_type* const_pointer;
415 typedef size_t size_type;
418 #if IUTEST_HAS_NULLPTR
420 : _Mybase(IUTEST_NULLPTR, 0)
425 : _Mybase(str, str ? traits_type::length(str) : 0)
432 #if IUTEST_HAS_DEFAULT_FUNCTIONS
442 iu_nullable_basic_string_view(value_type(&str)[N])
447 iu_nullable_basic_string_view(
const _Mybase& str_view)
452 template<
typename Allocator>
453 iu_nullable_basic_string_view(const ::std::basic_string<value_type, traits_type, Allocator>& str)
454 : _Mybase(str.data(), str.length())
460 typedef iu_basic_string_view<char> iu_string_view;
461 typedef iu_basic_string_view<wchar_t> iu_wstring_view;
#define IUTEST_CXX_CONSTEXPR
constexpr
Definition: iutest_compiler.hpp:372
#define IUTEST_CXX_NOEXCEPT_SPEC
noexcept specification definition
Definition: iutest_compiler.hpp:811
#define IUTEST_CXX_DEFAULT_FUNCTION
default function
Definition: iutest_compiler.hpp:494
iutest root namespace
Definition: iutest_charcode.hpp:33