TLA Line data Source code
1 : //
2 : // Copyright (c) 2026 Steve Gerbino
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/cppalliance/capy
8 : //
9 :
10 : #ifndef BOOST_CAPY_BUFFERS_BUFFERS_ITERATOR_HPP
11 : #define BOOST_CAPY_BUFFERS_BUFFERS_ITERATOR_HPP
12 :
13 : #include <boost/capy/detail/config.hpp>
14 : #include <boost/capy/buffers.hpp>
15 :
16 : #include <compare>
17 : #include <cstddef>
18 : #include <iterator>
19 : #include <ranges>
20 : #include <type_traits>
21 : #include <utility>
22 :
23 : namespace boost {
24 : namespace capy {
25 :
26 : namespace detail {
27 :
28 : // Walking back from the end needs an end iterator, not a sentinel
29 : template<class T>
30 : concept common_buffer_sequence =
31 : ConstBufferSequence<T> &&
32 : (ConstBuffer<T> || std::ranges::common_range<T const>);
33 :
34 : } // detail
35 :
36 : /** Iterate over the bytes of a buffer sequence as one continuous run.
37 :
38 : A buffer sequence stores its bytes in several separate regions.
39 : This iterator presents them as one run of bytes. Standard
40 : algorithms such as `std::search` can then scan across the
41 : boundaries between buffers without first copying the data into
42 : contiguous storage. Empty buffers in the sequence are skipped.
43 :
44 : The iterator provides the random access operations and tag. It does
45 : not meet the constant time requirement of a random access iterator.
46 :
47 : The iterator dereferences to `ByteType&` for a
48 : @ref MutableBufferSequence and to `ByteType const&` otherwise.
49 :
50 : Moving by `n` bytes walks the underlying buffers. The operators that
51 : move by an integer are linear in the number of buffers crossed.
52 : These are `operator+=`, `operator-=`, `operator+`, `operator-` with
53 : an integer, and `operator[]`. Comparison and the difference of two
54 : iterators are constant time.
55 :
56 : @pre The buffer sequence and the memory it refers to outlive every
57 : iterator obtained from it.
58 : @pre The sequence's buffers are not changed, and its iterators are
59 : not invalidated, while an iterator obtained from it is in use.
60 : @pre Iterators compared or subtracted come from the same sequence.
61 :
62 : @par Example
63 : @code
64 : std::array<capy::const_buffer, 2> bufs{
65 : capy::make_buffer(std::string_view("GET / HTTP/1.1\r")),
66 : capy::make_buffer(std::string_view("\n\r\nbody")) };
67 :
68 : // Find the end of the header, which spans both buffers
69 : char const crlfcrlf[] = "\r\n\r\n";
70 : auto it = std::search(
71 : capy::buffers_begin(bufs), capy::buffers_end(bufs),
72 : crlfcrlf, crlfcrlf + 4);
73 : std::size_t header_size = it - capy::buffers_begin(bufs) + 4;
74 : @endcode
75 :
76 : @tparam BufferSequence The buffer sequence type. It must be a single
77 : buffer, or a range of buffers whose begin and end have the same type.
78 :
79 : @tparam ByteType The byte type presented by the iterator. Its size
80 : must be 1.
81 :
82 : @see @ref buffers_begin, @ref buffers_end,
83 : @ref buffer_sequence_begin
84 : */
85 : template<class BufferSequence, class ByteType = char>
86 : requires detail::common_buffer_sequence<BufferSequence>
87 : class buffers_iterator
88 : {
89 : static_assert(sizeof(ByteType) == 1, "ByteType must be a byte");
90 :
91 : using sequence_iterator = decltype(
92 : buffer_sequence_begin(std::declval<BufferSequence const&>()));
93 : using buffer_type = capy::buffer_type<BufferSequence>;
94 : using byte_type = std::conditional_t<
95 : MutableBufferSequence<BufferSequence>,
96 : ByteType,
97 : ByteType const>;
98 :
99 : buffer_type current_buffer_{};
100 : std::size_t current_buffer_position_ = 0;
101 : sequence_iterator current_{};
102 : sequence_iterator end_{};
103 : std::size_t position_ = 0;
104 :
105 : // Move to the next non-empty buffer, or to the end of the sequence
106 : void
107 HIT 202 : skip_forward() noexcept
108 : {
109 363 : while(current_ != end_)
110 : {
111 334 : current_buffer_ = buffer_type(*current_);
112 334 : if(current_buffer_.size() > 0)
113 173 : return;
114 161 : ++current_;
115 : }
116 29 : current_buffer_ = {};
117 : }
118 :
119 : // Move to the previous non-empty buffer, positioned past its last byte
120 : void
121 155 : skip_backward() noexcept
122 : {
123 : do
124 : {
125 277 : --current_;
126 277 : current_buffer_ = buffer_type(*current_);
127 : }
128 277 : while(current_buffer_.size() == 0);
129 155 : current_buffer_position_ = current_buffer_.size();
130 155 : }
131 :
132 : void
133 318 : advance(std::ptrdiff_t n) noexcept
134 : {
135 318 : if(n >= 0)
136 : {
137 177 : auto left = static_cast<std::size_t>(n);
138 349 : while(left > 0)
139 : {
140 269 : std::size_t const avail =
141 269 : current_buffer_.size() - current_buffer_position_;
142 269 : if(left < avail)
143 : {
144 97 : current_buffer_position_ += left;
145 97 : position_ += left;
146 97 : return;
147 : }
148 172 : left -= avail;
149 172 : position_ += avail;
150 172 : ++current_;
151 172 : current_buffer_position_ = 0;
152 172 : skip_forward();
153 : }
154 : }
155 : else
156 : {
157 141 : auto left = static_cast<std::size_t>(-n);
158 296 : while(left > 0)
159 : {
160 296 : if(left <= current_buffer_position_)
161 : {
162 141 : current_buffer_position_ -= left;
163 141 : position_ -= left;
164 141 : return;
165 : }
166 155 : left -= current_buffer_position_;
167 155 : position_ -= current_buffer_position_;
168 155 : skip_backward();
169 : }
170 : }
171 : }
172 :
173 : public:
174 : /// The signed type of the distance between two iterators.
175 : using difference_type = std::ptrdiff_t;
176 :
177 : /// The type of the bytes the iterator visits.
178 : using value_type = ByteType;
179 :
180 : /// The type returned by `operator->`.
181 : using pointer = byte_type*;
182 :
183 : /// The type returned by `operator*`.
184 : using reference = byte_type&;
185 :
186 : /// The iterator category.
187 : using iterator_category = std::random_access_iterator_tag;
188 :
189 : /// Construct a singular iterator.
190 14 : buffers_iterator() = default;
191 :
192 : /** Return an iterator to the first byte of a buffer sequence.
193 :
194 : @param bs The buffer sequence. It must outlive the iterator.
195 :
196 : @return An iterator to the first byte, or the end iterator if
197 : the sequence holds no bytes.
198 : */
199 : static
200 : buffers_iterator
201 18 : begin(BufferSequence const& bs) noexcept
202 : {
203 18 : buffers_iterator it;
204 18 : it.current_ = buffer_sequence_begin(bs);
205 18 : it.end_ = buffer_sequence_end(bs);
206 18 : it.skip_forward();
207 18 : return it;
208 : }
209 :
210 : /** Return an iterator past the last byte of a buffer sequence.
211 :
212 : @param bs The buffer sequence. It must outlive the iterator.
213 :
214 : @return The end iterator.
215 : */
216 : static
217 : buffers_iterator
218 24 : end(BufferSequence const& bs) noexcept
219 : {
220 24 : buffers_iterator it;
221 24 : it.current_ = buffer_sequence_end(bs);
222 24 : it.end_ = it.current_;
223 24 : it.position_ = buffer_size(bs);
224 24 : return it;
225 : }
226 :
227 : /** Return a reference to the current byte.
228 :
229 : @pre The iterator is not the end iterator.
230 : */
231 : reference
232 125 : operator*() const noexcept
233 : {
234 : return static_cast<pointer>(
235 125 : current_buffer_.data())[current_buffer_position_];
236 : }
237 :
238 : /** Return a pointer to the current byte.
239 :
240 : @pre The iterator is not the end iterator.
241 : */
242 : pointer
243 : operator->() const noexcept
244 : {
245 : return &**this;
246 : }
247 :
248 : /** Return a reference to the byte `n` positions away.
249 :
250 : @par Complexity
251 : Linear in the number of buffers crossed.
252 :
253 : @param n The offset from this iterator, in bytes.
254 :
255 : @return A reference to the byte at `*this + n`.
256 : */
257 : reference
258 33 : operator[](difference_type n) const noexcept
259 : {
260 33 : return *(*this + n);
261 : }
262 :
263 : /// Advance to the next byte.
264 : buffers_iterator&
265 59 : operator++() noexcept
266 : {
267 59 : ++position_;
268 59 : if(++current_buffer_position_ < current_buffer_.size())
269 47 : return *this;
270 12 : ++current_;
271 12 : current_buffer_position_ = 0;
272 12 : skip_forward();
273 12 : return *this;
274 : }
275 :
276 : /// Advance to the next byte, returning the previous position.
277 : buffers_iterator
278 : operator++(int) noexcept
279 : {
280 : auto tmp = *this;
281 : ++*this;
282 : return tmp;
283 : }
284 :
285 : /// Move back to the previous byte.
286 : buffers_iterator&
287 20 : operator--() noexcept
288 : {
289 20 : advance(-1);
290 20 : return *this;
291 : }
292 :
293 : /// Move back to the previous byte, returning the previous position.
294 : buffers_iterator
295 : operator--(int) noexcept
296 : {
297 : auto tmp = *this;
298 : --*this;
299 : return tmp;
300 : }
301 :
302 : /** Move by `n` bytes.
303 :
304 : @par Complexity
305 : Linear in the number of buffers crossed.
306 :
307 : @param n The number of bytes to move; negative moves backward.
308 :
309 : @return A reference to `*this`.
310 : */
311 : buffers_iterator&
312 166 : operator+=(difference_type n) noexcept
313 : {
314 166 : advance(n);
315 166 : return *this;
316 : }
317 :
318 : /** Move back by `n` bytes.
319 :
320 : @par Complexity
321 : Linear in the number of buffers crossed.
322 :
323 : @param n The number of bytes to move back.
324 :
325 : @return A reference to `*this`.
326 : */
327 : buffers_iterator&
328 132 : operator-=(difference_type n) noexcept
329 : {
330 132 : advance(-n);
331 132 : return *this;
332 : }
333 :
334 : /// Return an iterator `n` bytes after `it`.
335 : friend
336 : buffers_iterator
337 166 : operator+(buffers_iterator it, difference_type n) noexcept
338 : {
339 166 : it += n;
340 166 : return it;
341 : }
342 :
343 : /// Return an iterator `n` bytes after `it`.
344 : friend
345 : buffers_iterator
346 : operator+(difference_type n, buffers_iterator it) noexcept
347 : {
348 : it += n;
349 : return it;
350 : }
351 :
352 : /// Return an iterator `n` bytes before `it`.
353 : friend
354 : buffers_iterator
355 11 : operator-(buffers_iterator it, difference_type n) noexcept
356 : {
357 11 : it -= n;
358 11 : return it;
359 : }
360 :
361 : /// Return the number of bytes from `b` to `a`.
362 : friend
363 : difference_type
364 264 : operator-(
365 : buffers_iterator const& a,
366 : buffers_iterator const& b) noexcept
367 : {
368 264 : return static_cast<difference_type>(a.position_) -
369 264 : static_cast<difference_type>(b.position_);
370 : }
371 :
372 : /// Test whether two iterators refer to the same byte.
373 : friend
374 : bool
375 312 : operator==(
376 : buffers_iterator const& a,
377 : buffers_iterator const& b) noexcept
378 : {
379 312 : return a.position_ == b.position_;
380 : }
381 :
382 : /// Order two iterators by byte position.
383 : friend
384 : std::strong_ordering
385 121 : operator<=>(
386 : buffers_iterator const& a,
387 : buffers_iterator const& b) noexcept
388 : {
389 121 : return a.position_ <=> b.position_;
390 : }
391 : };
392 :
393 : /** Return an iterator to the first byte of a buffer sequence.
394 :
395 : @param bs The buffer sequence. It must outlive the iterator.
396 :
397 : @return An iterator to the first byte of `bs`.
398 :
399 : @see @ref buffers_iterator, @ref buffers_end
400 : */
401 : template<detail::common_buffer_sequence BufferSequence>
402 : buffers_iterator<BufferSequence>
403 17 : buffers_begin(BufferSequence const& bs) noexcept
404 : {
405 17 : return buffers_iterator<BufferSequence>::begin(bs);
406 : }
407 :
408 : /// Rejects a temporary sequence, since the iterator would dangle.
409 : template<detail::common_buffer_sequence BufferSequence>
410 : buffers_iterator<BufferSequence>
411 : buffers_begin(BufferSequence const&& bs) = delete;
412 :
413 : /** Return an iterator past the last byte of a buffer sequence.
414 :
415 : @param bs The buffer sequence. It must outlive the iterator.
416 :
417 : @return The end iterator for `bs`.
418 :
419 : @see @ref buffers_iterator, @ref buffers_begin
420 : */
421 : template<detail::common_buffer_sequence BufferSequence>
422 : buffers_iterator<BufferSequence>
423 24 : buffers_end(BufferSequence const& bs) noexcept
424 : {
425 24 : return buffers_iterator<BufferSequence>::end(bs);
426 : }
427 :
428 : /// Rejects a temporary sequence, since the iterator would dangle.
429 : template<detail::common_buffer_sequence BufferSequence>
430 : buffers_iterator<BufferSequence>
431 : buffers_end(BufferSequence const&& bs) = delete;
432 :
433 : } // capy
434 : } // boost
435 :
436 : #endif
|