Coverage Report

Created: 2026-07-25 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/leveldb/include/leveldb/slice.h
Line
Count
Source
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
//
5
// Slice is a simple structure containing a pointer into some external
6
// storage and a size.  The user of a Slice must ensure that the slice
7
// is not used after the corresponding external storage has been
8
// deallocated.
9
//
10
// Multiple threads can invoke const methods on a Slice without
11
// external synchronization, but if any of the threads may call a
12
// non-const method, all threads accessing the same Slice must use
13
// external synchronization.
14
15
#ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_
16
#define STORAGE_LEVELDB_INCLUDE_SLICE_H_
17
18
#include <cassert>
19
#include <cstddef>
20
#include <cstring>
21
#include <string>
22
23
#include "leveldb/export.h"
24
25
namespace leveldb {
26
27
class LEVELDB_EXPORT Slice {
28
 public:
29
  // Create an empty slice.
30
5.25k
  Slice() : data_(""), size_(0) {}
31
32
  // Create a slice that refers to d[0,n-1].
33
31.5k
  Slice(const char* d, size_t n) : data_(d), size_(n) {}
34
35
  // Create a slice that refers to the contents of "s"
36
10.6k
  Slice(const std::string& s) : data_(s.data()), size_(s.size()) {}
37
38
  // Create a slice that refers to s[0,strlen(s)-1]
39
10.0k
  Slice(const char* s) : data_(s), size_(strlen(s)) {}
40
41
  // Intentionally copyable.
42
  Slice(const Slice&) = default;
43
  Slice& operator=(const Slice&) = default;
44
45
  // Return a pointer to the beginning of the referenced data
46
42.0k
  const char* data() const { return data_; }
47
48
  // Return the length (in bytes) of the referenced data
49
4.43M
  size_t size() const { return size_; }
50
51
  // Return true iff the length of the referenced data is zero
52
1.80k
  bool empty() const { return size_ == 0; }
53
54
0
  const char* begin() const { return data(); }
55
0
  const char* end() const { return data() + size(); }
56
57
  // Return the ith byte in the referenced data.
58
  // REQUIRES: n < size()
59
4.37M
  char operator[](size_t n) const {
60
4.37M
    assert(n < size());
61
4.37M
    return data_[n];
62
4.37M
  }
63
64
  // Change this slice to refer to an empty array
65
1.54k
  void clear() {
66
1.54k
    data_ = "";
67
1.54k
    size_ = 0;
68
1.54k
  }
69
70
  // Drop the first "n" bytes from this slice.
71
5.26k
  void remove_prefix(size_t n) {
72
5.26k
    assert(n <= size());
73
5.26k
    data_ += n;
74
5.26k
    size_ -= n;
75
5.26k
  }
76
77
  // Return a string that contains the copy of the referenced data.
78
471
  std::string ToString() const { return std::string(data_, size_); }
79
80
  // Three-way comparison.  Returns value:
81
  //   <  0 iff "*this" <  "b",
82
  //   == 0 iff "*this" == "b",
83
  //   >  0 iff "*this" >  "b"
84
  int compare(const Slice& b) const;
85
86
  // Return true iff "x" is a prefix of "*this"
87
1.73k
  bool starts_with(const Slice& x) const {
88
1.73k
    return ((size_ >= x.size_) && (memcmp(data_, x.data_, x.size_) == 0));
89
1.73k
  }
90
91
 private:
92
  const char* data_;
93
  size_t size_;
94
};
95
96
7.97k
inline bool operator==(const Slice& x, const Slice& y) {
97
7.97k
  return ((x.size() == y.size()) &&
98
1.97k
          (memcmp(x.data(), y.data(), x.size()) == 0));
99
7.97k
}
100
101
58
inline bool operator!=(const Slice& x, const Slice& y) { return !(x == y); }
102
103
5.28k
inline int Slice::compare(const Slice& b) const {
104
5.28k
  const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
105
5.28k
  int r = memcmp(data_, b.data_, min_len);
106
5.28k
  if (r == 0) {
107
111
    if (size_ < b.size_)
108
10
      r = -1;
109
101
    else if (size_ > b.size_)
110
8
      r = +1;
111
111
  }
112
5.28k
  return r;
113
5.28k
}
114
115
}  // namespace leveldb
116
117
#endif  // STORAGE_LEVELDB_INCLUDE_SLICE_H_