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>;
47 template <
typename CharT,
typename Traits = std::
char_traits<CharT>>
51 using traits_type = Traits;
52 using value_type = CharT;
53 using size_type = std::size_t;
54 using difference_type = std::ptrdiff_t;
55 using reference = value_type &;
56 using const_reference =
const value_type &;
57 using pointer = value_type *;
58 using const_pointer =
const value_type *;
59 using const_iterator = const_pointer;
60 using iterator = const_iterator;
61 using reverse_iterator = std::reverse_iterator<const_iterator>;
62 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
64 static constexpr
const size_type npos =
65 (std::numeric_limits<size_type>::max)();
87 constexpr const_iterator
begin() const noexcept;
88 constexpr const_iterator
cbegin() const noexcept;
89 constexpr const_iterator
end() const noexcept;
90 constexpr const_iterator
cend() const noexcept;
91 constexpr const_reverse_iterator
rbegin() const noexcept;
92 constexpr const_reverse_iterator
crbegin() const noexcept;
93 constexpr const_reverse_iterator
rend() const noexcept;
94 constexpr const_reverse_iterator
crend() const noexcept;
96 constexpr const CharT *
data() const noexcept;
97 constexpr size_type
size() const noexcept;
98 constexpr size_type
length() const noexcept;
99 constexpr
bool empty() const noexcept;
102 const CharT &
at(size_type pos) const;
103 constexpr const CharT &operator[](size_type pos) const noexcept;
104 constexpr const_reference
front() const noexcept;
105 constexpr const_reference
back() const noexcept;
112 size_type count = npos) const;
113 size_type
copy(CharT *dest, size_type count, size_type pos = 0) const;
114 inline
int compare(size_type pos1, size_type n1,
117 size_type pos2, size_type n2) const;
118 inline
int compare(const CharT *s) const noexcept;
119 inline
int compare(size_type pos1, size_type n1, const CharT *s) const;
120 inline
int compare(size_type pos1, size_type n1, const CharT *s,
125 size_type
find(CharT ch, size_type pos = 0) const noexcept;
126 size_type
find(const CharT *s, size_type pos = 0) const;
127 size_type
find(const CharT *s, size_type pos, size_type count) const;
131 size_type
rfind(const CharT *s, size_type pos, size_type count) const;
132 size_type
rfind(const CharT *s, size_type pos = npos) const;
133 size_type
rfind(CharT ch, size_type pos = npos) const noexcept;
137 size_type count) const;
141 size_type pos = 0) const noexcept;
143 size_type count) const;
147 size_type pos = npos) const noexcept;
149 size_type count) const;
153 size_type pos = npos) const noexcept;
155 size_type count) const;
161 const value_type *data_;
192 template <typename CharT, typename Traits>
194 : data_(
nullptr), size_(0)
205 template <
typename CharT,
typename Traits>
217 template <
typename CharT,
typename Traits>
219 const std::basic_string<CharT, Traits> &s)
220 : data_(s.c_str()), size_(s.size())
231 template <
typename CharT,
typename Traits>
234 : data_(data), size_(Traits::length(data))
243 template <
typename CharT,
typename Traits>
255 template <
typename CharT,
typename Traits>
269 template <
typename CharT,
typename Traits>
283 template <
typename CharT,
typename Traits>
287 return data_ + size_;
297 template <
typename CharT,
typename Traits>
301 return reverse_iterator(
cend());
311 template <
typename CharT,
typename Traits>
315 return reverse_iterator(
cend());
327 template <
typename CharT,
typename Traits>
331 return reverse_iterator(
cbegin());
343 template <
typename CharT,
typename Traits>
347 return reverse_iterator(
cbegin());
357 template <
typename CharT,
typename Traits>
358 constexpr
inline const CharT *
369 template <
typename CharT,
typename Traits>
370 constexpr
inline bool
382 template <
typename CharT,
typename Traits>
386 return (std::numeric_limits<size_type>::max)();
395 template <
typename CharT,
typename Traits>
407 template <
typename CharT,
typename Traits>
419 template <
typename CharT,
typename Traits>
420 constexpr
inline const CharT &
434 template <
typename CharT,
typename Traits>
439 throw std::out_of_range(
"Accessing a position out of bounds!");
449 template <
typename CharT,
typename Traits>
450 constexpr
inline const CharT &
453 return operator[](size() - 1);
462 template <
typename CharT,
typename Traits>
463 constexpr
inline const CharT &
466 return operator[](0);
475 template <
typename CharT,
typename Traits>
489 template <
typename CharT,
typename Traits>
501 template <
typename CharT,
typename Traits>
506 std::swap(data_, v.data_);
507 std::swap(size_, v.size_);
519 template <
typename CharT,
typename Traits>
522 size_type pos)
const noexcept
524 return find(str.data(), pos, str.size());
536 template <
typename CharT,
typename Traits>
540 return find(&ch, pos, 1);
554 template <
typename CharT,
typename Traits>
557 size_type count)
const
567 while (pos + count <= sz) {
568 auto found = traits_type::find(data() + pos, sz - pos, s[0]);
571 pos =
static_cast<size_type
>(std::distance(data(), found));
572 if (traits_type::compare(found, s, count) == 0) {
590 template <
typename CharT,
typename Traits>
594 return find(s, pos, traits_type::length(s));
607 template <
typename CharT,
typename Traits>
610 size_type pos)
const noexcept
612 return rfind(str.data(), pos, str.size());
631 template <
typename CharT,
typename Traits>
634 size_type count)
const
636 if (count <= size()) {
637 pos = (std::min)(size() - count, pos);
639 if (traits_type::compare(data() + pos, s, count) == 0)
658 template <
typename CharT,
typename Traits>
662 return rfind(s, pos, traits_type::length(s));
676 template <
typename CharT,
typename Traits>
680 return rfind(&ch, pos, 1);
692 template <
typename CharT,
typename Traits>
695 size_type pos)
const noexcept
697 return find_first_of(str.data(), pos, str.size());
713 template <
typename CharT,
typename Traits>
716 size_type count)
const
718 size_type first_of = npos;
719 for (
const CharT *c = s; c != s + count; ++c) {
720 size_type found = find(*c, pos);
721 if (found != npos && found < first_of)
739 template <
typename CharT,
typename Traits>
744 return find_first_of(s, pos, traits_type::length(s));
756 template <
typename CharT,
typename Traits>
761 return find(ch, pos);
773 template <
typename CharT,
typename Traits>
779 return find_first_not_of(str.data(), pos, str.size());
795 template <
typename CharT,
typename Traits>
799 size_type count)
const
804 for (
auto it =
cbegin() + pos; it !=
cend(); ++it)
805 if (!traits_type::find(s, count, *it))
806 return static_cast<size_type
>(
807 std::distance(
cbegin(), it));
823 template <
typename CharT,
typename Traits>
828 return find_first_not_of(s, pos, traits_type::length(s));
840 template <
typename CharT,
typename Traits>
846 return find_first_not_of(&ch, pos, 1);
858 template <
typename CharT,
typename Traits>
861 size_type pos)
const noexcept
863 return find_last_of(str.data(), pos, str.size());
879 template <
typename CharT,
typename Traits>
882 size_type count)
const
884 if (size() == 0 || count == 0)
888 size_type last_of = 0;
889 for (
const CharT *c = s; c != s + count; ++c) {
890 size_type position = rfind(*c, pos);
891 if (position != npos) {
893 if (position > last_of)
914 template <
typename CharT,
typename Traits>
919 return find_last_of(s, pos, traits_type::length(s));
931 template <
typename CharT,
typename Traits>
936 return rfind(ch, pos);
948 template <
typename CharT,
typename Traits>
951 size_type pos)
const noexcept
953 return find_last_not_of(str.data(), pos, str.size());
969 template <
typename CharT,
typename Traits>
973 size_type count)
const
976 pos = (std::min)(pos, size() - 1);
978 if (!traits_type::find(s, count, *(data() + pos)))
998 template <
typename CharT,
typename Traits>
1001 size_type pos)
const
1003 return find_last_not_of(s, pos, traits_type::length(s));
1015 template <
typename CharT,
typename Traits>
1018 size_type pos)
const noexcept
1020 return find_last_not_of(&ch, pos, 1);
1034 template <
typename CharT,
typename Traits>
1039 ?
throw std::out_of_range(
"string_view::substr")
1041 (std::min)(count, size() - pos));
1056 template <
typename CharT,
typename Traits>
1059 size_type pos)
const
1062 throw std::out_of_range(
"string_view::copy");
1063 size_type rlen = (std::min)(count, size() - pos);
1064 Traits::copy(dest, data() + pos, rlen);
1079 template <
typename CharT,
typename Traits>
1084 return substr(pos1, n1).
compare(sv);
1100 template <
typename CharT,
typename Traits>
1118 template <
typename CharT,
typename Traits>
1136 template <
typename CharT,
typename Traits>
1139 const CharT *s)
const
1156 template <
typename CharT,
typename Traits>
1159 const CharT *s, size_type n2)
const
1172 template <
typename CharT,
typename Traits>
1177 int ret = Traits::compare(data(), other.data(),
1178 (std::min)(size(), other.size()));
1181 if (size() < other.size())
1183 if (size() > other.size())
1192 template <
class CharT,
class Traits>
1204 template <
class CharT,
class Traits>
1210 return (lhs.
size() != rhs.size() ?
false : lhs.
compare(rhs) == 0);
1217 template <
class CharT,
class Traits>
1223 return (lhs.size() != rhs.
size() ?
false : lhs.compare(rhs) == 0);
1230 template <
class CharT,
class Traits>
1242 template <
class CharT,
class Traits>
1248 return (lhs.size() != rhs.
size() ?
true : lhs.compare(rhs) != 0);
1255 template <
class CharT,
class Traits>
1261 return (lhs.
size() != rhs.size() ?
true : lhs.
compare(rhs) != 0);
1268 template <
class CharT,
class Traits>
1280 template <
class CharT,
class Traits>
1285 return lhs.compare(rhs) < 0;
1292 template <
class CharT,
class Traits>
1304 template <
class CharT,
class Traits>
1316 template <
class CharT,
class Traits>
1329 template <
class CharT,
class Traits>
1335 return lhs.compare(rhs) <= 0;
1342 template <
class CharT,
class Traits>
1354 template <
class CharT,
class Traits>
1359 return lhs.compare(rhs) > 0;
1366 template <
class CharT,
class Traits>
1378 template <
class CharT,
class Traits>
1390 template <
class CharT,
class Traits>
1396 return lhs.compare(rhs) >= 0;
1403 template <
class CharT,
class Traits>
Our partial std::string_view implementation.
Definition: string_view.hpp:48
constexpr const_reverse_iterator crbegin() const noexcept
Returns a reverse_iterator to the character following the last character of the view (reverse beginni...
Definition: string_view.hpp:313
constexpr const_iterator begin() const noexcept
Returns an iterator to the first character of the view.
Definition: string_view.hpp:245
constexpr bool operator>(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs)
Non-member greater than operator.
Definition: string_view.hpp:1344
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:384
void swap(basic_string_view &v) noexcept
Exchanges the view with that of v.
Definition: string_view.hpp:503
constexpr size_type size() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:397
size_type find(basic_string_view str, size_type pos=0) const noexcept
Finds the first substring equal to str.
Definition: string_view.hpp:521
void remove_prefix(size_type n)
Moves the start of the view forward by n characters.
Definition: string_view.hpp:477
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:775
constexpr bool operator>(typename std::common_type< basic_string_view< CharT, Traits >>::type lhs, basic_string_view< CharT, Traits > rhs)
Non-member greater than operator.
Definition: string_view.hpp:1356
int compare(size_type pos1, size_type n1, basic_string_view sv) const
Compares two character sequences.
Definition: string_view.hpp:1081
constexpr const_reverse_iterator crend() const noexcept
Returns a reverse_iterator to the first character of the view.
Definition: string_view.hpp:345
constexpr const_iterator cbegin() const noexcept
Returns an iterator to the first character of the view.
Definition: string_view.hpp:257
constexpr size_type length() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:409
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:1368
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:950
constexpr bool operator!=(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs)
Non-member not equal operator.
Definition: string_view.hpp:1232
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:1257
constexpr const_reverse_iterator rbegin() const noexcept
Returns a reverse_iterator to the character following the last character of the view (reverse beginni...
Definition: string_view.hpp:299
void remove_suffix(size_type n)
Moves the end of the view back by n characters.
Definition: string_view.hpp:491
constexpr bool empty() const noexcept
Returns that view is empty or not.
Definition: string_view.hpp:371
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:860
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:1405
const CharT & at(size_type pos) const
Returns reference to the character at position.
Definition: string_view.hpp:436
size_type rfind(basic_string_view str, size_type pos=npos) const noexcept
Finds the last substring equal to str.
Definition: string_view.hpp:609
constexpr const_reverse_iterator rend() const noexcept
Returns a reverse_iterator to the first character of the view.
Definition: string_view.hpp:329
constexpr const_iterator cend() const noexcept
Returns an iterator to the character following the last character of the view.
Definition: string_view.hpp:285
constexpr bool operator==(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs)
Non-member equal operator.
Definition: string_view.hpp:1194
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:1058
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:1036
constexpr bool operator>=(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs)
Non-member greater or equal operator.
Definition: string_view.hpp:1380
constexpr const_reference front() const noexcept
Returns reference to the first character in the view.
Definition: string_view.hpp:464
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:694
constexpr bool operator==(basic_string_view< CharT, Traits > lhs, typename std::common_type< basic_string_view< CharT, Traits >>::type rhs)
Non-member equal operator.
Definition: string_view.hpp:1206
constexpr const_iterator end() const noexcept
Returns an iterator to the character following the last character of the view.
Definition: string_view.hpp:271
constexpr const_reference back() const noexcept
Returns reference to the last character in the view.
Definition: string_view.hpp:451
constexpr const CharT * data() const noexcept
Returns pointer to data stored in this pmem::obj::string_view.
Definition: string_view.hpp:359
constexpr basic_string_view() noexcept
Default constructor with empty data.
Definition: string_view.hpp:193
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:1219
constexpr bool operator>=(typename std::common_type< basic_string_view< CharT, Traits >>::type lhs, basic_string_view< CharT, Traits > rhs)
Non-member greater or equal operator.
Definition: string_view.hpp:1392
constexpr bool operator!=(typename std::common_type< basic_string_view< CharT, Traits >>::type lhs, basic_string_view< CharT, Traits > rhs)
Non-member not equal operator.
Definition: string_view.hpp:1244
Persistent string container with std::basic_string compatible interface.
Definition: basic_string.hpp:48
basic_string_view< wchar_t > wstring_view
The wide char specialization.
Definition: string_view.hpp:175
basic_string_view< char16_t > u16string_view
The char16 specialization.
Definition: string_view.hpp:181
basic_string_view< char32_t > u32string_view
The char32 specialization.
Definition: string_view.hpp:187
basic_string_view< char > string_view
The most typical string_view usage - the char specialization.
Definition: string_view.hpp:169
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::array< T, N >::const_iterator cbegin(const pmem::obj::array< T, N > &a)
Non-member cbegin.
Definition: array.hpp:797
bool operator<=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less or equal operator.
Definition: array.hpp:786
bool operator<(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less than operator.
Definition: array.hpp:752
pmem::obj::array< T, N >::const_iterator cend(const pmem::obj::array< T, N > &a)
Non-member cend.
Definition: array.hpp:808