| 128 | #include "gutil/hash/hash.h" |
| 129 | |
| 130 | class StringPiece { |
| 131 | private: |
| 132 | const char* ptr_; |
| 133 | int length_; |
| 134 | |
| 135 | public: |
| 136 | // We provide non-explicit singleton constructors so users can pass |
| 137 | // in a "const char*" or a "string" wherever a "StringPiece" is |
| 138 | // expected. |
| 139 | // |
| 140 | // Style guide exception granted: |
| 141 | // http://goto/style-guide-exception-20978288 |
| 142 | StringPiece() : ptr_(NULL), length_(0) {} |
| 143 | StringPiece(const char* str) // NOLINT(runtime/explicit) |
| 144 | : ptr_(str), length_(0) { |
| 145 | if (str != NULL) { |
| 146 | size_t length = strlen(str); |
| 147 | assert(length <= static_cast<size_t>(std::numeric_limits<int>::max())); |
| 148 | length_ = static_cast<int>(length); |
| 149 | } |
| 150 | } |
| 151 | StringPiece(const std::string& str) // NOLINT(runtime/explicit) |
| 152 | : ptr_(str.data()), length_(0) { |
| 153 | size_t length = str.size(); |
| 154 | assert(length <= static_cast<size_t>(std::numeric_limits<int>::max())); |
| 155 | length_ = static_cast<int>(length); |
| 156 | } |
| 157 | StringPiece(const char* offset, int len) : ptr_(offset), length_(len) { |
| 158 | assert(len >= 0); |
| 159 | } |
| 160 | |
| 161 | // Substring of another StringPiece. |
| 162 | // pos must be non-negative and <= x.length(). |
| 163 | StringPiece(StringPiece x, int pos); |
| 164 | // Substring of another StringPiece. |
| 165 | // pos must be non-negative and <= x.length(). |
| 166 | // len must be non-negative and will be pinned to at most x.length() - pos. |
| 167 | StringPiece(StringPiece x, int pos, int len); |
| 168 | |
| 169 | // data() may return a pointer to a buffer with embedded NULs, and the |
| 170 | // returned buffer may or may not be null terminated. Therefore it is |
| 171 | // typically a mistake to pass data() to a routine that expects a NUL |
| 172 | // terminated string. |
| 173 | const char* data() const { return ptr_; } |
| 174 | int size() const { return length_; } |
| 175 | int length() const { return length_; } |
| 176 | bool empty() const { return length_ == 0; } |
| 177 | |
| 178 | void clear() { |
| 179 | ptr_ = NULL; |
| 180 | length_ = 0; |
| 181 | } |
| 182 | |
| 183 | void set(const char* data, int len) { |
| 184 | assert(len >= 0); |
| 185 | ptr_ = data; |
| 186 | length_ = len; |
| 187 | } |
no outgoing calls
no test coverage detected