michael@0: // Copyright 2013 Google Inc. All Rights Reserved. michael@0: // michael@0: // Licensed under the Apache License, Version 2.0 (the "License"); michael@0: // you may not use this file except in compliance with the License. michael@0: // You may obtain a copy of the License at michael@0: // michael@0: // http://www.apache.org/licenses/LICENSE-2.0 michael@0: // michael@0: // Unless required by applicable law or agreed to in writing, software michael@0: // distributed under the License is distributed on an "AS IS" BASIS, michael@0: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: // See the License for the specific language governing permissions and michael@0: // limitations under the License. michael@0: michael@0: // michael@0: // A StringPiece points to part or all of a string, double-quoted string michael@0: // literal, or other string-like object. A StringPiece does *not* own the michael@0: // string to which it points. A StringPiece is not null-terminated. [subset] michael@0: // michael@0: michael@0: #ifndef STRINGS_STRINGPIECE_H_ michael@0: #define STRINGS_STRINGPIECE_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: michael@0: typedef int stringpiece_ssize_type; michael@0: michael@0: class StringPiece { michael@0: private: michael@0: const char* ptr_; michael@0: stringpiece_ssize_type length_; michael@0: michael@0: public: michael@0: // We provide non-explicit singleton constructors so users can pass michael@0: // in a "const char*" or a "string" wherever a "StringPiece" is michael@0: // expected. michael@0: StringPiece() : ptr_(NULL), length_(0) {} michael@0: michael@0: StringPiece(const char* str) // NOLINT(runtime/explicit) michael@0: : ptr_(str), length_(0) { michael@0: if (str != NULL) { michael@0: length_ = strlen(str); michael@0: } michael@0: } michael@0: michael@0: StringPiece(const std::string& str) // NOLINT(runtime/explicit) michael@0: : ptr_(str.data()), length_(0) { michael@0: length_ = str.size(); michael@0: } michael@0: michael@0: StringPiece(const char* offset, stringpiece_ssize_type len) michael@0: : ptr_(offset), length_(len) { michael@0: } michael@0: michael@0: void remove_prefix(stringpiece_ssize_type n) { michael@0: ptr_ += n; michael@0: length_ -= n; michael@0: } michael@0: michael@0: void remove_suffix(stringpiece_ssize_type n) { michael@0: length_ -= n; michael@0: } michael@0: michael@0: // data() may return a pointer to a buffer with embedded NULs, and the michael@0: // returned buffer may or may not be null terminated. Therefore it is michael@0: // typically a mistake to pass data() to a routine that expects a NUL michael@0: // terminated string. michael@0: const char* data() const { return ptr_; } michael@0: stringpiece_ssize_type size() const { return length_; } michael@0: stringpiece_ssize_type length() const { return length_; } michael@0: bool empty() const { return length_ == 0; } michael@0: }; michael@0: michael@0: class StringPiece; michael@0: michael@0: #endif // STRINGS_STRINGPIECE_H__