OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // Copied from strings/stringpiece.h with modifications | 4 // Copied from strings/stringpiece.h with modifications |
5 // | 5 // |
6 // A string-like object that points to a sized piece of memory. | 6 // A string-like object that points to a sized piece of memory. |
7 // | 7 // |
8 // Functions or methods may use const StringPiece& parameters to accept either | 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 | 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 | 10 // a StringPiece. The implicit conversion means that it is often appropriate |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 #include <string> | 27 #include <string> |
28 | 28 |
29 #include "base/base_export.h" | 29 #include "base/base_export.h" |
30 #include "base/basictypes.h" | 30 #include "base/basictypes.h" |
31 #include "base/hash_tables.h" | 31 #include "base/hash_tables.h" |
32 #include "base/string16.h" | 32 #include "base/string16.h" |
33 | 33 |
34 namespace base { | 34 namespace base { |
35 | 35 |
36 class BASE_EXPORT StringPiece { | 36 template <typename STRING_TYPE> class BasicStringPiece; |
| 37 |
| 38 namespace internal { |
| 39 |
| 40 // Defines the types, methods, operators, and data members common to both |
| 41 // StringPiece and StringPiece16. Do not refer to this class directly, but |
| 42 // rather to BasicStringPiece, StringPiece, or StringPiece16. |
| 43 template <typename STRING_TYPE> class StringPieceDetail { |
37 public: | 44 public: |
38 // standard STL container boilerplate | 45 // standard STL container boilerplate |
39 typedef size_t size_type; | 46 typedef size_t size_type; |
40 typedef char value_type; | 47 typedef typename STRING_TYPE::value_type value_type; |
41 typedef const char* pointer; | 48 typedef const value_type* pointer; |
42 typedef const char& reference; | 49 typedef const value_type& reference; |
43 typedef const char& const_reference; | 50 typedef const value_type& const_reference; |
44 typedef ptrdiff_t difference_type; | 51 typedef ptrdiff_t difference_type; |
45 typedef const char* const_iterator; | 52 typedef const value_type* const_iterator; |
46 typedef const char* iterator; | |
47 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | 53 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
48 typedef std::reverse_iterator<iterator> reverse_iterator; | |
49 | 54 |
50 static const size_type npos; | 55 static const size_type npos; |
51 | 56 |
52 public: | 57 public: |
53 // We provide non-explicit singleton constructors so users can pass | 58 // We provide non-explicit singleton constructors so users can pass |
54 // in a "const char*" or a "string" wherever a "StringPiece" is | 59 // in a "const char*" or a "string" wherever a "StringPiece" is |
55 // expected. | 60 // expected (likewise for char16, string16, StringPiece16). |
56 StringPiece() : ptr_(NULL), length_(0) { } | 61 StringPieceDetail() : ptr_(NULL), length_(0) {} |
57 StringPiece(const char* str) | 62 StringPieceDetail(const value_type* str) |
58 : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } | 63 : ptr_(str), |
59 StringPiece(const std::string& str) | 64 length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {} |
60 : ptr_(str.data()), length_(str.size()) { } | 65 StringPieceDetail(const STRING_TYPE& str) |
61 StringPiece(const char* offset, size_type len) | 66 : ptr_(str.data()), length_(str.size()) {} |
62 : ptr_(offset), length_(len) { } | 67 StringPieceDetail(const value_type* offset, size_type len) |
63 StringPiece(const std::string::const_iterator& begin, | 68 : ptr_(offset), length_(len) {} |
64 const std::string::const_iterator& end) | 69 StringPieceDetail(const typename STRING_TYPE::const_iterator& begin, |
65 : ptr_((end > begin) ? &(*begin) : NULL), | 70 const typename STRING_TYPE::const_iterator& end) |
66 length_((end > begin) ? (size_type)(end - begin) : 0) { } | 71 : ptr_((end > begin) ? &(*begin) : NULL), |
| 72 length_((end > begin) ? (size_type)(end - begin) : 0) {} |
67 | 73 |
68 // data() may return a pointer to a buffer with embedded NULs, and the | 74 // data() may return a pointer to a buffer with embedded NULs, and the |
69 // returned buffer may or may not be null terminated. Therefore it is | 75 // returned buffer may or may not be null terminated. Therefore it is |
70 // typically a mistake to pass data() to a routine that expects a NUL | 76 // typically a mistake to pass data() to a routine that expects a NUL |
71 // terminated string. | 77 // terminated string. |
72 const char* data() const { return ptr_; } | 78 const value_type* data() const { return ptr_; } |
73 size_type size() const { return length_; } | 79 size_type size() const { return length_; } |
74 size_type length() const { return length_; } | 80 size_type length() const { return length_; } |
75 bool empty() const { return length_ == 0; } | 81 bool empty() const { return length_ == 0; } |
76 | 82 |
77 void clear() { | 83 void clear() { |
78 ptr_ = NULL; | 84 ptr_ = NULL; |
79 length_ = 0; | 85 length_ = 0; |
80 } | 86 } |
81 void set(const char* data, size_type len) { | 87 void set(const value_type* data, size_type len) { |
82 ptr_ = data; | 88 ptr_ = data; |
83 length_ = len; | 89 length_ = len; |
84 } | 90 } |
85 void set(const char* str) { | 91 void set(const value_type* str) { |
86 ptr_ = str; | 92 ptr_ = str; |
87 length_ = str ? strlen(str) : 0; | 93 length_ = str ? STRING_TYPE::traits_type::length(str) : 0; |
88 } | |
89 void set(const void* data, size_type len) { | |
90 ptr_ = reinterpret_cast<const char*>(data); | |
91 length_ = len; | |
92 } | 94 } |
93 | 95 |
94 char operator[](size_type i) const { return ptr_[i]; } | 96 value_type operator[](size_type i) const { return ptr_[i]; } |
95 | 97 |
96 void remove_prefix(size_type n) { | 98 void remove_prefix(size_type n) { |
97 ptr_ += n; | 99 ptr_ += n; |
98 length_ -= n; | 100 length_ -= n; |
99 } | 101 } |
100 | 102 |
101 void remove_suffix(size_type n) { | 103 void remove_suffix(size_type n) { |
102 length_ -= n; | 104 length_ -= n; |
103 } | 105 } |
104 | 106 |
105 int compare(const StringPiece& x) const { | 107 int compare(const BasicStringPiece<STRING_TYPE>& x) const { |
106 int r = wordmemcmp( | 108 int r = wordmemcmp( |
107 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); | 109 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); |
108 if (r == 0) { | 110 if (r == 0) { |
109 if (length_ < x.length_) r = -1; | 111 if (length_ < x.length_) r = -1; |
110 else if (length_ > x.length_) r = +1; | 112 else if (length_ > x.length_) r = +1; |
111 } | 113 } |
112 return r; | 114 return r; |
113 } | 115 } |
114 | 116 |
115 std::string as_string() const { | 117 STRING_TYPE as_string() const { |
116 // std::string doesn't like to take a NULL pointer even with a 0 size. | 118 // std::string doesn't like to take a NULL pointer even with a 0 size. |
117 return std::string(!empty() ? data() : "", size()); | 119 return empty() ? STRING_TYPE() : STRING_TYPE(data(), size()); |
118 } | 120 } |
119 | 121 |
120 void CopyToString(std::string* target) const; | 122 const_iterator begin() const { return ptr_; } |
121 void AppendToString(std::string* target) const; | 123 const_iterator end() const { return ptr_ + length_; } |
122 | |
123 // Does "this" start with "x" | |
124 bool starts_with(const StringPiece& x) const { | |
125 return ((length_ >= x.length_) && | |
126 (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); | |
127 } | |
128 | |
129 // Does "this" end with "x" | |
130 bool ends_with(const StringPiece& x) const { | |
131 return ((length_ >= x.length_) && | |
132 (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); | |
133 } | |
134 | |
135 iterator begin() const { return ptr_; } | |
136 iterator end() const { return ptr_ + length_; } | |
137 const_reverse_iterator rbegin() const { | 124 const_reverse_iterator rbegin() const { |
138 return const_reverse_iterator(ptr_ + length_); | 125 return const_reverse_iterator(ptr_ + length_); |
139 } | 126 } |
140 const_reverse_iterator rend() const { | 127 const_reverse_iterator rend() const { |
141 return const_reverse_iterator(ptr_); | 128 return const_reverse_iterator(ptr_); |
142 } | 129 } |
143 | 130 |
144 size_type max_size() const { return length_; } | 131 size_type max_size() const { return length_; } |
145 size_type capacity() const { return length_; } | 132 size_type capacity() const { return length_; } |
146 | 133 |
| 134 static int wordmemcmp(const value_type* p, |
| 135 const value_type* p2, |
| 136 size_type N) { |
| 137 return STRING_TYPE::traits_type::compare(p, p2, N); |
| 138 } |
| 139 |
| 140 protected: |
| 141 const value_type* ptr_; |
| 142 size_type length_; |
| 143 }; |
| 144 |
| 145 template <typename STRING_TYPE> |
| 146 const typename StringPieceDetail<STRING_TYPE>::size_type |
| 147 StringPieceDetail<STRING_TYPE>::npos = |
| 148 typename StringPieceDetail<STRING_TYPE>::size_type(-1); |
| 149 |
| 150 // MSVC doesn't like complex extern templates and DLLs. |
| 151 #if !defined(COMPILER_MSVC) |
| 152 extern template class BASE_EXPORT StringPieceDetail<std::string>; |
| 153 extern template class BASE_EXPORT StringPieceDetail<string16>; |
| 154 #endif |
| 155 |
| 156 } // namespace internal |
| 157 |
| 158 // Defines the template type that is instantiated as either StringPiece or |
| 159 // StringPiece16. |
| 160 template <typename STRING_TYPE> class BasicStringPiece : |
| 161 public internal::StringPieceDetail<STRING_TYPE> { |
| 162 public: |
| 163 typedef typename internal::StringPieceDetail<STRING_TYPE>::value_type |
| 164 value_type; |
| 165 typedef typename internal::StringPieceDetail<STRING_TYPE>::size_type |
| 166 size_type; |
| 167 |
| 168 BasicStringPiece() {} |
| 169 BasicStringPiece(const value_type*str) |
| 170 : internal::StringPieceDetail<STRING_TYPE>(str) {} |
| 171 BasicStringPiece(const STRING_TYPE& str) |
| 172 : internal::StringPieceDetail<STRING_TYPE>(str) {} |
| 173 BasicStringPiece(const value_type* offset, size_type len) |
| 174 : internal::StringPieceDetail<STRING_TYPE>(offset, len) {} |
| 175 BasicStringPiece(const typename STRING_TYPE::const_iterator& begin, |
| 176 const typename STRING_TYPE::const_iterator& end) |
| 177 : internal::StringPieceDetail<STRING_TYPE>(begin, end) {} |
| 178 }; |
| 179 |
| 180 // Specializes BasicStringPiece for std::string to add a few operations that |
| 181 // are not needed for string16. |
| 182 template <> class BASE_EXPORT BasicStringPiece<std::string> : |
| 183 public internal::StringPieceDetail<std::string> { |
| 184 public: |
| 185 BasicStringPiece() {} |
| 186 BasicStringPiece(const char* str) |
| 187 : internal::StringPieceDetail<std::string>(str) {} |
| 188 BasicStringPiece(const std::string& str) |
| 189 : internal::StringPieceDetail<std::string>(str) {} |
| 190 BasicStringPiece(const char* offset, size_type len) |
| 191 : internal::StringPieceDetail<std::string>(offset, len) {} |
| 192 BasicStringPiece(const std::string::const_iterator& begin, |
| 193 const std::string::const_iterator& end) |
| 194 : internal::StringPieceDetail<std::string>(begin, end) {} |
| 195 |
| 196 // Prevent the following overload of set() from hiding the definitions in the |
| 197 // base class. |
| 198 using internal::StringPieceDetail<std::string>::set; |
| 199 |
| 200 void set(const void* data, size_type len) { |
| 201 ptr_ = reinterpret_cast<const value_type*>(data); |
| 202 length_ = len; |
| 203 } |
| 204 |
| 205 void CopyToString(std::string* target) const; |
| 206 void AppendToString(std::string* target) const; |
| 207 |
| 208 // Does "this" start with "x" |
| 209 bool starts_with(const BasicStringPiece& x) const { |
| 210 return ((length_ >= x.length_) && |
| 211 (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); |
| 212 } |
| 213 |
| 214 // Does "this" end with "x" |
| 215 bool ends_with(const BasicStringPiece& x) const { |
| 216 return ((length_ >= x.length_) && |
| 217 (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
| 218 } |
| 219 |
147 size_type copy(char* buf, size_type n, size_type pos = 0) const; | 220 size_type copy(char* buf, size_type n, size_type pos = 0) const; |
148 | 221 |
149 size_type find(const StringPiece& s, size_type pos = 0) const; | 222 size_type find(const BasicStringPiece& s, size_type pos = 0) const; |
150 size_type find(char c, size_type pos = 0) const; | 223 size_type find(char c, size_type pos = 0) const; |
151 size_type rfind(const StringPiece& s, size_type pos = npos) const; | 224 size_type rfind(const BasicStringPiece& s, size_type pos = npos) const; |
152 size_type rfind(char c, size_type pos = npos) const; | 225 size_type rfind(char c, size_type pos = npos) const; |
153 | 226 |
154 size_type find_first_of(const StringPiece& s, size_type pos = 0) const; | 227 size_type find_first_of(const BasicStringPiece& s, size_type pos = 0) const; |
155 size_type find_first_of(char c, size_type pos = 0) const { | 228 size_type find_first_of(char c, size_type pos = 0) const { |
156 return find(c, pos); | 229 return find(c, pos); |
157 } | 230 } |
158 size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const; | 231 size_type find_first_not_of(const BasicStringPiece& s, |
| 232 size_type pos = 0) const; |
159 size_type find_first_not_of(char c, size_type pos = 0) const; | 233 size_type find_first_not_of(char c, size_type pos = 0) const; |
160 size_type find_last_of(const StringPiece& s, size_type pos = npos) const; | 234 size_type find_last_of(const BasicStringPiece& s, size_type pos = npos) const; |
161 size_type find_last_of(char c, size_type pos = npos) const { | 235 size_type find_last_of(char c, size_type pos = npos) const { |
162 return rfind(c, pos); | 236 return rfind(c, pos); |
163 } | 237 } |
164 size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const; | 238 size_type find_last_not_of(const BasicStringPiece& s, |
| 239 size_type pos = npos) const; |
165 size_type find_last_not_of(char c, size_type pos = npos) const; | 240 size_type find_last_not_of(char c, size_type pos = npos) const; |
166 | 241 |
167 StringPiece substr(size_type pos, size_type n = npos) const; | 242 BasicStringPiece substr(size_type pos, size_type n = npos) const; |
168 | |
169 static int wordmemcmp(const char* p, const char* p2, size_type N) { | |
170 return memcmp(p, p2, N); | |
171 } | |
172 | |
173 private: | |
174 const char* ptr_; | |
175 size_type length_; | |
176 }; | 243 }; |
177 | 244 |
178 class BASE_EXPORT StringPiece16 { | 245 // MSVC doesn't like complex extern templates and DLLs. |
179 public: | 246 #if !defined(COMPILER_MSVC) |
180 // standard STL container boilerplate | 247 // We can't explicitly declare the std::string instantiation here because it was |
181 typedef size_t size_type; | 248 // already instantiated when specialized, above. Not only is it a no-op, but |
182 typedef char16 value_type; | 249 // currently it also crashes Clang (see http://crbug.com/107412). |
183 typedef const char16* pointer; | 250 extern template class BASE_EXPORT BasicStringPiece<string16>; |
184 typedef const char16& reference; | 251 #endif |
185 typedef const char16& const_reference; | |
186 typedef ptrdiff_t difference_type; | |
187 typedef const char16* const_iterator; | |
188 typedef const char16* iterator; | |
189 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
190 typedef std::reverse_iterator<iterator> reverse_iterator; | |
191 | 252 |
192 public: | 253 typedef BasicStringPiece<std::string> StringPiece; |
193 // We provide non-explicit singleton constructors so users can pass | 254 typedef BasicStringPiece<string16> StringPiece16; |
194 // in a "const char16*" or a "string16" wherever a "StringPiece16" is | |
195 // expected. | |
196 StringPiece16() : ptr_(NULL), length_(0) { } | |
197 StringPiece16(const char16* str) | |
198 : ptr_(str), | |
199 length_((str == NULL) ? 0 : string16::traits_type::length(str)) { } | |
200 StringPiece16(const string16& str) | |
201 : ptr_(str.data()), length_(str.size()) { } | |
202 StringPiece16(const char16* offset, size_type len) | |
203 : ptr_(offset), length_(len) { } | |
204 StringPiece16(const string16::const_iterator& begin, | |
205 const string16::const_iterator& end) | |
206 : ptr_((end > begin) ? &(*begin) : NULL), | |
207 length_((end > begin) ? (size_type)(end - begin) : 0) { } | |
208 | |
209 // data() may return a pointer to a buffer with embedded NULs, and the | |
210 // returned buffer may or may not be null terminated. Therefore it is | |
211 // typically a mistake to pass data() to a routine that expects a NUL | |
212 // terminated string. | |
213 const char16* data() const { return ptr_; } | |
214 size_type size() const { return length_; } | |
215 size_type length() const { return length_; } | |
216 bool empty() const { return length_ == 0; } | |
217 | |
218 void clear() { | |
219 ptr_ = NULL; | |
220 length_ = 0; | |
221 } | |
222 void set(const char16* data, size_type len) { | |
223 ptr_ = data; | |
224 length_ = len; | |
225 } | |
226 void set(const char16* str) { | |
227 ptr_ = str; | |
228 length_ = str ? string16::traits_type::length(str) : 0; | |
229 } | |
230 | |
231 char16 operator[](size_type i) const { return ptr_[i]; } | |
232 | |
233 string16 as_string16() const { | |
234 // StringPiece claims that this is bad when data() is NULL, but unittesting | |
235 // seems to say otherwise. | |
236 return string16(data(), size()); | |
237 } | |
238 | |
239 iterator begin() const { return ptr_; } | |
240 iterator end() const { return ptr_ + length_; } | |
241 const_reverse_iterator rbegin() const { | |
242 return const_reverse_iterator(ptr_ + length_); | |
243 } | |
244 const_reverse_iterator rend() const { | |
245 return const_reverse_iterator(ptr_); | |
246 } | |
247 | |
248 size_type max_size() const { return length_; } | |
249 size_type capacity() const { return length_; } | |
250 | |
251 static int wordmemcmp(const char16* p, const char16* p2, size_type N) { | |
252 return string16::traits_type::compare(p, p2, N); | |
253 } | |
254 | |
255 private: | |
256 const char16* ptr_; | |
257 size_type length_; | |
258 }; | |
259 | 255 |
260 BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y); | 256 BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y); |
261 | 257 |
262 inline bool operator!=(const StringPiece& x, const StringPiece& y) { | 258 inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
263 return !(x == y); | 259 return !(x == y); |
264 } | 260 } |
265 | 261 |
266 inline bool operator<(const StringPiece& x, const StringPiece& y) { | 262 inline bool operator<(const StringPiece& x, const StringPiece& y) { |
267 const int r = StringPiece::wordmemcmp( | 263 const int r = StringPiece::wordmemcmp( |
268 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); | 264 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 } | 344 } |
349 inline size_t hash_value(const base::StringPiece16& sp16) { | 345 inline size_t hash_value(const base::StringPiece16& sp16) { |
350 HASH_STRING_PIECE(base::StringPiece16, sp16); | 346 HASH_STRING_PIECE(base::StringPiece16, sp16); |
351 } | 347 } |
352 | 348 |
353 #endif // COMPILER | 349 #endif // COMPILER |
354 | 350 |
355 } // namespace BASE_HASH_NAMESPACE | 351 } // namespace BASE_HASH_NAMESPACE |
356 | 352 |
357 #endif // BASE_STRING_PIECE_H_ | 353 #endif // BASE_STRING_PIECE_H_ |
OLD | NEW |