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