search_slice.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  19. // This source code is licensed under both the GPLv2 (found in the
  20. // COPYING file in the root directory) and Apache 2.0 License
  21. // (found in the LICENSE.Apache file in the root directory).
  22. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  23. // Use of this source code is governed by a BSD-style license that can be
  24. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  25. //
  26. // Slice is a simple structure containing a pointer into some external
  27. // storage and a size. The user of a Slice must ensure that the slice
  28. // is not used after the corresponding external storage has been
  29. // deallocated.
  30. //
  31. // Multiple threads can invoke const methods on a Slice without
  32. // external synchronization, but if any of the threads may call a
  33. // non-const method, all threads accessing the same Slice must use
  34. // external synchronization.
  35. #pragma once
  36. #include <assert.h>
  37. #include <stddef.h>
  38. #include <string.h>
  39. #include <cstdio>
  40. #include <string>
  41. #ifdef __cpp_lib_string_view
  42. #include <string_view>
  43. #endif
  44. // code copy from rocksdb/leveldb.
  45. namespace wwsearch {
  46. class Slice {
  47. public:
  48. // Create an empty slice.
  49. Slice() : data_(""), size_(0) {}
  50. // Create a slice that refers to d[0,n-1].
  51. Slice(const char* d, size_t n) : data_(d), size_(n) {}
  52. // Create a slice that refers to the contents of "s"
  53. /* implicit */
  54. Slice(const std::string& s) : data_(s.data()), size_(s.size()) {}
  55. #ifdef __cpp_lib_string_view
  56. // Create a slice that refers to the same contents as "sv"
  57. /* implicit */
  58. Slice(std::string_view sv) : data_(sv.data()), size_(sv.size()) {}
  59. #endif
  60. // Create a slice that refers to s[0,strlen(s)-1]
  61. /* implicit */
  62. Slice(const char* s) : data_(s) { size_ = (s == nullptr) ? 0 : strlen(s); }
  63. // Create a single slice from SliceParts using buf as storage.
  64. // buf must exist as long as the returned Slice exists.
  65. Slice(const struct SliceParts& parts, std::string* buf);
  66. // Return a pointer to the beginning of the referenced data
  67. const char* data() const { return data_; }
  68. // Return the length (in bytes) of the referenced data
  69. size_t size() const { return size_; }
  70. // Return true iff the length of the referenced data is zero
  71. bool empty() const { return size_ == 0; }
  72. // Return the ith byte in the referenced data.
  73. // REQUIRES: n < size()
  74. char operator[](size_t n) const {
  75. assert(n < size());
  76. return data_[n];
  77. }
  78. // Change this slice to refer to an empty array
  79. void clear() {
  80. data_ = "";
  81. size_ = 0;
  82. }
  83. // Drop the first "n" bytes from this slice.
  84. void remove_prefix(size_t n) {
  85. assert(n <= size());
  86. data_ += n;
  87. size_ -= n;
  88. }
  89. void remove_suffix(size_t n) {
  90. assert(n <= size());
  91. size_ -= n;
  92. }
  93. // Return a string that contains the copy of the referenced data.
  94. // when hex is true, returns a string of twice the length hex encoded (0-9A-F)
  95. std::string ToString(bool hex = false) const;
  96. #ifdef __cpp_lib_string_view
  97. // Return a string_view that references the same data as this slice.
  98. std::string_view ToStringView() const {
  99. return std::string_view(data_, size_);
  100. }
  101. #endif
  102. // Decodes the current slice interpreted as an hexadecimal string into result,
  103. // if successful returns true, if this isn't a valid hex string
  104. // (e.g not coming from Slice::ToString(true)) DecodeHex returns false.
  105. // This slice is expected to have an even number of 0-9A-F characters
  106. // also accepts lowercase (a-f)
  107. bool DecodeHex(std::string* result) const;
  108. // Three-way comparison. Returns value:
  109. // < 0 iff "*this" < "b",
  110. // == 0 iff "*this" == "b",
  111. // > 0 iff "*this" > "b"
  112. int compare(const Slice& b) const;
  113. // Return true iff "x" is a prefix of "*this"
  114. bool starts_with(const Slice& x) const {
  115. return ((size_ >= x.size_) && (memcmp(data_, x.data_, x.size_) == 0));
  116. }
  117. bool ends_with(const Slice& x) const {
  118. return ((size_ >= x.size_) &&
  119. (memcmp(data_ + size_ - x.size_, x.data_, x.size_) == 0));
  120. }
  121. // Compare two slices and returns the first byte where they differ
  122. size_t difference_offset(const Slice& b) const;
  123. // private: make these public for rockssearchjni access
  124. const char* data_;
  125. size_t size_;
  126. // Intentionally copyable
  127. };
  128. } // namespace wwsearch