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
44 : data_(reinterpret_cast<std::uint8_t const*>(data)), size_(size)
45 {
46 }
47
49 [[nodiscard]] bool
50 empty() const noexcept
51 {
52 return size_ == 0;
53 }
54
60 [[nodiscard]] std::size_t
61 size() const noexcept
62 {
63 return size_;
64 }
65
66 [[nodiscard]] std::size_t
67 length() const noexcept
68 {
69 return size_;
70 }
77 std::uint8_t const*
78 data() const noexcept
79 {
80 return data_;
81 }
82
85 operator[](std::size_t i) const noexcept
86 {
87 XRPL_ASSERT(i < size_, "xrpl::Slice::operator[](std::size_t) const : valid input");
88 return data_[i];
89 }
90
93 Slice&
95 {
96 if (n > size_)
97 Throw<std::domain_error>("too small");
98 data_ += n;
99 size_ -= n;
100 return *this;
101 }
102
103 Slice
105 {
106 Slice temp = *this;
107 return temp += n;
108 }
112 void
114 {
115 data_ += n;
116 size_ -= n;
117 }
118
120 void
122 {
123 size_ -= n;
124 }
125
127 begin() const noexcept
128 {
129 return data_;
130 }
131
133 cbegin() const noexcept
134 {
135 return data_;
136 }
137
139 end() const noexcept
140 {
141 return data_ + size_;
142 }
143
145 cend() const noexcept
146 {
147 return data_ + size_;
148 }
149
161 Slice
163 {
164 if (pos > size())
165 throw std::out_of_range("Requested sub-slice is out of bounds");
166
167 return {data_ + pos, std::min(count, size() - pos)};
168 }
169};
170
171//------------------------------------------------------------------------------
172
173template <class Hasher>
174inline void
175hash_append(Hasher& h, Slice const& v)
176{
177 h(v.data(), v.size());
178}
179
180inline bool
181operator==(Slice const& lhs, Slice const& rhs) noexcept
182{
183 if (lhs.size() != rhs.size())
184 return false;
185
186 if (lhs.size() == 0)
187 return true;
188
189 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
190}
191
192inline bool
193operator!=(Slice const& lhs, Slice const& rhs) noexcept
194{
195 return !(lhs == rhs);
196}
197
198inline bool
199operator<(Slice const& lhs, Slice const& rhs) noexcept
200{
202 lhs.data(), lhs.data() + lhs.size(), rhs.data(), rhs.data() + rhs.size());
203}
204
205template <class Stream>
206Stream&
207operator<<(Stream& s, Slice const& v)
208{
209 s << strHex(v);
210 return s;
211}
212
213template <class T, std::size_t N>
216{
217 return Slice(a.data(), a.size());
218}
219
220template <class T, class Alloc>
223{
224 return Slice(v.data(), v.size());
225}
226
227template <class Traits, class Alloc>
228Slice
230{
231 return Slice(s.data(), s.size());
232}
233
234} // namespace xrpl
An immutable linear range of bytes.
Definition Slice.h:26
const_iterator begin() const noexcept
Definition Slice.h:127
std::size_t length() const noexcept
Definition Slice.h:67
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:50
void remove_suffix(std::size_t n)
Shrinks the slice by moving its end backward by n characters.
Definition Slice.h:121
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:78
void remove_prefix(std::size_t n)
Shrinks the slice by moving its start forward by n characters.
Definition Slice.h:113
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:61
Slice & operator+=(std::size_t n)
Advance the buffer.
Definition Slice.h:94
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:162
std::uint8_t operator[](std::size_t i) const noexcept
Access raw bytes.
Definition Slice.h:85
Slice operator+(std::size_t n) const
Definition Slice.h:104
std::size_t size_
Definition Slice.h:29
const_iterator cend() const noexcept
Definition Slice.h:145
const_iterator cbegin() const noexcept
Definition Slice.h:133
const_iterator end() const noexcept
Definition Slice.h:139
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:199
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:617
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:557
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:209
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:175
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:215
T size(T... args)