rippled
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 <type_traits>
15#include <vector>
16
17namespace xrpl {
18
25class Slice
26{
27private:
28 std::uint8_t const* data_ = nullptr;
30
31public:
33 using const_iterator = value_type const*;
34
36 Slice() noexcept = default;
37
38 Slice(Slice const&) noexcept = default;
39 Slice&
40 operator=(Slice const&) noexcept = default;
41
43 Slice(void const* data, std::size_t size) noexcept : data_(reinterpret_cast<std::uint8_t const*>(data)), size_(size)
44 {
45 }
46
48 [[nodiscard]] bool
49 empty() const noexcept
50 {
51 return size_ == 0;
52 }
53
59 [[nodiscard]] std::size_t
60 size() const noexcept
61 {
62 return size_;
63 }
64
65 [[nodiscard]] std::size_t
66 length() const noexcept
67 {
68 return size_;
69 }
76 std::uint8_t const*
77 data() const noexcept
78 {
79 return data_;
80 }
81
84 operator[](std::size_t i) const noexcept
85 {
86 XRPL_ASSERT(i < size_, "xrpl::Slice::operator[](std::size_t) const : valid input");
87 return data_[i];
88 }
89
92 Slice&
94 {
95 if (n > size_)
96 Throw<std::domain_error>("too small");
97 data_ += n;
98 size_ -= n;
99 return *this;
100 }
101
102 Slice
104 {
105 Slice temp = *this;
106 return temp += n;
107 }
111 void
113 {
114 data_ += n;
115 size_ -= n;
116 }
117
119 void
121 {
122 size_ -= n;
123 }
124
126 begin() const noexcept
127 {
128 return data_;
129 }
130
132 cbegin() const noexcept
133 {
134 return data_;
135 }
136
138 end() const noexcept
139 {
140 return data_ + size_;
141 }
142
144 cend() const noexcept
145 {
146 return data_ + size_;
147 }
148
160 Slice
162 {
163 if (pos > size())
164 throw std::out_of_range("Requested sub-slice is out of bounds");
165
166 return {data_ + pos, std::min(count, size() - pos)};
167 }
168};
169
170//------------------------------------------------------------------------------
171
172template <class Hasher>
173inline void
174hash_append(Hasher& h, Slice const& v)
175{
176 h(v.data(), v.size());
177}
178
179inline bool
180operator==(Slice const& lhs, Slice const& rhs) noexcept
181{
182 if (lhs.size() != rhs.size())
183 return false;
184
185 if (lhs.size() == 0)
186 return true;
187
188 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
189}
190
191inline bool
192operator!=(Slice const& lhs, Slice const& rhs) noexcept
193{
194 return !(lhs == rhs);
195}
196
197inline bool
198operator<(Slice const& lhs, Slice const& rhs) noexcept
199{
200 return std::lexicographical_compare(lhs.data(), lhs.data() + lhs.size(), rhs.data(), rhs.data() + rhs.size());
201}
202
203template <class Stream>
204Stream&
205operator<<(Stream& s, Slice const& v)
206{
207 s << strHex(v);
208 return s;
209}
210
211template <class T, std::size_t N>
214{
215 return Slice(a.data(), a.size());
216}
217
218template <class T, class Alloc>
221{
222 return Slice(v.data(), v.size());
223}
224
225template <class Traits, class Alloc>
226Slice
228{
229 return Slice(s.data(), s.size());
230}
231
232} // namespace xrpl
An immutable linear range of bytes.
Definition Slice.h:26
const_iterator begin() const noexcept
Definition Slice.h:126
std::size_t length() const noexcept
Definition Slice.h:66
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:49
void remove_suffix(std::size_t n)
Shrinks the slice by moving its end backward by n characters.
Definition Slice.h:120
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:77
void remove_prefix(std::size_t n)
Shrinks the slice by moving its start forward by n characters.
Definition Slice.h:112
std::uint8_t const * data_
Definition Slice.h:28
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:60
Slice & operator+=(std::size_t n)
Advance the buffer.
Definition Slice.h:93
value_type const * const_iterator
Definition Slice.h:33
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:161
std::uint8_t operator[](std::size_t i) const noexcept
Access raw bytes.
Definition Slice.h:84
Slice operator+(std::size_t n) const
Definition Slice.h:103
std::size_t size_
Definition Slice.h:29
const_iterator cend() const noexcept
Definition Slice.h:144
const_iterator cbegin() const noexcept
Definition Slice.h:132
const_iterator end() const noexcept
Definition Slice.h:138
T data(T... args)
T lexicographical_compare(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:198
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:612
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:552
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:209
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:174
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:213
T size(T... args)