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
27#include <algorithm>
28#include <array>
29#include <cstdint>
30#include <cstring>
31#include <limits>
32#include <stdexcept>
33#include <string>
34#include <type_traits>
35#include <vector>
36
37namespace ripple {
38
45class Slice
46{
47private:
48 std::uint8_t const* data_ = nullptr;
50
51public:
53 using const_iterator = value_type const*;
54
56 Slice() noexcept = default;
57
58 Slice(Slice const&) noexcept = default;
59 Slice&
60 operator=(Slice const&) noexcept = default;
61
63 Slice(void const* data, std::size_t size) noexcept
64 : data_(reinterpret_cast<std::uint8_t const*>(data)), size_(size)
65 {
66 }
67
69 [[nodiscard]] bool
70 empty() const noexcept
71 {
72 return size_ == 0;
73 }
74
80 [[nodiscard]] std::size_t
81 size() const noexcept
82 {
83 return size_;
84 }
85
86 [[nodiscard]] std::size_t
87 length() const noexcept
88 {
89 return size_;
90 }
97 std::uint8_t const*
98 data() const noexcept
99 {
100 return data_;
101 }
102
105 operator[](std::size_t i) const noexcept
106 {
107 XRPL_ASSERT(
108 i < size_,
109 "ripple::Slice::operator[](std::size_t) const : valid input");
110 return data_[i];
111 }
112
115 Slice&
117 {
118 if (n > size_)
119 Throw<std::domain_error>("too small");
120 data_ += n;
121 size_ -= n;
122 return *this;
123 }
124
125 Slice
127 {
128 Slice temp = *this;
129 return temp += n;
130 }
134 void
136 {
137 data_ += n;
138 size_ -= n;
139 }
140
142 void
144 {
145 size_ -= n;
146 }
147
149 begin() const noexcept
150 {
151 return data_;
152 }
153
155 cbegin() const noexcept
156 {
157 return data_;
158 }
159
161 end() const noexcept
162 {
163 return data_ + size_;
164 }
165
167 cend() const noexcept
168 {
169 return data_ + size_;
170 }
171
183 Slice
185 std::size_t pos,
187 {
188 if (pos > size())
189 throw std::out_of_range("Requested sub-slice is out of bounds");
190
191 return {data_ + pos, std::min(count, size() - pos)};
192 }
193};
194
195//------------------------------------------------------------------------------
196
197template <class Hasher>
198inline void
199hash_append(Hasher& h, Slice const& v)
200{
201 h(v.data(), v.size());
202}
203
204inline bool
205operator==(Slice const& lhs, Slice const& rhs) noexcept
206{
207 if (lhs.size() != rhs.size())
208 return false;
209
210 if (lhs.size() == 0)
211 return true;
212
213 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
214}
215
216inline bool
217operator!=(Slice const& lhs, Slice const& rhs) noexcept
218{
219 return !(lhs == rhs);
220}
221
222inline bool
223operator<(Slice const& lhs, Slice const& rhs) noexcept
224{
226 lhs.data(),
227 lhs.data() + lhs.size(),
228 rhs.data(),
229 rhs.data() + rhs.size());
230}
231
232template <class Stream>
233Stream&
234operator<<(Stream& s, Slice const& v)
235{
236 s << strHex(v);
237 return s;
238}
239
240template <class T, std::size_t N>
243 Slice>
245{
246 return Slice(a.data(), a.size());
247}
248
249template <class T, class Alloc>
252 Slice>
254{
255 return Slice(v.data(), v.size());
256}
257
258template <class Traits, class Alloc>
259Slice
261{
262 return Slice(s.data(), s.size());
263}
264
265} // namespace ripple
266
267#endif
An immutable linear range of bytes.
Definition Slice.h:46
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:184
std::uint8_t const * data_
Definition Slice.h:48
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:70
const_iterator begin() const noexcept
Definition Slice.h:149
const_iterator cend() const noexcept
Definition Slice.h:167
const_iterator end() const noexcept
Definition Slice.h:161
std::size_t size_
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:143
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:135
Slice operator+(std::size_t n) const
Definition Slice.h:126
const_iterator cbegin() const noexcept
Definition Slice.h:155
value_type const * const_iterator
Definition Slice.h:53
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:98
std::uint8_t operator[](std::size_t i) const noexcept
Access raw bytes.
Definition Slice.h:105
Slice & operator+=(std::size_t n)
Advance the buffer.
Definition Slice.h:116
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:81
std::size_t length() const noexcept
Definition Slice.h:87
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:25
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:199
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:647
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:244
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition Slice.h:223
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:585
STL namespace.
T size(T... args)