Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

104 Zeilen
2.8 KiB

  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. #ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_
  10. #define STORAGE_LEVELDB_INCLUDE_SLICE_H_
  11. #include <assert.h>
  12. #include <stddef.h>
  13. #include <string.h>
  14. #include <string>
  15. namespace leveldb {
  16. class Slice {
  17. public:
  18. // Create an empty slice.
  19. Slice() : data_(""), size_(0) { }
  20. // Create a slice that refers to data[0,n-1].
  21. Slice(const char* data, size_t n) : data_(data), size_(n) { }
  22. // Create a slice that refers to the contents of "s"
  23. Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
  24. // Create a slice that refers to s[0,strlen(s)-1]
  25. Slice(const char* s) : data_(s), size_(strlen(s)) { }
  26. // Return a pointer to the beginning of the referenced data
  27. const char* data() const { return data_; }
  28. // Return the length (in bytes) of the referenced data
  29. size_t size() const { return size_; }
  30. // Return true iff the length of the referenced data is zero
  31. bool empty() const { return size_ == 0; }
  32. // Return the ith byte in the referenced data.
  33. // REQUIRES: n < size()
  34. char operator[](size_t n) const {
  35. assert(n < size());
  36. return data_[n];
  37. }
  38. // Change this slice to refer to an empty array
  39. void clear() { data_ = ""; size_ = 0; }
  40. // Drop the first "n" bytes from this slice.
  41. void remove_prefix(size_t n) {
  42. assert(n <= size());
  43. data_ += n;
  44. size_ -= n;
  45. }
  46. // Return a string that contains the copy of the referenced data.
  47. std::string ToString() const { return std::string(data_, size_); }
  48. // Three-way comparison. Returns value:
  49. // < 0 iff "*this" < "b",
  50. // == 0 iff "*this" == "b",
  51. // > 0 iff "*this" > "b"
  52. int compare(const Slice& b) const;
  53. // Return true iff "x" is a prefix of "*this"
  54. bool starts_with(const Slice& x) const {
  55. return ((size_ >= x.size_) &&
  56. (memcmp(data_, x.data_, x.size_) == 0));
  57. }
  58. private:
  59. const char* data_;
  60. size_t size_;
  61. // Intentionally copyable
  62. };
  63. inline bool operator==(const Slice& x, const Slice& y) {
  64. return ((x.size() == y.size()) &&
  65. (memcmp(x.data(), y.data(), x.size()) == 0));
  66. }
  67. inline bool operator!=(const Slice& x, const Slice& y) {
  68. return !(x == y);
  69. }
  70. inline int Slice::compare(const Slice& b) const {
  71. const int min_len = (size_ < b.size_) ? size_ : b.size_;
  72. int r = memcmp(data_, b.data_, min_len);
  73. if (r == 0) {
  74. if (size_ < b.size_) r = -1;
  75. else if (size_ > b.size_) r = +1;
  76. }
  77. return r;
  78. }
  79. }
  80. #endif // STORAGE_LEVELDB_INCLUDE_SLICE_H_