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;
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;
129 size_type pos = 0) const noexcept;
131 size_type count) const;
135 size_type pos = npos) const noexcept;
137 size_type count) const;
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>
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>
224 template <
typename CharT,
typename Traits>
238 template <
typename CharT,
typename Traits>
252 template <
typename CharT,
typename Traits>
256 return data_ + size_;
259 template <
typename CharT,
typename Traits>
263 return reverse_iterator(
cend());
266 template <
typename CharT,
typename Traits>
267 constexpr
typename basic_string_view<CharT, Traits>::const_reverse_iterator
270 return reverse_iterator(
cend());
273 template <
typename CharT,
typename Traits>
274 constexpr
typename basic_string_view<CharT, Traits>::const_reverse_iterator
277 return reverse_iterator(
cbegin());
280 template <
typename CharT,
typename Traits>
281 constexpr
typename basic_string_view<CharT, Traits>::const_reverse_iterator
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>
323 return (std::numeric_limits<size_type>::max)();
332 template <
typename CharT,
typename Traits>
344 template <
typename CharT,
typename Traits>
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>
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>
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>
531 return find(s, pos, traits_type::length(s));
544 template <
typename CharT,
typename Traits>
547 size_type pos)
const noexcept
549 return rfind(str.data(), pos, str.size());
568 template <
typename CharT,
typename Traits>
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>
599 return rfind(s, pos, traits_type::length(s));
613 template <
typename CharT,
typename Traits>
617 return rfind(&ch, pos, 1);
629 template <
typename CharT,
typename Traits>
632 size_type pos)
const noexcept
634 return find_first_of(str.data(), pos, str.size());
650 template <
typename CharT,
typename Traits>
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>
681 return find_first_of(s, pos, traits_type::length(s));
693 template <
typename CharT,
typename Traits>
698 return find(ch, pos);
710 template <
typename CharT,
typename Traits>
716 return find_first_not_of(str.data(), pos, str.size());
732 template <
typename CharT,
typename Traits>
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>
765 return find_first_not_of(s, pos, traits_type::length(s));
777 template <
typename CharT,
typename Traits>
783 return find_first_not_of(&ch, pos, 1);
795 template <
typename CharT,
typename Traits>
798 size_type pos)
const noexcept
800 return find_last_of(str.data(), pos, str.size());
816 template <
typename CharT,
typename Traits>
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>
856 return find_last_of(s, pos, traits_type::length(s));
868 template <
typename CharT,
typename Traits>
873 return rfind(ch, pos);
885 template <
typename CharT,
typename Traits>
888 size_type pos)
const noexcept
890 return find_last_not_of(str.data(), pos, str.size());
906 template <
typename CharT,
typename Traits>
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>
940 return find_last_not_of(s, pos, traits_type::length(s));
952 template <
typename CharT,
typename Traits>
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>
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>
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>
Our partial std::string_view implementation.
Definition: string_view.hpp:46
constexpr const_iterator begin() const noexcept
Returns an iterator to the first character of the view.
Definition: string_view.hpp:214
constexpr size_type max_size() const noexcept
Returns the largest possible number of char-like objects that can be referred to by a basic_string_vi...
Definition: string_view.hpp:321
void swap(basic_string_view &v) noexcept
Exchanges the view with that of v.
Definition: string_view.hpp:440
constexpr size_type size() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:334
void remove_prefix(size_type n)
Moves the start of the view forward by n characters.
Definition: string_view.hpp:414
size_type find_first_not_of(basic_string_view str, size_type pos=0) const noexcept
Finds the first character equal to none of the characters in str.
Definition: string_view.hpp:712
int compare(size_type pos1, size_type n1, basic_string_view sv) const
Compares two character sequences.
Definition: string_view.hpp:1018
constexpr const_iterator cbegin() const noexcept
Returns an iterator to the first character of the view.
Definition: string_view.hpp:226
constexpr size_type length() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:346
size_type find_last_not_of(basic_string_view str, size_type pos=npos) const noexcept
Finds the last character equal to none of the characters in str.
Definition: string_view.hpp:887
void remove_suffix(size_type n)
Moves the end of the view back by n characters.
Definition: string_view.hpp:428
constexpr bool empty() const noexcept
Returns that view is empty or not.
Definition: string_view.hpp:308
size_type find_last_of(basic_string_view str, size_type pos=npos) const noexcept
Finds the last character equal to any of the characters in str.
Definition: string_view.hpp:797
const CharT & at(size_type pos) const
Returns reference to the character at position.
Definition: string_view.hpp:373
size_type rfind(basic_string_view str, size_type pos=npos) const noexcept
Finds the last substring equal to str.
Definition: string_view.hpp:546
constexpr const_iterator cend() const noexcept
Returns an iterator to the character following the last character of the view.
Definition: string_view.hpp:254
size_type copy(CharT *dest, size_type count, size_type pos=0) const
Copies the substring [pos, pos + rcount) to the character array pointed to by dest,...
Definition: string_view.hpp:995
constexpr basic_string_view substr(size_type pos=0, size_type count=npos) const
Returns a view of the substring [pos, pos + rcount), where rcount is the smaller of count and size() ...
Definition: string_view.hpp:973
constexpr const_reference front() const noexcept
Returns reference to the first character in the view.
Definition: string_view.hpp:401
size_type find_first_of(basic_string_view str, size_type pos=0) const noexcept
Finds the first character equal to any of the characters in str.
Definition: string_view.hpp:631
constexpr const_iterator end() const noexcept
Returns an iterator to the character following the last character of the view.
Definition: string_view.hpp:240
constexpr const_reference back() const noexcept
Returns reference to the last character in the view.
Definition: string_view.hpp:388
constexpr const CharT * data() const noexcept
Returns pointer to data stored in this pmem::obj::string_view.
Definition: string_view.hpp:296
constexpr basic_string_view() noexcept
Default constructor with empty data.
Definition: string_view.hpp:162
pmem::obj::string - persistent container with std::basic_string compatible interface.
Definition: basic_string.hpp:46
pmem::obj::array< T, N >::reverse_iterator rbegin(pmem::obj::array< T, N > &a)
Non-member rbegin.
Definition: array.hpp:869
pmem::obj::array< T, N >::const_iterator cbegin(const pmem::obj::array< T, N > &a)
Non-member cbegin.
Definition: array.hpp:789
pmem::obj::array< T, N >::const_reverse_iterator crend(const pmem::obj::array< T, N > &a)
Non-member crend.
Definition: array.hpp:819
pmem::obj::array< T, N >::const_reverse_iterator crbegin(const pmem::obj::array< T, N > &a)
Non-member crbegin.
Definition: array.hpp:809
constexpr bool operator!=(basic_string_view< CharT, Traits > lhs, typename std::common_type< basic_string_view< CharT, Traits >>::type rhs)
Non-member not equal operator.
Definition: string_view.hpp:1188
constexpr bool operator>(basic_string_view< CharT, Traits > lhs, typename std::common_type< basic_string_view< CharT, Traits >>::type rhs)
Non-member greater than operator.
Definition: string_view.hpp:1290
constexpr bool operator<=(typename std::common_type< basic_string_view< CharT, Traits >>::type lhs, basic_string_view< CharT, Traits > rhs)
Non-member less or equal operator.
Definition: string_view.hpp:1256
pmem::obj::array< T, N >::iterator end(pmem::obj::array< T, N > &a)
Non-member end.
Definition: array.hpp:849
constexpr bool operator==(typename std::common_type< basic_string_view< CharT, Traits >>::type lhs, basic_string_view< CharT, Traits > rhs)
Non-member equal operator.
Definition: string_view.hpp:1153
constexpr bool operator>=(basic_string_view< CharT, Traits > lhs, typename std::common_type< basic_string_view< CharT, Traits >>::type rhs)
Non-member greater or equal operator.
Definition: string_view.hpp:1324
constexpr bool operator<(basic_string_view< CharT, Traits > lhs, typename std::common_type< basic_string_view< CharT, Traits >>::type rhs)
Non-member less than operator.
Definition: string_view.hpp:1222
pmem::obj::array< T, N >::reverse_iterator rend(pmem::obj::array< T, N > &a)
Non-member rend.
Definition: array.hpp:889
pmem::obj::array< T, N >::const_iterator cend(const pmem::obj::array< T, N > &a)
Non-member cend.
Definition: array.hpp:799
void swap(pmem::obj::array< T, N > &lhs, pmem::obj::array< T, N > &rhs)
Non-member swap function.
Definition: array.hpp:909
Persistent memory namespace.
Definition: allocation_flag.hpp:15