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>>
46 class basic_string_view {
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)();
73 operator=(
const basic_string_view &rhs) noexcept =
default;
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;
97 void swap(basic_string_view &v) noexcept;
99 constexpr basic_string_view
substr(size_type pos = 0,
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,
103 basic_string_view sv)
const;
104 inline int compare(size_type pos1, size_type n1, basic_string_view sv,
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,
110 int compare(
const basic_string_view &other)
const noexcept;
112 size_type find(basic_string_view str, size_type pos = 0)
const noexcept;
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;
117 size_type
rfind(basic_string_view str, size_type pos = npos)
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;
122 size_type
find_first_of(basic_string_view str, size_type pos = 0)
const 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>
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
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>
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 &
399 template <
typename CharT,
typename Traits>
400 constexpr
inline const CharT &
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
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
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
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
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
777 template <
typename CharT,
typename Traits>
778 typename basic_string_view<CharT, Traits>::size_type
795 template <
typename CharT,
typename Traits>
796 typename basic_string_view<CharT, Traits>::size_type
798 size_type pos)
const noexcept
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
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
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
952 template <
typename CharT,
typename Traits>
953 typename basic_string_view<CharT, Traits>::size_type
955 size_type pos)
const noexcept
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>
1019 basic_string_view sv)
const 1037 template <
typename CharT,
typename Traits>
1040 basic_string_view sv, size_type pos2,
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>
1200 operator<(basic_string_view<CharT, Traits> lhs,
1209 template <
class CharT,
class Traits>
1211 operator<(typename std::common_type<basic_string_view<CharT, Traits>>::type lhs,
1220 template <
class CharT,
class Traits>
1222 operator<(basic_string_view<CharT, Traits> lhs,
1223 typename std::common_type<basic_string_view<CharT, Traits>>::type rhs)
1225 return lhs.compare(rhs) < 0;
1231 template <
class CharT,
class Traits>
1233 operator<=(basic_string_view<CharT, Traits> lhs,
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>
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
void remove_prefix(size_type n)
Moves the start of the view forward by n characters.
Definition: string_view.hpp:414
bool operator<=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less or equal operator.
Definition: array.hpp:750
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
void swap(pmem::obj::array< T, N > &lhs, pmem::obj::array< T, N > &rhs)
Non-member swap function.
Definition: array.hpp:880
constexpr bool empty() const noexcept
Returns that view is empty or not.
Definition: string_view.hpp:308
constexpr size_type size() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:334
constexpr const CharT & operator[](size_type pos) const noexcept
Returns reference to a character at position.
Definition: string_view.hpp:358
int compare(size_type pos1, size_type n1, basic_string_view sv) const
Compares two character sequences.
Definition: string_view.hpp:1018
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 begin() const noexcept
Returns an iterator to the first character of the view.
Definition: string_view.hpp:214
constexpr const_reference front() const noexcept
Returns reference to the first character in the view.
Definition: string_view.hpp:401
bool operator>(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member greater than operator.
Definition: array.hpp:730
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
bool operator!=(const allocator< T, P, Tr > &lhs, const OtherAllocator &rhs)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:536
const CharT & at(size_type pos) const
Returns reference to the character at position.
Definition: string_view.hpp:373
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
bool operator>=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member greater or equal operator.
Definition: array.hpp:740
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
void swap(basic_string_view &v) noexcept
Exchanges the view with that of v.
Definition: string_view.hpp:440
constexpr size_type length() const noexcept
Returns count of characters stored in this pmem::obj::string_view data.
Definition: string_view.hpp:346
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
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, where rcount is the smaller of count and size() - pos.
Definition: string_view.hpp:995
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 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
Our partial std::string_view implementation.
Definition: string_view.hpp:46
Persistent memory namespace.
Definition: allocation_flag.hpp:14
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 const_iterator cbegin() const noexcept
Returns an iterator to the first character of the view.
Definition: string_view.hpp:226
bool operator==(standard_alloc_policy< T > const &, standard_alloc_policy< T2 > const &)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:420