7 #ifndef KUDU_UTIL_SLICE_H_ 8 #define KUDU_UTIL_SLICE_H_ 22 #ifdef KUDU_HEADERS_USE_RICH_SLICE 23 #include "kudu/gutil/strings/fastmem.h" 24 #include "kudu/gutil/strings/stringpiece.h" 25 #include "kudu/util/faststring.h" 27 #ifdef KUDU_HEADERS_NO_STUBS 28 #include "kudu/gutil/port.h" 30 #include "kudu/util/kudu_export.h" 54 Slice() : data_(reinterpret_cast<const uint8_t *>(
"")),
63 Slice(
const uint8_t* d,
size_t n) : data_(d), size_(n) { }
71 Slice(
const char* d,
size_t n) :
72 data_(reinterpret_cast<const uint8_t *>(d)),
80 data_(reinterpret_cast<const uint8_t *>(s.data())),
88 data_(reinterpret_cast<const uint8_t *>(s)),
91 #ifdef KUDU_HEADERS_USE_RICH_SLICE 92 Slice(
const faststring &s)
107 Slice(
const StringPiece& s)
108 : data_(reinterpret_cast<const uint8_t*>(s.data())),
114 const uint8_t*
data()
const {
return data_; }
120 size_t size()
const {
return size_; }
123 bool empty()
const {
return size_ == 0; }
137 data_ =
reinterpret_cast<const uint8_t *
>(
"");
174 Status check_size(
size_t expected_size)
const;
177 std::string ToString()
const;
185 std::string ToDebugString(
size_t max_len = 0)
const;
195 int compare(
const Slice& b)
const;
202 return ((size_ >= x.size_) &&
203 (MemEqual(data_, x.data_, x.size_)));
228 memcpy(d, data_, size_);
234 friend bool operator==(
const Slice& x,
const Slice& y);
236 static bool MemEqual(
const void* a,
const void* b,
size_t n) {
237 #ifdef KUDU_HEADERS_USE_RICH_SLICE 238 return strings::memeq(a, b, n);
240 return memcmp(a, b, n) == 0;
244 static int MemCompare(
const void* a,
const void* b,
size_t n) {
245 #ifdef KUDU_HEADERS_USE_RICH_SLICE 246 return strings::fastmemcmp_inlined(a, b, n);
248 return memcmp(a, b, n);
252 const uint8_t* data_;
277 inline bool operator!=(
const Slice& x,
const Slice& y) {
288 inline std::ostream& operator<<(std::ostream& o,
const Slice& s) {
293 const int min_len = (size_ < b.size_) ? size_ : b.size_;
294 int r = MemCompare(data_, b.data_, min_len);
296 if (size_ < b.size_) r = -1;
297 else if (size_ > b.size_) r = +1;
305 #ifdef KUDU_HEADERS_NO_STUBS 306 ATTRIBUTE_NO_SANITIZE_THREAD
308 bool IsAllZeros(
const Slice& s);
325 template <
typename T>
328 typedef std::map<Slice, T, Slice::Comparator>
type;
333 #endif // KUDU_UTIL_SLICE_H_ A representation of an operation's outcome.
Definition: status.h:165
Definition: callbacks.h:28
void relocate(uint8_t *d)
Definition: slice.h:226
Slice(const char *s)
Definition: slice.h:87
bool starts_with(const Slice &x) const
Definition: slice.h:201
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:328
bool operator()(const Slice &a, const Slice &b) const
Definition: slice.h:215
void clear()
Change this slice to refer to an empty array.
Definition: slice.h:136
const uint8_t & operator[](size_t n) const
Definition: slice.h:130
Comparator struct, useful for ordered collections (like STL maps).
Definition: slice.h:207
void truncate(size_t n)
Definition: slice.h:165
STL map whose keys are Slices.
Definition: slice.h:326
A wrapper around externally allocated data.
Definition: slice.h:51
size_t size() const
Definition: slice.h:120
Slice(const uint8_t *d, size_t n)
Definition: slice.h:63
Slice(const char *d, size_t n)
Definition: slice.h:71
uint8_t * mutable_data()
Definition: slice.h:117
const uint8_t * data() const
Definition: slice.h:114
void remove_prefix(size_t n)
Definition: slice.h:150
Slice(const std::string &s)
Definition: slice.h:79
int compare(const Slice &b) const
Definition: slice.h:292
Slice()
Create an empty slice.
Definition: slice.h:54
bool empty() const
Definition: slice.h:123