rippled
Loading...
Searching...
No Matches
Slice.h
1#ifndef XRPL_BASICS_SLICE_H_INCLUDED
2#define XRPL_BASICS_SLICE_H_INCLUDED
3
4#include <xrpl/basics/contract.h>
5#include <xrpl/basics/strHex.h>
6#include <xrpl/beast/utility/instrumentation.h>
7
8#include <algorithm>
9#include <array>
10#include <cstdint>
11#include <cstring>
12#include <limits>
13#include <stdexcept>
14#include <string>
15#include <type_traits>
16#include <vector>
17
18namespace ripple {
19
26class Slice
27{
28private:
29 std::uint8_t const* data_ = nullptr;
31
32public:
34 using const_iterator = value_type const*;
35
37 Slice() noexcept = default;
38
39 Slice(Slice const&) noexcept = default;
40 Slice&
41 operator=(Slice const&) noexcept = default;
42
44 Slice(void const* data, std::size_t size) noexcept
45 : data_(reinterpret_cast<std::uint8_t const*>(data)), size_(size)
46 {
47 }
48
50 [[nodiscard]] bool
51 empty() const noexcept
52 {
53 return size_ == 0;
54 }
55
61 [[nodiscard]] std::size_t
62 size() const noexcept
63 {
64 return size_;
65 }
66
67 [[nodiscard]] std::size_t
68 length() const noexcept
69 {
70 return size_;
71 }
78 std::uint8_t const*
79 data() const noexcept
80 {
81 return data_;
82 }
83
86 operator[](std::size_t i) const noexcept
87 {
88 XRPL_ASSERT(
89 i < size_,
90 "ripple::Slice::operator[](std::size_t) const : valid input");
91 return data_[i];
92 }
93
96 Slice&
98 {
99 if (n > size_)
100 Throw<std::domain_error>("too small");
101 data_ += n;
102 size_ -= n;
103 return *this;
104 }
105
106 Slice
108 {
109 Slice temp = *this;
110 return temp += n;
111 }
115 void
117 {
118 data_ += n;
119 size_ -= n;
120 }
121
123 void
125 {
126 size_ -= n;
127 }
128
130 begin() const noexcept
131 {
132 return data_;
133 }
134
136 cbegin() const noexcept
137 {
138 return data_;
139 }
140
142 end() const noexcept
143 {
144 return data_ + size_;
145 }
146
148 cend() const noexcept
149 {
150 return data_ + size_;
151 }
152
164 Slice
166 std::size_t pos,
168 {
169 if (pos > size())
170 throw std::out_of_range("Requested sub-slice is out of bounds");
171
172 return {data_ + pos, std::min(count, size() - pos)};
173 }
174};
175
176//------------------------------------------------------------------------------
177
178template <class Hasher>
179inline void
180hash_append(Hasher& h, Slice const& v)
181{
182 h(v.data(), v.size());
183}
184
185inline bool
186operator==(Slice const& lhs, Slice const& rhs) noexcept
187{
188 if (lhs.size() != rhs.size())
189 return false;
190
191 if (lhs.size() == 0)
192 return true;
193
194 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
195}
196
197inline bool
198operator!=(Slice const& lhs, Slice const& rhs) noexcept
199{
200 return !(lhs == rhs);
201}
202
203inline bool
204operator<(Slice const& lhs, Slice const& rhs) noexcept
205{
207 lhs.data(),
208 lhs.data() + lhs.size(),
209 rhs.data(),
210 rhs.data() + rhs.size());
211}
212
213template <class Stream>
214Stream&
215operator<<(Stream& s, Slice const& v)
216{
217 s << strHex(v);
218 return s;
219}
220
221template <class T, std::size_t N>
224 Slice>
226{
227 return Slice(a.data(), a.size());
228}
229
230template <class T, class Alloc>
233 Slice>
235{
236 return Slice(v.data(), v.size());
237}
238
239template <class Traits, class Alloc>
240Slice
242{
243 return Slice(s.data(), s.size());
244}
245
246} // namespace ripple
247
248#endif
An immutable linear range of bytes.
Definition Slice.h:27
Slice substr(std::size_t pos, std::size_t count=std::numeric_limits< std::size_t >::max()) const
Return a "sub slice" of given length starting at the given position.
Definition Slice.h:165
std::uint8_t const * data_
Definition Slice.h:29
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:51
const_iterator begin() const noexcept
Definition Slice.h:130
const_iterator cend() const noexcept
Definition Slice.h:148
const_iterator end() const noexcept
Definition Slice.h:142
std::size_t size_
Definition Slice.h:30
void remove_suffix(std::size_t n)
Shrinks the slice by moving its end backward by n characters.
Definition Slice.h:124
Slice() noexcept=default
Default constructed Slice has length 0.
void remove_prefix(std::size_t n)
Shrinks the slice by moving its start forward by n characters.
Definition Slice.h:116
Slice operator+(std::size_t n) const
Definition Slice.h:107
const_iterator cbegin() const noexcept
Definition Slice.h:136
value_type const * const_iterator
Definition Slice.h:34
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:79
std::uint8_t operator[](std::size_t i) const noexcept
Access raw bytes.
Definition Slice.h:86
Slice & operator+=(std::size_t n)
Advance the buffer.
Definition Slice.h:97
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:62
std::size_t length() const noexcept
Definition Slice.h:68
T data(T... args)
T lexicographical_compare(T... args)
T memcmp(T... args)
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:180
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:213
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:628
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:225
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition Slice.h:204
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:566
STL namespace.
T size(T... args)