include/boost/capy/buffers/buffers_iterator.hpp
100.0% Lines (88 / 88)
96.3% Functions (79 / 82)
Functions (82)
Function
Calls
Lines
Blocks
boost::capy::buffers_iterator<boost::capy::const_buffer, char>::skip_forward()
:107
2x
85.7%
89.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>::skip_forward()
:107
1x
71.4%
67.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::skip_forward()
:107
17x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char>::skip_forward()
:107
5x
100.0%
89.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::skip_forward()
:107
169x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, unsigned char>::skip_forward()
:107
2x
85.7%
78.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::skip_forward()
:107
6x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>::skip_backward()
:121
0
0.0%
0.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::skip_backward()
:121
21x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::skip_backward()
:121
134x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, unsigned char>::skip_backward()
:121
0
0.0%
0.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::skip_backward()
:121
0
0.0%
0.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>::advance(long)
:133
1x
41.7%
54.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::advance(long)
:133
40x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::advance(long)
:133
275x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, unsigned char>::advance(long)
:133
1x
50.0%
54.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::advance(long)
:133
1x
62.5%
64.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::buffers_iterator()
:190
14x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::const_buffer, char>::begin(boost::capy::const_buffer const&)
:201
1x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>::begin(boost::capy::mutable_buffer const&)
:201
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::begin(std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> > const&)
:201
2x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char>::begin(std::array<boost::capy::const_buffer, 2ul> const&)
:201
4x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::begin(std::array<boost::capy::const_buffer, 5ul> const&)
:201
6x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, unsigned char>::begin(std::array<boost::capy::const_buffer, 5ul> const&)
:201
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::begin(std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> > const&)
:201
3x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::const_buffer, char>::end(boost::capy::const_buffer const&)
:218
1x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>::end(boost::capy::mutable_buffer const&)
:218
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::end(std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> > const&)
:218
12x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char>::end(std::array<boost::capy::const_buffer, 2ul> const&)
:218
3x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::end(std::array<boost::capy::const_buffer, 5ul> const&)
:218
6x
100.0%
100.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::end(std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> > const&)
:218
1x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::const_buffer, char>::operator*() const
:232
10x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>::operator*() const
:232
2x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::operator*() const
:232
40x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char>::operator*() const
:232
18x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::operator*() const
:232
46x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, unsigned char>::operator*() const
:232
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::operator*() const
:232
8x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>::operator[](long) const
:258
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::operator[](long) const
:258
20x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::operator[](long) const
:258
10x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, unsigned char>::operator[](long) const
:258
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::operator[](long) const
:258
1x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::const_buffer, char>::operator++()
:265
10x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::operator++()
:265
10x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char>::operator++()
:265
17x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::operator++()
:265
15x
100.0%
100.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::operator++()
:265
7x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::operator--()
:287
10x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::operator--()
:287
10x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>::operator+=(long)
:312
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::operator+=(long)
:312
20x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::operator+=(long)
:312
143x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, unsigned char>::operator+=(long)
:312
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>::operator+=(long)
:312
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>::operator-=(long)
:328
10x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>::operator-=(long)
:328
122x
100.0%
100.0%
boost::capy::operator+(boost::capy::buffers_iterator<boost::capy::mutable_buffer, char>, long)
:337
1x
100.0%
100.0%
boost::capy::operator+(boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>, long)
:337
20x
100.0%
100.0%
boost::capy::operator+(boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>, long)
:337
143x
100.0%
100.0%
boost::capy::operator+(boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, unsigned char>, long)
:337
1x
100.0%
100.0%
boost::capy::operator+(boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char>, long)
:337
1x
100.0%
100.0%
boost::capy::operator-(boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char>, long)
:355
10x
100.0%
100.0%
boost::capy::operator-(boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char>, long)
:355
1x
100.0%
100.0%
boost::capy::operator-(boost::capy::buffers_iterator<boost::capy::const_buffer, char> const&, boost::capy::buffers_iterator<boost::capy::const_buffer, char> const&)
:364
1x
100.0%
100.0%
boost::capy::operator-(boost::capy::buffers_iterator<boost::capy::mutable_buffer, char> const&, boost::capy::buffers_iterator<boost::capy::mutable_buffer, char> const&)
:364
1x
100.0%
100.0%
boost::capy::operator-(boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char> const&, boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char> const&)
:364
1x
100.0%
100.0%
boost::capy::operator-(boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char> const&, boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char> const&)
:364
3x
100.0%
100.0%
boost::capy::operator-(boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char> const&, boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char> const&)
:364
258x
100.0%
100.0%
boost::capy::operator==(boost::capy::buffers_iterator<boost::capy::const_buffer, char> const&, boost::capy::buffers_iterator<boost::capy::const_buffer, char> const&)
:375
11x
100.0%
100.0%
boost::capy::operator==(boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char> const&, boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char> const&)
:375
22x
100.0%
100.0%
boost::capy::operator==(boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char> const&, boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char> const&)
:375
6x
100.0%
100.0%
boost::capy::operator==(boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char> const&, boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char> const&)
:375
272x
100.0%
100.0%
boost::capy::operator==(boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char> const&, boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char> const&)
:375
1x
100.0%
100.0%
boost::capy::operator<=>(boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char> const&, boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char> const&)
:385
121x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::const_buffer, char> boost::capy::buffers_begin<boost::capy::const_buffer>(boost::capy::const_buffer const&)
:403
1x
100.0%
100.0%
boost::capy::buffers_iterator<boost::capy::mutable_buffer, char> boost::capy::buffers_begin<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&)
:403
1x
100.0%
100.0%
boost::capy::buffers_iterator<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, char> boost::capy::buffers_begin<std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> > >(std::__cxx11::list<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> > const&)
:403
2x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char> boost::capy::buffers_begin<std::array<boost::capy::const_buffer, 2ul> >(std::array<boost::capy::const_buffer, 2ul> const&)
:403
4x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 5ul>, char> boost::capy::buffers_begin<std::array<boost::capy::const_buffer, 5ul> >(std::array<boost::capy::const_buffer, 5ul> const&)
:403
6x
100.0%
100.0%
boost::capy::buffers_iterator<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, char> boost::capy::buffers_begin<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> > >(std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> > const&)
:403
3x
100.0%
100.0%
boost::capy::buffers_iterator<std::array<boost::capy::const_buffer, 2ul>, char> boost::capy::buffers_end<std::array<boost::capy::const_buffer, 2ul> >(std::array<boost::capy::const_buffer, 2ul> const&)
:423
1x
100.0%
100.0%
| Line | TLA | Hits | 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 | 202x | skip_forward() noexcept | |
| 108 | { | ||
| 109 | 363x | while(current_ != end_) | |
| 110 | { | ||
| 111 | 334x | current_buffer_ = buffer_type(*current_); | |
| 112 | 334x | if(current_buffer_.size() > 0) | |
| 113 | 173x | return; | |
| 114 | 161x | ++current_; | |
| 115 | } | ||
| 116 | 29x | current_buffer_ = {}; | |
| 117 | } | ||
| 118 | |||
| 119 | // Move to the previous non-empty buffer, positioned past its last byte | ||
| 120 | void | ||
| 121 | 155x | skip_backward() noexcept | |
| 122 | { | ||
| 123 | do | ||
| 124 | { | ||
| 125 | 277x | --current_; | |
| 126 | 277x | current_buffer_ = buffer_type(*current_); | |
| 127 | } | ||
| 128 | 277x | while(current_buffer_.size() == 0); | |
| 129 | 155x | current_buffer_position_ = current_buffer_.size(); | |
| 130 | 155x | } | |
| 131 | |||
| 132 | void | ||
| 133 | 318x | advance(std::ptrdiff_t n) noexcept | |
| 134 | { | ||
| 135 | 318x | if(n >= 0) | |
| 136 | { | ||
| 137 | 177x | auto left = static_cast<std::size_t>(n); | |
| 138 | 349x | while(left > 0) | |
| 139 | { | ||
| 140 | 269x | std::size_t const avail = | |
| 141 | 269x | current_buffer_.size() - current_buffer_position_; | |
| 142 | 269x | if(left < avail) | |
| 143 | { | ||
| 144 | 97x | current_buffer_position_ += left; | |
| 145 | 97x | position_ += left; | |
| 146 | 97x | return; | |
| 147 | } | ||
| 148 | 172x | left -= avail; | |
| 149 | 172x | position_ += avail; | |
| 150 | 172x | ++current_; | |
| 151 | 172x | current_buffer_position_ = 0; | |
| 152 | 172x | skip_forward(); | |
| 153 | } | ||
| 154 | } | ||
| 155 | else | ||
| 156 | { | ||
| 157 | 141x | auto left = static_cast<std::size_t>(-n); | |
| 158 | 296x | while(left > 0) | |
| 159 | { | ||
| 160 | 296x | if(left <= current_buffer_position_) | |
| 161 | { | ||
| 162 | 141x | current_buffer_position_ -= left; | |
| 163 | 141x | position_ -= left; | |
| 164 | 141x | return; | |
| 165 | } | ||
| 166 | 155x | left -= current_buffer_position_; | |
| 167 | 155x | position_ -= current_buffer_position_; | |
| 168 | 155x | 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 | 14x | 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 | 18x | begin(BufferSequence const& bs) noexcept | |
| 202 | { | ||
| 203 | 18x | buffers_iterator it; | |
| 204 | 18x | it.current_ = buffer_sequence_begin(bs); | |
| 205 | 18x | it.end_ = buffer_sequence_end(bs); | |
| 206 | 18x | it.skip_forward(); | |
| 207 | 18x | 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 | 24x | end(BufferSequence const& bs) noexcept | |
| 219 | { | ||
| 220 | 24x | buffers_iterator it; | |
| 221 | 24x | it.current_ = buffer_sequence_end(bs); | |
| 222 | 24x | it.end_ = it.current_; | |
| 223 | 24x | it.position_ = buffer_size(bs); | |
| 224 | 24x | 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 | 125x | operator*() const noexcept | |
| 233 | { | ||
| 234 | return static_cast<pointer>( | ||
| 235 | 125x | 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 | 33x | operator[](difference_type n) const noexcept | |
| 259 | { | ||
| 260 | 33x | return *(*this + n); | |
| 261 | } | ||
| 262 | |||
| 263 | /// Advance to the next byte. | ||
| 264 | buffers_iterator& | ||
| 265 | 59x | operator++() noexcept | |
| 266 | { | ||
| 267 | 59x | ++position_; | |
| 268 | 59x | if(++current_buffer_position_ < current_buffer_.size()) | |
| 269 | 47x | return *this; | |
| 270 | 12x | ++current_; | |
| 271 | 12x | current_buffer_position_ = 0; | |
| 272 | 12x | skip_forward(); | |
| 273 | 12x | 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 | 20x | operator--() noexcept | |
| 288 | { | ||
| 289 | 20x | advance(-1); | |
| 290 | 20x | 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 | 166x | operator+=(difference_type n) noexcept | |
| 313 | { | ||
| 314 | 166x | advance(n); | |
| 315 | 166x | 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 | 132x | operator-=(difference_type n) noexcept | |
| 329 | { | ||
| 330 | 132x | advance(-n); | |
| 331 | 132x | return *this; | |
| 332 | } | ||
| 333 | |||
| 334 | /// Return an iterator `n` bytes after `it`. | ||
| 335 | friend | ||
| 336 | buffers_iterator | ||
| 337 | 166x | operator+(buffers_iterator it, difference_type n) noexcept | |
| 338 | { | ||
| 339 | 166x | it += n; | |
| 340 | 166x | 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 | 11x | operator-(buffers_iterator it, difference_type n) noexcept | |
| 356 | { | ||
| 357 | 11x | it -= n; | |
| 358 | 11x | return it; | |
| 359 | } | ||
| 360 | |||
| 361 | /// Return the number of bytes from `b` to `a`. | ||
| 362 | friend | ||
| 363 | difference_type | ||
| 364 | 264x | operator-( | |
| 365 | buffers_iterator const& a, | ||
| 366 | buffers_iterator const& b) noexcept | ||
| 367 | { | ||
| 368 | 264x | return static_cast<difference_type>(a.position_) - | |
| 369 | 264x | static_cast<difference_type>(b.position_); | |
| 370 | } | ||
| 371 | |||
| 372 | /// Test whether two iterators refer to the same byte. | ||
| 373 | friend | ||
| 374 | bool | ||
| 375 | 312x | operator==( | |
| 376 | buffers_iterator const& a, | ||
| 377 | buffers_iterator const& b) noexcept | ||
| 378 | { | ||
| 379 | 312x | return a.position_ == b.position_; | |
| 380 | } | ||
| 381 | |||
| 382 | /// Order two iterators by byte position. | ||
| 383 | friend | ||
| 384 | std::strong_ordering | ||
| 385 | 121x | operator<=>( | |
| 386 | buffers_iterator const& a, | ||
| 387 | buffers_iterator const& b) noexcept | ||
| 388 | { | ||
| 389 | 121x | 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 | 17x | buffers_begin(BufferSequence const& bs) noexcept | |
| 404 | { | ||
| 405 | 17x | 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 | 24x | buffers_end(BufferSequence const& bs) noexcept | |
| 424 | { | ||
| 425 | 24x | 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 | ||
| 437 |