/ src / leveldb / include / leveldb / slice.h
slice.h
  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 <assert.h>
 19  #include <stddef.h>
 20  #include <string.h>
 21  
 22  #include <string>
 23  
 24  #include "leveldb/export.h"
 25  
 26  namespace leveldb {
 27  
 28  class LEVELDB_EXPORT Slice {
 29   public:
 30    // Create an empty slice.
 31    Slice() : data_(""), size_(0) {}
 32  
 33    // Create a slice that refers to d[0,n-1].
 34    Slice(const char* d, size_t n) : data_(d), size_(n) {}
 35  
 36    // Create a slice that refers to the contents of "s"
 37    Slice(const std::string& s) : data_(s.data()), size_(s.size()) {}
 38  
 39    // Create a slice that refers to s[0,strlen(s)-1]
 40    Slice(const char* s) : data_(s), size_(strlen(s)) {}
 41  
 42    // Intentionally copyable.
 43    Slice(const Slice&) = default;
 44    Slice& operator=(const Slice&) = default;
 45  
 46    // Return a pointer to the beginning of the referenced data
 47    const char* data() const { return data_; }
 48  
 49    // Return the length (in bytes) of the referenced data
 50    size_t size() const { return size_; }
 51  
 52    // Return true iff the length of the referenced data is zero
 53    bool empty() const { return size_ == 0; }
 54  
 55    const char* begin() const { return data(); }
 56    const char* end() const { return data() + size(); }
 57  
 58    // Return the ith byte in the referenced data.
 59    // REQUIRES: n < size()
 60    char operator[](size_t n) const {
 61      assert(n < size());
 62      return data_[n];
 63    }
 64  
 65    // Change this slice to refer to an empty array
 66    void clear() {
 67      data_ = "";
 68      size_ = 0;
 69    }
 70  
 71    // Drop the first "n" bytes from this slice.
 72    void remove_prefix(size_t n) {
 73      assert(n <= size());
 74      data_ += n;
 75      size_ -= n;
 76    }
 77  
 78    // Return a string that contains the copy of the referenced data.
 79    std::string ToString() const { return std::string(data_, size_); }
 80  
 81    // Three-way comparison.  Returns value:
 82    //   <  0 iff "*this" <  "b",
 83    //   == 0 iff "*this" == "b",
 84    //   >  0 iff "*this" >  "b"
 85    int compare(const Slice& b) const;
 86  
 87    // Return true iff "x" is a prefix of "*this"
 88    bool starts_with(const Slice& x) const {
 89      return ((size_ >= x.size_) && (memcmp(data_, x.data_, x.size_) == 0));
 90    }
 91  
 92   private:
 93    const char* data_;
 94    size_t size_;
 95  };
 96  
 97  inline bool operator==(const Slice& x, const Slice& y) {
 98    return ((x.size() == y.size()) &&
 99            (memcmp(x.data(), y.data(), x.size()) == 0));
100  }
101  
102  inline bool operator!=(const Slice& x, const Slice& y) { return !(x == y); }
103  
104  inline int Slice::compare(const Slice& b) const {
105    const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
106    int r = memcmp(data_, b.data_, min_len);
107    if (r == 0) {
108      if (size_ < b.size_)
109        r = -1;
110      else if (size_ > b.size_)
111        r = +1;
112    }
113    return r;
114  }
115  
116  }  // namespace leveldb
117  
118  #endif  // STORAGE_LEVELDB_INCLUDE_SLICE_H_