xrpld
Loading...
Searching...
No Matches
Slice.h
1#pragma once
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/basics/strHex.h>
5#include <xrpl/beast/utility/instrumentation.h>
6
7#include <algorithm>
8#include <array>
9#include <cstdint>
10#include <cstring>
11#include <limits>
12#include <stdexcept>
13#include <string>
14#include <string_view>
15#include <type_traits>
16#include <vector>
17
18namespace xrpl {
19
27class Slice
28{
29private:
30 std::uint8_t const* data_ = nullptr;
32
33public:
35 using const_iterator = value_type const*;
36
40 Slice() noexcept = default;
41
42 Slice(Slice const&) noexcept = default;
43 Slice&
44 operator=(Slice const&) noexcept = default;
45
49 Slice(void const* data, std::size_t size) noexcept
50 : data_(reinterpret_cast<std::uint8_t const*>(data)), size_(size)
51 {
52 }
53
57 [[nodiscard]] bool
58 empty() const noexcept
59 {
60 return size_ == 0;
61 }
62
69 [[nodiscard]] std::size_t
70 size() const noexcept
71 {
72 return size_;
73 }
74
75 [[nodiscard]] std::size_t
76 length() const noexcept
77 {
78 return size_;
79 }
80
81
87 [[nodiscard]] std::uint8_t const*
88 data() const noexcept
89 {
90 return data_;
91 }
92
97 operator[](std::size_t i) const noexcept
98 {
99 XRPL_ASSERT(i < size_, "xrpl::Slice::operator[](std::size_t) const : valid input");
100 return data_[i];
101 }
102
107 Slice&
109 {
110 if (n > size_)
111 Throw<std::domain_error>("too small");
112 data_ += n;
113 size_ -= n;
114 return *this;
115 }
116
117 Slice
119 {
120 Slice temp = *this;
121 return temp += n;
122 }
123
124
128 void
130 {
131 data_ += n;
132 size_ -= n;
133 }
134
138 void
140 {
141 size_ -= n;
142 }
143
144 [[nodiscard]] const_iterator
145 begin() const noexcept
146 {
147 return data_;
148 }
149
150 [[nodiscard]] const_iterator
151 cbegin() const noexcept
152 {
153 return data_;
154 }
155
156 [[nodiscard]] const_iterator
157 end() const noexcept
158 {
159 return data_ + size_;
160 }
161
162 [[nodiscard]] const_iterator
163 cend() const noexcept
164 {
165 return data_ + size_;
166 }
167
180 [[nodiscard]] Slice
182 {
183 if (pos > size())
184 throw std::out_of_range("Requested sub-slice is out of bounds");
185
186 return {data_ + pos, std::min(count, size() - pos)};
187 }
188};
189
190//------------------------------------------------------------------------------
191
192template <class Hasher>
193inline void
194hash_append(Hasher& h, Slice const& v)
195{
196 h(v.data(), v.size());
197}
198
199inline bool
200operator==(Slice const& lhs, Slice const& rhs) noexcept
201{
202 if (lhs.size() != rhs.size())
203 return false;
204
205 if (lhs.empty())
206 return true;
207
208 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
209}
210
211inline bool
212operator<(Slice const& lhs, Slice const& rhs) noexcept
213{
215 lhs.data(), lhs.data() + lhs.size(), rhs.data(), rhs.data() + rhs.size());
216}
217
218template <class Stream>
219Stream&
220operator<<(Stream& s, Slice const& v)
221{
222 s << strHex(v);
223 return s;
224}
225
226template <class T, std::size_t N>
227Slice
230{
231 return Slice(a.data(), a.size());
232}
233
234template <class T, class Alloc>
235Slice
238{
239 return Slice(v.data(), v.size());
240}
241
242template <class Traits, class Alloc>
243Slice
245{
246 return Slice(s.data(), s.size());
247}
248
249template <class Traits>
250Slice
255
256} // namespace xrpl
An immutable linear range of bytes.
Definition Slice.h:28
const_iterator begin() const noexcept
Definition Slice.h:145
std::size_t length() const noexcept
Definition Slice.h:76
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:58
std::uint8_t value_type
Definition Slice.h:34
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:88
std::uint8_t const * data_
Definition Slice.h:30
Slice() noexcept=default
Default constructed Slice has length 0.
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:70
Slice & operator+=(std::size_t n)
Advance the buffer.
Definition Slice.h:108
value_type const * const_iterator
Definition Slice.h:35
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:181
std::uint8_t operator[](std::size_t i) const noexcept
Access raw bytes.
Definition Slice.h:97
void removePrefix(std::size_t n)
Shrinks the slice by moving its start forward by n characters.
Definition Slice.h:129
void removeSuffix(std::size_t n)
Shrinks the slice by moving its end backward by n characters.
Definition Slice.h:139
Slice operator+(std::size_t n) const
Definition Slice.h:118
std::size_t size_
Definition Slice.h:31
const_iterator cend() const noexcept
Definition Slice.h:163
const_iterator cbegin() const noexcept
Definition Slice.h:151
const_iterator end() const noexcept
Definition Slice.h:157
T data(T... args)
T is_same_v
T lexicographical_compare(T... args)
T max(T... args)
T memcmp(T... args)
T min(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition Slice.h:212
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:606
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:666
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
Dir::ConstIterator const_iterator
Definition Dir.cpp:16
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:194
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T size(T... args)