rippled
Loading...
Searching...
No Matches
Slice.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_BASICS_SLICE_H_INCLUDED
21#define RIPPLE_BASICS_SLICE_H_INCLUDED
22
23#include <xrpl/basics/contract.h>
24#include <xrpl/basics/strHex.h>
25#include <xrpl/beast/utility/instrumentation.h>
26#include <algorithm>
27#include <array>
28#include <cstdint>
29#include <cstring>
30#include <limits>
31#include <stdexcept>
32#include <string>
33#include <type_traits>
34#include <vector>
35
36namespace ripple {
37
44class Slice
45{
46private:
47 std::uint8_t const* data_ = nullptr;
49
50public:
52 using const_iterator = value_type const*;
53
55 Slice() noexcept = default;
56
57 Slice(Slice const&) noexcept = default;
58 Slice&
59 operator=(Slice const&) noexcept = default;
60
62 Slice(void const* data, std::size_t size) noexcept
63 : data_(reinterpret_cast<std::uint8_t const*>(data)), size_(size)
64 {
65 }
66
68 [[nodiscard]] bool
69 empty() const noexcept
70 {
71 return size_ == 0;
72 }
73
79 [[nodiscard]] std::size_t
80 size() const noexcept
81 {
82 return size_;
83 }
84
85 [[nodiscard]] std::size_t
86 length() const noexcept
87 {
88 return size_;
89 }
96 std::uint8_t const*
97 data() const noexcept
98 {
99 return data_;
100 }
101
104 operator[](std::size_t i) const noexcept
105 {
106 XRPL_ASSERT(
107 i < size_,
108 "ripple::Slice::operator[](std::size_t) const : valid input");
109 return data_[i];
110 }
111
114 Slice&
116 {
117 if (n > size_)
118 Throw<std::domain_error>("too small");
119 data_ += n;
120 size_ -= n;
121 return *this;
122 }
123
124 Slice
126 {
127 Slice temp = *this;
128 return temp += n;
129 }
133 void
135 {
136 data_ += n;
137 size_ -= n;
138 }
139
141 void
143 {
144 size_ -= n;
145 }
146
148 begin() const noexcept
149 {
150 return data_;
151 }
152
154 cbegin() const noexcept
155 {
156 return data_;
157 }
158
160 end() const noexcept
161 {
162 return data_ + size_;
163 }
164
166 cend() const noexcept
167 {
168 return data_ + size_;
169 }
170
182 Slice
184 std::size_t pos,
186 {
187 if (pos > size())
188 throw std::out_of_range("Requested sub-slice is out of bounds");
189
190 return {data_ + pos, std::min(count, size() - pos)};
191 }
192};
193
194//------------------------------------------------------------------------------
195
196template <class Hasher>
197inline void
198hash_append(Hasher& h, Slice const& v)
199{
200 h(v.data(), v.size());
201}
202
203inline bool
204operator==(Slice const& lhs, Slice const& rhs) noexcept
205{
206 if (lhs.size() != rhs.size())
207 return false;
208
209 if (lhs.size() == 0)
210 return true;
211
212 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
213}
214
215inline bool
216operator!=(Slice const& lhs, Slice const& rhs) noexcept
217{
218 return !(lhs == rhs);
219}
220
221inline bool
222operator<(Slice const& lhs, Slice const& rhs) noexcept
223{
225 lhs.data(),
226 lhs.data() + lhs.size(),
227 rhs.data(),
228 rhs.data() + rhs.size());
229}
230
231template <class Stream>
232Stream&
233operator<<(Stream& s, Slice const& v)
234{
235 s << strHex(v);
236 return s;
237}
238
239template <class T, std::size_t N>
242 Slice>
244{
245 return Slice(a.data(), a.size());
246}
247
248template <class T, class Alloc>
251 Slice>
253{
254 return Slice(v.data(), v.size());
255}
256
257template <class Traits, class Alloc>
258Slice
260{
261 return Slice(s.data(), s.size());
262}
263
264} // namespace ripple
265
266#endif
An immutable linear range of bytes.
Definition: Slice.h:45
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:183
std::uint8_t const * data_
Definition: Slice.h:47
bool empty() const noexcept
Return true if the byte range is empty.
Definition: Slice.h:69
const_iterator begin() const noexcept
Definition: Slice.h:148
const_iterator cend() const noexcept
Definition: Slice.h:166
const_iterator end() const noexcept
Definition: Slice.h:160
std::size_t size_
Definition: Slice.h:48
void remove_suffix(std::size_t n)
Shrinks the slice by moving its end backward by n characters.
Definition: Slice.h:142
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:134
Slice operator+(std::size_t n) const
Definition: Slice.h:125
const_iterator cbegin() const noexcept
Definition: Slice.h:154
value_type const * const_iterator
Definition: Slice.h:52
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:97
std::uint8_t operator[](std::size_t i) const noexcept
Access raw bytes.
Definition: Slice.h:104
Slice & operator+=(std::size_t n)
Advance the buffer.
Definition: Slice.h:115
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
std::size_t length() const noexcept
Definition: Slice.h:86
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:26
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:198
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition: Buffer.h:232
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition: base_uint.h:636
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
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:243
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition: Slice.h:222
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition: base_uint.h:584
STL namespace.
T size(T... args)