1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LIBTEXTCLASSIFIER_UTILS_STRINGS_STRINGPIECE_H_ 18 #define LIBTEXTCLASSIFIER_UTILS_STRINGS_STRINGPIECE_H_ 19 20 #include <stddef.h> 21 #include <string> 22 23 #include "utils/base/logging.h" 24 25 namespace libtextclassifier3 { 26 27 // Read-only "view" of a piece of data. Does not own the underlying data. 28 class StringPiece { 29 public: 30 StringPiece() : StringPiece(nullptr, 0) {} 31 32 StringPiece(const char *str) // NOLINT(runtime/explicit) 33 : start_(str), size_(str == nullptr ? 0 : strlen(str)) {} 34 35 StringPiece(const char *start, size_t size) : start_(start), size_(size) {} 36 37 // Intentionally no "explicit" keyword: in function calls, we want strings to 38 // be converted to StringPiece implicitly. 39 StringPiece(const std::string &s) // NOLINT(runtime/explicit) 40 : StringPiece(s.data(), s.size()) {} 41 42 StringPiece(const std::string &s, int offset, int len) 43 : StringPiece(s.data() + offset, len) {} 44 45 char operator[](size_t i) const { return start_[i]; } 46 47 // Returns start address of underlying data. 48 const char *data() const { return start_; } 49 50 // Returns number of bytes of underlying data. 51 size_t size() const { return size_; } 52 size_t length() const { return size_; } 53 54 bool empty() const { return size_ == 0; } 55 56 // Returns a std::string containing a copy of the underlying data. 57 std::string ToString() const { return std::string(data(), size()); } 58 59 // Returns whether string ends with a given suffix. 60 bool EndsWith(StringPiece suffix) const { 61 return suffix.empty() || (size_ >= suffix.size() && 62 memcmp(start_ + (size_ - suffix.size()), 63 suffix.data(), suffix.size()) == 0); 64 } 65 66 // Returns whether the string begins with a given prefix. 67 bool StartsWith(StringPiece prefix) const { 68 return prefix.empty() || 69 (size_ >= prefix.size() && 70 memcmp(start_, prefix.data(), prefix.size()) == 0); 71 } 72 73 bool Equals(StringPiece other) const { 74 return size() == other.size() && memcmp(start_, other.data(), size_) == 0; 75 } 76 77 // Removes the first `n` characters from the string piece. Note that the 78 // underlying string is not changed, only the view. 79 void RemovePrefix(int n) { 80 TC3_CHECK_LE(n, size_); 81 start_ += n; 82 size_ -= n; 83 } 84 85 private: 86 const char *start_; // Not owned. 87 size_t size_; 88 }; 89 90 inline bool EndsWith(StringPiece text, StringPiece suffix) { 91 return text.EndsWith(suffix); 92 } 93 94 inline bool StartsWith(StringPiece text, StringPiece prefix) { 95 return text.StartsWith(prefix); 96 } 97 98 inline bool ConsumePrefix(StringPiece *text, StringPiece prefix) { 99 if (!text->StartsWith(prefix)) { 100 return false; 101 } 102 text->RemovePrefix(prefix.size()); 103 return true; 104 } 105 106 } // namespace libtextclassifier3 107 108 #endif // LIBTEXTCLASSIFIER_UTILS_STRINGS_STRINGPIECE_H_ 109