Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | */ |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 16 | |
| 17 | // A string-like object that points to a sized piece of memory. |
| 18 | // |
| 19 | // Functions or methods may use const StringPiece& parameters to accept either |
| 20 | // a "const char*" or a "string" value that will be implicitly converted to |
| 21 | // a StringPiece. The implicit conversion means that it is often appropriate |
| 22 | // to include this .h file in other files rather than forward-declaring |
| 23 | // StringPiece as would be appropriate for most other Google classes. |
| 24 | // |
| 25 | // Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
| 26 | // conversions from "const char*" to "string" and back again. |
| 27 | |
| 28 | #ifndef ART_SRC_STRINGPIECE_H_ |
| 29 | #define ART_SRC_STRINGPIECE_H_ |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <algorithm> |
| 33 | #include <iosfwd> |
| 34 | #include <string> |
| 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | class StringPiece { |
| 39 | private: |
| 40 | const char* ptr_; |
| 41 | int length_; |
| 42 | |
| 43 | public: |
| 44 | // We provide non-explicit singleton constructors so users can pass |
| 45 | // in a "const char*" or a "string" wherever a "StringPiece" is |
| 46 | // expected. |
| 47 | StringPiece() : ptr_(NULL), length_(0) { } |
| 48 | StringPiece(const char* str) // NOLINT |
| 49 | : ptr_(str), length_((str == NULL) ? 0 : static_cast<int>(strlen(str))) { } |
| 50 | StringPiece(const std::string& str) // NOLINT |
| 51 | : ptr_(str.data()), length_(static_cast<int>(str.size())) { } |
| 52 | StringPiece(const char* offset, int len) : ptr_(offset), length_(len) { } |
| 53 | |
| 54 | // data() may return a pointer to a buffer with embedded NULs, and the |
| 55 | // returned buffer may or may not be null terminated. Therefore it is |
| 56 | // typically a mistake to pass data() to a routine that expects a NUL |
| 57 | // terminated string. |
| 58 | const char* data() const { return ptr_; } |
| 59 | int size() const { return length_; } |
| 60 | int length() const { return length_; } |
| 61 | bool empty() const { return length_ == 0; } |
| 62 | |
| 63 | void clear() { |
| 64 | ptr_ = NULL; |
| 65 | length_ = 0; |
| 66 | } |
| 67 | void set(const char* data, int len) { |
| 68 | ptr_ = data; |
| 69 | length_ = len; |
| 70 | } |
| 71 | void set(const char* str) { |
| 72 | ptr_ = str; |
| 73 | if (str != NULL) |
| 74 | length_ = static_cast<int>(strlen(str)); |
| 75 | else |
| 76 | length_ = 0; |
| 77 | } |
| 78 | void set(const void* data, int len) { |
| 79 | ptr_ = reinterpret_cast<const char*>(data); |
| 80 | length_ = len; |
| 81 | } |
| 82 | |
| 83 | char operator[](int i) const { return ptr_[i]; } |
| 84 | |
| 85 | void remove_prefix(int n) { |
| 86 | ptr_ += n; |
| 87 | length_ -= n; |
| 88 | } |
| 89 | |
| 90 | void remove_suffix(int n) { |
| 91 | length_ -= n; |
| 92 | } |
| 93 | |
| 94 | int compare(const StringPiece& x) const; |
| 95 | |
| 96 | std::string as_string() const { |
| 97 | return std::string(data(), size()); |
| 98 | } |
| 99 | // We also define ToString() here, since many other string-like |
| 100 | // interfaces name the routine that converts to a C++ string |
| 101 | // "ToString", and it's confusing to have the method that does that |
| 102 | // for a StringPiece be called "as_string()". We also leave the |
| 103 | // "as_string()" method defined here for existing code. |
| 104 | std::string ToString() const { |
| 105 | return std::string(data(), size()); |
| 106 | } |
| 107 | |
| 108 | void CopyToString(std::string* target) const; |
| 109 | void AppendToString(std::string* target) const; |
| 110 | |
| 111 | // Does "this" start with "x" |
| 112 | bool starts_with(const StringPiece& x) const { |
| 113 | return ((length_ >= x.length_) && |
| 114 | (memcmp(ptr_, x.ptr_, x.length_) == 0)); |
| 115 | } |
| 116 | |
| 117 | // Does "this" end with "x" |
| 118 | bool ends_with(const StringPiece& x) const { |
| 119 | return ((length_ >= x.length_) && |
| 120 | (memcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
| 121 | } |
| 122 | |
| 123 | // standard STL container boilerplate |
| 124 | typedef char value_type; |
| 125 | typedef const char* pointer; |
| 126 | typedef const char& reference; |
| 127 | typedef const char& const_reference; |
| 128 | typedef size_t size_type; |
| 129 | typedef ptrdiff_t difference_type; |
| 130 | static const size_type npos; |
| 131 | typedef const char* const_iterator; |
| 132 | typedef const char* iterator; |
| 133 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 134 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 135 | iterator begin() const { return ptr_; } |
| 136 | iterator end() const { return ptr_ + length_; } |
| 137 | const_reverse_iterator rbegin() const { |
| 138 | return const_reverse_iterator(ptr_ + length_); |
| 139 | } |
| 140 | const_reverse_iterator rend() const { |
| 141 | return const_reverse_iterator(ptr_); |
| 142 | } |
| 143 | // STLS says return size_type, but Google says return int |
| 144 | int max_size() const { return length_; } |
| 145 | int capacity() const { return length_; } |
| 146 | |
| 147 | int copy(char* buf, size_type n, size_type pos = 0) const; |
| 148 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 149 | size_type find(const StringPiece& s, size_type pos = 0) const; |
| 150 | size_type find(char c, size_type pos = 0) const; |
| 151 | size_type rfind(const StringPiece& s, size_type pos = npos) const; |
| 152 | size_type rfind(char c, size_type pos = npos) const; |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 153 | |
| 154 | StringPiece substr(size_type pos, size_type n = npos) const; |
| 155 | }; |
| 156 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 157 | // This large function is defined inline so that in a fairly common case where |
| 158 | // one of the arguments is a literal, the compiler can elide a lot of the |
| 159 | // following comparisons. |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 160 | inline bool operator==(const StringPiece& x, const StringPiece& y) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 161 | int len = x.size(); |
| 162 | if (len != y.size()) { |
| 163 | return false; |
| 164 | } |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 165 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 166 | const char* p1 = x.data(); |
| 167 | const char* p2 = y.data(); |
| 168 | if (p1 == p2) { |
| 169 | return true; |
| 170 | } |
| 171 | if (len <= 0) { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | // Test last byte in case strings share large common prefix |
| 176 | if (p1[len-1] != p2[len-1]) return false; |
| 177 | if (len == 1) return true; |
| 178 | |
| 179 | // At this point we can, but don't have to, ignore the last byte. We use |
| 180 | // this observation to fold the odd-length case into the even-length case. |
| 181 | len &= ~1; |
| 182 | |
| 183 | return memcmp(p1, p2, len) == 0; |
| 184 | } |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 185 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 186 | inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 187 | return !(x == y); |
| 188 | } |
| 189 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 190 | bool operator<(const StringPiece& x, const StringPiece& y); |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 191 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 192 | inline bool operator>(const StringPiece& x, const StringPiece& y) { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 193 | return y < x; |
| 194 | } |
| 195 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 196 | inline bool operator<=(const StringPiece& x, const StringPiece& y) { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 197 | return !(x > y); |
| 198 | } |
| 199 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 200 | inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 201 | return !(x < y); |
| 202 | } |
| 203 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 204 | extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 205 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 206 | struct StringPieceHash { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 207 | size_t operator()(const StringPiece& string_piece) const { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 208 | size_t string_size = string_piece.size(); |
| 209 | const char* string_data = string_piece.data(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 210 | // This is the java.lang.String hashcode for convenience, not interoperability. |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 211 | size_t hash = 0; |
| 212 | while (string_size--) { |
| 213 | hash = hash * 31 + *string_data++; |
| 214 | } |
| 215 | return hash; |
| 216 | } |
| 217 | }; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 218 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 219 | } // namespace art |
| 220 | |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 221 | #endif // ART_SRC_STRINGPIECE_H_ |