40 typedef size_t size_type;
46 StringPiece(
const char *d,
size_t n) : data_(d), size_(n) {}
49 StringPiece(
const std::string& s) : data_(s.data()), size_(s.size()) {}
52 StringPiece(
const char *s) : data_(s), size_(strlen(s)) {}
54 void set(
const void *data,
size_t len) {
55 data_ =
reinterpret_cast<const char *
>(data);
60 const char *data()
const {
return data_; }
63 size_t size()
const {
return size_; }
66 bool empty()
const {
return size_ == 0; }
68 typedef const char *const_iterator;
69 typedef const char *iterator;
70 iterator begin()
const {
return data_; }
71 iterator end()
const {
return data_ + size_; }
73 static const size_t npos;
77 char operator[](
size_t n)
const {
89 void remove_prefix(
size_t n) {
95 void remove_suffix(
size_t n) {
100 size_t find(
char c,
size_t pos = 0)
const;
101 size_t rfind(
char c,
size_t pos = npos)
const;
108 if (starts_with(x)) {
109 remove_prefix(x.size_);
115 StringPiece substr(
size_t pos,
size_t n = npos)
const;
122 std::string ToString()
const {
return std::string(data_, size_); }
132 return ((size_ >= x.size_) && (memcmp(data_, x.data_, x.size_) == 0));
136 return ((size_ >= x.size_) &&
137 (memcmp(data_ + (size_ - x.size_), x.data_, x.size_) == 0));
148 return ((x.size() == y.size()) &&
149 (memcmp(x.data(), y.data(), x.size()) == 0));
157 return x.compare(y) <= 0;
160 return x.compare(y) >= 0;
163 inline int StringPiece::compare(
StringPiece b)
const {
164 const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
165 int r = memcmp(data_, b.data_, min_len);
169 else if (size_ > b.size_)