9 #ifndef LIBPMEMOBJ_CPP_STRING_VIEW
10 #define LIBPMEMOBJ_CPP_STRING_VIEW
18 #if __cpp_lib_string_view
19 #include <string_view>
28 #if __cpp_lib_string_view
30 template <
typename CharT,
typename Traits = std::
char_traits<CharT>>
31 using basic_string_view = std::basic_string_view<CharT, Traits>;
32 using string_view = std::string_view;
33 using wstring_view = std::basic_string_view<wchar_t>;
34 using u16string_view = std::basic_string_view<char16_t>;
35 using u32string_view = std::basic_string_view<char32_t>;
45 template <
typename CharT,
typename Traits = std::
char_traits<CharT>>
49 using traits_type = Traits;
50 using value_type = CharT;
51 using size_type = std::size_t;
52 using difference_type = std::ptrdiff_t;
53 using reference = value_type &;
54 using const_reference =
const value_type &;
55 using pointer = value_type *;
56 using const_pointer =
const value_type *;
57 using const_iterator = const_pointer;
58 using iterator = const_iterator;
59 using reverse_iterator = std::reverse_iterator<const_iterator>;
60 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
62 static constexpr
const size_type npos =
63 (std::numeric_limits<size_type>::max)();
75 constexpr const_iterator
begin()
const noexcept;
76 constexpr const_iterator
cbegin()
const noexcept;
77 constexpr const_iterator
end()
const noexcept;
78 constexpr const_iterator
cend()
const noexcept;
79 constexpr const_reverse_iterator rbegin()
const noexcept;
80 constexpr const_reverse_iterator crbegin()
const noexcept;
81 constexpr const_reverse_iterator rend()
const noexcept;
82 constexpr const_reverse_iterator crend()
const noexcept;
84 constexpr
const CharT *
data()
const noexcept;
85 constexpr size_type
size()
const noexcept;
86 constexpr size_type
length()
const noexcept;
87 constexpr
bool empty()
const noexcept;
88 constexpr size_type
max_size()
const noexcept;
90 const CharT &
at(size_type pos)
const;
91 constexpr
const CharT &
operator[](size_type pos)
const noexcept;
92 constexpr const_reference
front()
const noexcept;
93 constexpr const_reference
back()
const noexcept;
100 size_type count = npos)
const;
101 size_type
copy(CharT *dest, size_type count, size_type pos = 0)
const;
102 inline int compare(size_type pos1, size_type n1,
105 size_type pos2, size_type n2)
const;
106 inline int compare(
const CharT *s)
const noexcept;
107 inline int compare(size_type pos1, size_type n1,
const CharT *s)
const;
108 inline int compare(size_type pos1, size_type n1,
const CharT *s,
113 size_type find(CharT ch, size_type pos = 0)
const noexcept;
114 size_type find(
const CharT *s, size_type pos = 0)
const;
115 size_type find(
const CharT *s, size_type pos, size_type count)
const;
119 size_type
rfind(
const CharT *s, size_type pos, size_type count)
const;
120 size_type
rfind(
const CharT *s, size_type pos = npos)
const;
121 size_type
rfind(CharT ch, size_type pos = npos)
const noexcept;
125 size_type count)
const;
126 size_type
find_first_of(
const CharT *s, size_type pos = 0)
const;
127 size_type
find_first_of(CharT ch, size_type pos = 0)
const noexcept;
129 size_type pos = 0)
const noexcept;
131 size_type count)
const;
135 size_type pos = npos)
const noexcept;
137 size_type count)
const;
138 size_type
find_last_of(
const CharT *s, size_type pos = npos)
const;
139 size_type
find_last_of(CharT ch, size_type pos = npos)
const noexcept;
141 size_type pos = npos)
const noexcept;
143 size_type count)
const;
149 const value_type *data_;
161 template <
typename CharT,
typename Traits>
163 : data_(
nullptr), size_(0)
174 template <
typename CharT,
typename Traits>
176 const CharT *data, size_type size)
177 : data_(data), size_(size)
186 template <
typename CharT,
typename Traits>
188 const std::basic_string<CharT, Traits> &s)
189 : data_(s.c_str()), size_(s.size())
200 template <
typename CharT,
typename Traits>
203 : data_(data), size_(Traits::length(data))
212 template <
typename CharT,
typename Traits>
213 constexpr
typename basic_string_view<CharT, Traits>::const_iterator
224 template <
typename CharT,
typename Traits>
225 constexpr
typename basic_string_view<CharT, Traits>::const_iterator
238 template <
typename CharT,
typename Traits>
239 constexpr
typename basic_string_view<CharT, Traits>::const_iterator
252 template <
typename CharT,
typename Traits>
253 constexpr
typename basic_string_view<CharT, Traits>::const_iterator
256 return data_ + size_;
259 template <
typename CharT,
typename Traits>
260 constexpr
typename basic_string_view<CharT, Traits>::const_reverse_iterator
263 return reverse_iterator(
cend());
266 template <
typename CharT,
typename Traits>
267 constexpr
typename basic_string_view<CharT, Traits>::const_reverse_iterator
268 basic_string_view<CharT, Traits>::crbegin() const noexcept
270 return reverse_iterator(
cend());
273 template <
typename CharT,
typename Traits>
274 constexpr
typename basic_string_view<CharT, Traits>::const_reverse_iterator
275 basic_string_view<CharT, Traits>::rend() const noexcept
277 return reverse_iterator(
cbegin());
280 template <
typename CharT,
typename Traits>
281 constexpr
typename basic_string_view<CharT, Traits>::const_reverse_iterator
282 basic_string_view<CharT, Traits>::crend() const noexcept
284 return reverse_iterator(
cbegin());
294 template <
typename CharT,
typename Traits>
295 constexpr
inline const CharT *
306 template <
typename CharT,
typename Traits>
307 constexpr
inline bool
319 template <
typename CharT,
typename Traits>
320 constexpr
inline typename basic_string_view<CharT, Traits>::size_type
323 return (std::numeric_limits<size_type>::max)();
332 template <
typename CharT,
typename Traits>
333 constexpr
inline typename basic_string_view<CharT, Traits>::size_type
344 template <
typename CharT,
typename Traits>
345 constexpr
inline typename basic_string_view<CharT, Traits>::size_type
356 template <
typename CharT,
typename Traits>
357 constexpr
inline const CharT &
371 template <
typename CharT,
typename Traits>
376 throw std::out_of_range(
"Accessing a position out of bounds!");
386 template <
typename CharT,
typename Traits>
387 constexpr
inline const CharT &
390 return operator[](size() - 1);
399 template <
typename CharT,
typename Traits>
400 constexpr
inline const CharT &
403 return operator[](0);
412 template <
typename CharT,
typename Traits>
426 template <
typename CharT,
typename Traits>
438 template <
typename CharT,
typename Traits>
456 template <
typename CharT,
typename Traits>
457 typename basic_string_view<CharT, Traits>::size_type
459 size_type pos)
const noexcept
461 return find(str.data(), pos, str.size());
473 template <
typename CharT,
typename Traits>
474 typename basic_string_view<CharT, Traits>::size_type
477 return find(&ch, pos, 1);
491 template <
typename CharT,
typename Traits>
492 typename basic_string_view<CharT, Traits>::size_type
494 size_type count)
const
504 while (pos + count <= sz) {
505 auto found = traits_type::find(data() + pos, sz - pos, s[0]);
508 pos =
static_cast<size_type
>(std::distance(data(), found));
509 if (traits_type::compare(found, s, count) == 0) {
527 template <
typename CharT,
typename Traits>
528 typename basic_string_view<CharT, Traits>::size_type
531 return find(s, pos, traits_type::length(s));
544 template <
typename CharT,
typename Traits>
545 typename basic_string_view<CharT, Traits>::size_type
547 size_type pos)
const noexcept
549 return rfind(str.data(), pos, str.size());
568 template <
typename CharT,
typename Traits>
569 typename basic_string_view<CharT, Traits>::size_type
571 size_type count)
const
573 if (count <= size()) {
574 pos = (std::min)(size() - count, pos);
576 if (traits_type::compare(data() + pos, s, count) == 0)
595 template <
typename CharT,
typename Traits>
596 typename basic_string_view<CharT, Traits>::size_type
599 return rfind(s, pos, traits_type::length(s));
613 template <
typename CharT,
typename Traits>
614 typename basic_string_view<CharT, Traits>::size_type
617 return rfind(&ch, pos, 1);
629 template <
typename CharT,
typename Traits>
630 typename basic_string_view<CharT, Traits>::size_type
632 size_type pos)
const noexcept
634 return find_first_of(str.data(), pos, str.size());
650 template <
typename CharT,
typename Traits>
651 typename basic_string_view<CharT, Traits>::size_type
653 size_type count)
const
655 size_type first_of = npos;
656 for (
const CharT *c = s; c != s + count; ++c) {
657 size_type found = find(*c, pos);
658 if (found != npos && found < first_of)
676 template <
typename CharT,
typename Traits>
677 typename basic_string_view<CharT, Traits>::size_type
681 return find_first_of(s, pos, traits_type::length(s));
693 template <
typename CharT,
typename Traits>
694 typename basic_string_view<CharT, Traits>::size_type
698 return find(ch, pos);
710 template <
typename CharT,
typename Traits>
711 typename basic_string_view<CharT, Traits>::size_type
716 return find_first_not_of(str.data(), pos, str.size());
732 template <
typename CharT,
typename Traits>
733 typename basic_string_view<CharT, Traits>::size_type
736 size_type count)
const
741 for (
auto it =
cbegin() + pos; it !=
cend(); ++it)
742 if (!traits_type::find(s, count, *it))
743 return static_cast<size_type
>(
744 std::distance(
cbegin(), it));
760 template <
typename CharT,
typename Traits>
761 typename basic_string_view<CharT, Traits>::size_type
765 return find_first_not_of(s, pos, traits_type::length(s));
777 template <
typename CharT,
typename Traits>
778 typename basic_string_view<CharT, Traits>::size_type
783 return find_first_not_of(&ch, pos, 1);
795 template <
typename CharT,
typename Traits>
796 typename basic_string_view<CharT, Traits>::size_type
798 size_type pos)
const noexcept
800 return find_last_of(str.data(), pos, str.size());
816 template <
typename CharT,
typename Traits>
817 typename basic_string_view<CharT, Traits>::size_type
819 size_type count)
const
821 if (size() == 0 || count == 0)
825 size_type last_of = 0;
826 for (
const CharT *c = s; c != s + count; ++c) {
827 size_type position = rfind(*c, pos);
828 if (position != npos) {
830 if (position > last_of)
851 template <
typename CharT,
typename Traits>
852 typename basic_string_view<CharT, Traits>::size_type
856 return find_last_of(s, pos, traits_type::length(s));
868 template <
typename CharT,
typename Traits>
869 typename basic_string_view<CharT, Traits>::size_type
873 return rfind(ch, pos);
885 template <
typename CharT,
typename Traits>
886 typename basic_string_view<CharT, Traits>::size_type
888 size_type pos)
const noexcept
890 return find_last_not_of(str.data(), pos, str.size());
906 template <
typename CharT,
typename Traits>
907 typename basic_string_view<CharT, Traits>::size_type
910 size_type count)
const
913 pos = (std::min)(pos, size() - 1);
915 if (!traits_type::find(s, count, *(data() + pos)))
935 template <
typename CharT,
typename Traits>
936 typename basic_string_view<CharT, Traits>::size_type
940 return find_last_not_of(s, pos, traits_type::length(s));
952 template <
typename CharT,
typename Traits>
953 typename basic_string_view<CharT, Traits>::size_type
955 size_type pos)
const noexcept
957 return find_last_not_of(&ch, pos, 1);
971 template <
typename CharT,
typename Traits>
976 ?
throw std::out_of_range(
"string_view::substr")
978 (std::min)(count, size() - pos));
993 template <
typename CharT,
typename Traits>
994 typename basic_string_view<CharT, Traits>::size_type
999 throw std::out_of_range(
"string_view::copy");
1000 size_type rlen = (std::min)(count, size() - pos);
1001 Traits::copy(dest, data() + pos, rlen);
1016 template <
typename CharT,
typename Traits>
1021 return substr(pos1, n1).compare(sv);
1037 template <
typename CharT,
typename Traits>
1043 return substr(pos1, n1).compare(sv.
substr(pos2, n2));
1055 template <
typename CharT,
typename Traits>
1073 template <
typename CharT,
typename Traits>
1076 const CharT *s)
const
1093 template <
typename CharT,
typename Traits>
1096 const CharT *s, size_type n2)
const
1109 template <
typename CharT,
typename Traits>
1114 int ret = Traits::compare(data(), other.data(),
1115 (std::min)(size(), other.size()));
1118 if (size() < other.size())
1120 if (size() > other.size())
1128 template <
class CharT,
class Traits>
1139 template <
class CharT,
class Traits>
1145 return (lhs.
size() != rhs.size() ?
false : lhs.
compare(rhs) == 0);
1151 template <
class CharT,
class Traits>
1157 return (lhs.size() != rhs.
size() ?
false : lhs.compare(rhs) == 0);
1163 template <
class CharT,
class Traits>
1174 template <
class CharT,
class Traits>
1180 return (lhs.size() != rhs.
size() ?
true : lhs.compare(rhs) != 0);
1186 template <
class CharT,
class Traits>
1192 return (lhs.
size() != rhs.size() ?
true : lhs.
compare(rhs) != 0);
1198 template <
class CharT,
class Traits>
1209 template <
class CharT,
class Traits>
1214 return lhs.compare(rhs) < 0;
1220 template <
class CharT,
class Traits>
1231 template <
class CharT,
class Traits>
1242 template <
class CharT,
class Traits>
1254 template <
class CharT,
class Traits>
1260 return lhs.compare(rhs) <= 0;
1266 template <
class CharT,
class Traits>
1277 template <
class CharT,
class Traits>
1282 return lhs.compare(rhs) > 0;
1288 template <
class CharT,
class Traits>
1299 template <
class CharT,
class Traits>
1310 template <
class CharT,
class Traits>
1316 return lhs.compare(rhs) >= 0;
1322 template <
class CharT,
class Traits>