7 #ifndef KUDU_UTIL_SLICE_H_ 8 #define KUDU_UTIL_SLICE_H_ 21 #ifdef KUDU_HEADERS_USE_RICH_SLICE 22 #include "kudu/gutil/strings/fastmem.h" 23 #include "kudu/gutil/strings/stringpiece.h" 24 #include "kudu/util/faststring.h" 26 #ifdef KUDU_HEADERS_NO_STUBS 27 #include "kudu/gutil/port.h" 29 #include "kudu/util/kudu_export.h" 53 Slice() : data_(reinterpret_cast<const uint8_t *>(
"")),
62 Slice(
const uint8_t* d,
size_t n) : data_(d), size_(n) { }
70 Slice(
const char* d,
size_t n) :
71 data_(reinterpret_cast<const uint8_t *>(d)),
79 data_(reinterpret_cast<const uint8_t *>(s.data())),
87 data_(reinterpret_cast<const uint8_t *>(s)),
90 #ifdef KUDU_HEADERS_USE_RICH_SLICE 91 Slice(
const faststring &s)
106 Slice(
const StringPiece& s)
107 : data_(reinterpret_cast<const uint8_t*>(s.data())),
113 const uint8_t*
data()
const {
return data_; }
119 size_t size()
const {
return size_; }
122 bool empty()
const {
return size_ == 0; }
136 data_ =
reinterpret_cast<const uint8_t *
>(
"");
173 Status check_size(
size_t expected_size)
const;
176 std::string ToString()
const;
184 std::string ToDebugString(
size_t max_len = 0)
const;
194 int compare(
const Slice& b)
const;
201 return ((size_ >= x.size_) &&
202 (MemEqual(data_, x.data_, x.size_)));
227 memcpy(d, data_, size_);
233 friend bool operator==(
const Slice& x,
const Slice& y);
235 static bool MemEqual(
const void* a,
const void* b,
size_t n) {
236 #ifdef KUDU_HEADERS_USE_RICH_SLICE 237 return strings::memeq(a, b, n);
239 return memcmp(a, b, n) == 0;
243 static int MemCompare(
const void* a,
const void* b,
size_t n) {
244 #ifdef KUDU_HEADERS_USE_RICH_SLICE 245 return strings::fastmemcmp_inlined(a, b, n);
247 return memcmp(a, b, n);
251 const uint8_t* data_;
276 inline bool operator!=(
const Slice& x,
const Slice& y) {
287 inline std::ostream& operator<<(std::ostream& o,
const Slice& s) {
292 const int min_len = (size_ < b.size_) ? size_ : b.size_;
293 int r = MemCompare(data_, b.data_, min_len);
295 if (size_ < b.size_) r = -1;
296 else if (size_ > b.size_) r = +1;
304 #ifdef KUDU_HEADERS_NO_STUBS 305 ATTRIBUTE_NO_SANITIZE_THREAD
307 bool IsAllZeros(
const Slice& s);
324 template <
typename T>
327 typedef std::map<Slice, T, Slice::Comparator>
type;
332 #endif // KUDU_UTIL_SLICE_H_ A representation of an operation's outcome.
Definition: status.h:145
Definition: callbacks.h:28
void relocate(uint8_t *d)
Definition: slice.h:225
Slice(const char *s)
Definition: slice.h:86
bool starts_with(const Slice &x) const
Definition: slice.h:200
std::string ToDebugString(size_t max_len=0) const
std::map< Slice, T, Slice::Comparator > type
A handy typedef for the slice map with appropriate comparison operator.
Definition: slice.h:327
bool operator()(const Slice &a, const Slice &b) const
Definition: slice.h:214
void clear()
Change this slice to refer to an empty array.
Definition: slice.h:135
const uint8_t & operator[](size_t n) const
Definition: slice.h:129
Comparator struct, useful for ordered collections (like STL maps).
Definition: slice.h:206
void truncate(size_t n)
Definition: slice.h:164
STL map whose keys are Slices.
Definition: slice.h:325
A wrapper around externally allocated data.
Definition: slice.h:50
size_t size() const
Definition: slice.h:119
Slice(const uint8_t *d, size_t n)
Definition: slice.h:62
Slice(const char *d, size_t n)
Definition: slice.h:70
uint8_t * mutable_data()
Definition: slice.h:116
const uint8_t * data() const
Definition: slice.h:113
void remove_prefix(size_t n)
Definition: slice.h:149
Slice(const std::string &s)
Definition: slice.h:78
int compare(const Slice &b) const
Definition: slice.h:291
Slice()
Create an empty slice.
Definition: slice.h:53
bool empty() const
Definition: slice.h:122