Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit d7471a9

Browse files
committed
string_view: implement iterator API for string_view
- add forward iterator API for string_view
1 parent 63110eb commit d7471a9

1 file changed

Lines changed: 66 additions & 1 deletion

File tree

include/libpmemobj++/string_view.hpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class basic_string_view {
5353
using const_reference = const value_type &;
5454
using pointer = value_type *;
5555
using const_pointer = const value_type *;
56+
using const_iterator = const_pointer;
57+
using iterator = const_iterator;
5658

5759
constexpr basic_string_view() noexcept;
5860
constexpr basic_string_view(const CharT *data, size_type size);
@@ -64,6 +66,17 @@ class basic_string_view {
6466
basic_string_view &
6567
operator=(const basic_string_view &rhs) noexcept = default;
6668

69+
constexpr const_iterator begin() const noexcept;
70+
constexpr const_iterator cbegin() const noexcept;
71+
constexpr const_iterator end() const noexcept;
72+
constexpr const_iterator cend() const noexcept;
73+
#ifdef XXX
74+
constexpr const_reverse_iterator rbegin() const noexcept;
75+
constexpr const_reverse_iterator crbegin() const noexcept;
76+
constexpr const_reverse_iterator rend() const noexcept;
77+
constexpr const_reverse_iterator crend() const noexcept;
78+
#endif
79+
6780
constexpr const CharT *data() const noexcept;
6881
constexpr size_type size() const noexcept;
6982
constexpr size_type length() const noexcept;
@@ -136,9 +149,61 @@ constexpr inline basic_string_view<CharT, Traits>::basic_string_view(
136149
{
137150
}
138151

152+
/**
153+
* Returns an iterator to the first character of the view.
154+
*
155+
* @return const_iterator to the first character
156+
*/
157+
template <typename CharT, typename Traits>
158+
constexpr typename basic_string_view<CharT, Traits>::const_iterator
159+
basic_string_view<CharT, Traits>::begin() const noexcept
160+
{
161+
return cbegin();
162+
}
163+
164+
/**
165+
* Returns an iterator to the first character of the view.
166+
*
167+
* @return const_iterator to the first character
168+
*/
169+
template <typename CharT, typename Traits>
170+
constexpr typename basic_string_view<CharT, Traits>::const_iterator
171+
basic_string_view<CharT, Traits>::cbegin() const noexcept
172+
{
173+
return data_;
174+
}
175+
176+
/**
177+
* Returns an iterator to the character following the last character of the
178+
* view. This character acts as a placeholder, attempting to access it results
179+
* in undefined behavior.
180+
*
181+
* @return const_iterator to the character following the last character.
182+
*/
183+
template <typename CharT, typename Traits>
184+
constexpr typename basic_string_view<CharT, Traits>::const_iterator
185+
basic_string_view<CharT, Traits>::end() const noexcept
186+
{
187+
return cend();
188+
}
189+
190+
/**
191+
* Returns an iterator to the character following the last character of the
192+
* view. This character acts as a placeholder, attempting to access it results
193+
* in undefined behavior.
194+
*
195+
* @return const_iterator to the character following the last character.
196+
*/
197+
template <typename CharT, typename Traits>
198+
constexpr typename basic_string_view<CharT, Traits>::const_iterator
199+
basic_string_view<CharT, Traits>::cend() const noexcept
200+
{
201+
return data_ + size_;
202+
}
203+
139204
/**
140205
* Returns pointer to data stored in this pmem::obj::string_view. It may
141-
*not contain the terminating null character.
206+
* not contain the terminating null character.
142207
*
143208
* @return pointer to C-like string (char *), it may not end with null
144209
* character.

0 commit comments

Comments
 (0)