OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // Copied from strings/stringpiece.h with modifications |
| 5 // |
| 6 // A string-like object that points to a sized piece of memory. |
| 7 // |
| 8 // Functions or methods may use const StringPiece& parameters to accept either |
| 9 // a "const char*" or a "string" value that will be implicitly converted to |
| 10 // a StringPiece. The implicit conversion means that it is often appropriate |
| 11 // to include this .h file in other files rather than forward-declaring |
| 12 // StringPiece as would be appropriate for most other Google classes. |
| 13 // |
| 14 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
| 15 // conversions from "const char*" to "string" and back again. |
| 16 // |
| 17 |
| 18 #ifndef BASE_STRING_PIECE_H_ |
| 19 #define BASE_STRING_PIECE_H_ |
| 20 #pragma once |
| 21 |
| 22 #include <string> |
| 23 |
| 24 #include "base/basictypes.h" |
| 25 |
| 26 namespace base { |
| 27 |
| 28 class StringPiece { |
| 29 public: |
| 30 // standard STL container boilerplate |
| 31 typedef size_t size_type; |
| 32 typedef char value_type; |
| 33 typedef const char* pointer; |
| 34 typedef const char& reference; |
| 35 typedef const char& const_reference; |
| 36 typedef ptrdiff_t difference_type; |
| 37 typedef const char* const_iterator; |
| 38 typedef const char* iterator; |
| 39 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 40 typedef std::reverse_iterator<iterator> reverse_iterator; |
| 41 |
| 42 static const size_type npos; |
| 43 |
| 44 public: |
| 45 // We provide non-explicit singleton constructors so users can pass |
| 46 // in a "const char*" or a "string" wherever a "StringPiece" is |
| 47 // expected. |
| 48 StringPiece() : ptr_(NULL), length_(0) { } |
| 49 StringPiece(const char* str) |
| 50 : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } |
| 51 StringPiece(const std::string& str) |
| 52 : ptr_(str.data()), length_(str.size()) { } |
| 53 StringPiece(const char* offset, size_type len) |
| 54 : ptr_(offset), length_(len) { } |
| 55 |
| 56 // data() may return a pointer to a buffer with embedded NULs, and the |
| 57 // returned buffer may or may not be null terminated. Therefore it is |
| 58 // typically a mistake to pass data() to a routine that expects a NUL |
| 59 // terminated string. |
| 60 const char* data() const { return ptr_; } |
| 61 size_type size() const { return length_; } |
| 62 size_type length() const { return length_; } |
| 63 bool empty() const { return length_ == 0; } |
| 64 |
| 65 void clear() { |
| 66 ptr_ = NULL; |
| 67 length_ = 0; |
| 68 } |
| 69 void set(const char* data, size_type len) { |
| 70 ptr_ = data; |
| 71 length_ = len; |
| 72 } |
| 73 void set(const char* str) { |
| 74 ptr_ = str; |
| 75 length_ = str ? strlen(str) : 0; |
| 76 } |
| 77 void set(const void* data, size_type len) { |
| 78 ptr_ = reinterpret_cast<const char*>(data); |
| 79 length_ = len; |
| 80 } |
| 81 |
| 82 char operator[](size_type i) const { return ptr_[i]; } |
| 83 |
| 84 void remove_prefix(size_type n) { |
| 85 ptr_ += n; |
| 86 length_ -= n; |
| 87 } |
| 88 |
| 89 void remove_suffix(size_type n) { |
| 90 length_ -= n; |
| 91 } |
| 92 |
| 93 int compare(const StringPiece& x) const { |
| 94 int r = wordmemcmp( |
| 95 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); |
| 96 if (r == 0) { |
| 97 if (length_ < x.length_) r = -1; |
| 98 else if (length_ > x.length_) r = +1; |
| 99 } |
| 100 return r; |
| 101 } |
| 102 |
| 103 std::string as_string() const { |
| 104 // std::string doesn't like to take a NULL pointer even with a 0 size. |
| 105 return std::string(!empty() ? 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 (wordmemcmp(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 (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
| 121 } |
| 122 |
| 123 iterator begin() const { return ptr_; } |
| 124 iterator end() const { return ptr_ + length_; } |
| 125 const_reverse_iterator rbegin() const { |
| 126 return const_reverse_iterator(ptr_ + length_); |
| 127 } |
| 128 const_reverse_iterator rend() const { |
| 129 return const_reverse_iterator(ptr_); |
| 130 } |
| 131 |
| 132 size_type max_size() const { return length_; } |
| 133 size_type capacity() const { return length_; } |
| 134 |
| 135 size_type copy(char* buf, size_type n, size_type pos = 0) const; |
| 136 |
| 137 size_type find(const StringPiece& s, size_type pos = 0) const; |
| 138 size_type find(char c, size_type pos = 0) const; |
| 139 size_type rfind(const StringPiece& s, size_type pos = npos) const; |
| 140 size_type rfind(char c, size_type pos = npos) const; |
| 141 |
| 142 size_type find_first_of(const StringPiece& s, size_type pos = 0) const; |
| 143 size_type find_first_of(char c, size_type pos = 0) const { |
| 144 return find(c, pos); |
| 145 } |
| 146 size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const; |
| 147 size_type find_first_not_of(char c, size_type pos = 0) const; |
| 148 size_type find_last_of(const StringPiece& s, size_type pos = npos) const; |
| 149 size_type find_last_of(char c, size_type pos = npos) const { |
| 150 return rfind(c, pos); |
| 151 } |
| 152 size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const; |
| 153 size_type find_last_not_of(char c, size_type pos = npos) const; |
| 154 |
| 155 StringPiece substr(size_type pos, size_type n = npos) const; |
| 156 |
| 157 static int wordmemcmp(const char* p, const char* p2, size_type N) { |
| 158 return memcmp(p, p2, N); |
| 159 } |
| 160 |
| 161 private: |
| 162 const char* ptr_; |
| 163 size_type length_; |
| 164 }; |
| 165 |
| 166 bool operator==(const StringPiece& x, const StringPiece& y); |
| 167 |
| 168 inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
| 169 return !(x == y); |
| 170 } |
| 171 |
| 172 inline bool operator<(const StringPiece& x, const StringPiece& y) { |
| 173 const int r = StringPiece::wordmemcmp( |
| 174 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); |
| 175 return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
| 176 } |
| 177 |
| 178 inline bool operator>(const StringPiece& x, const StringPiece& y) { |
| 179 return y < x; |
| 180 } |
| 181 |
| 182 inline bool operator<=(const StringPiece& x, const StringPiece& y) { |
| 183 return !(x > y); |
| 184 } |
| 185 |
| 186 inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
| 187 return !(x < y); |
| 188 } |
| 189 |
| 190 } // namespace base |
| 191 |
| 192 #endif // BASE_STRING_PIECE_H_ |
OLD | NEW |