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 |
11 // to include this .h file in other files rather than forward-declaring | 11 // to include this .h file in other files rather than forward-declaring |
12 // StringPiece as would be appropriate for most other Google classes. | 12 // StringPiece as would be appropriate for most other Google classes. |
13 // | 13 // |
14 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary | 14 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
15 // conversions from "const char*" to "string" and back again. | 15 // conversions from "const char*" to "string" and back again. |
16 // | 16 // |
17 | 17 |
18 #ifndef BASE_STRING_PIECE_H_ | 18 #ifndef BASE_STRING_PIECE_H_ |
19 #define BASE_STRING_PIECE_H_ | 19 #define BASE_STRING_PIECE_H_ |
20 #pragma once | 20 #pragma once |
21 | 21 |
22 #include <string> | 22 #include <string> |
23 | 23 |
| 24 #include "base/base_api.h" |
24 #include "base/basictypes.h" | 25 #include "base/basictypes.h" |
25 | 26 |
26 namespace base { | 27 namespace base { |
27 | 28 |
28 class StringPiece { | 29 class BASE_API StringPiece { |
29 public: | 30 public: |
30 // standard STL container boilerplate | 31 // standard STL container boilerplate |
31 typedef size_t size_type; | 32 typedef size_t size_type; |
32 typedef char value_type; | 33 typedef char value_type; |
33 typedef const char* pointer; | 34 typedef const char* pointer; |
34 typedef const char& reference; | 35 typedef const char& reference; |
35 typedef const char& const_reference; | 36 typedef const char& const_reference; |
36 typedef ptrdiff_t difference_type; | 37 typedef ptrdiff_t difference_type; |
37 typedef const char* const_iterator; | 38 typedef const char* const_iterator; |
38 typedef const char* iterator; | 39 typedef const char* iterator; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 157 |
157 static int wordmemcmp(const char* p, const char* p2, size_type N) { | 158 static int wordmemcmp(const char* p, const char* p2, size_type N) { |
158 return memcmp(p, p2, N); | 159 return memcmp(p, p2, N); |
159 } | 160 } |
160 | 161 |
161 private: | 162 private: |
162 const char* ptr_; | 163 const char* ptr_; |
163 size_type length_; | 164 size_type length_; |
164 }; | 165 }; |
165 | 166 |
166 bool operator==(const StringPiece& x, const StringPiece& y); | 167 BASE_API bool operator==(const StringPiece& x, const StringPiece& y); |
167 | 168 |
168 inline bool operator!=(const StringPiece& x, const StringPiece& y) { | 169 inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
169 return !(x == y); | 170 return !(x == y); |
170 } | 171 } |
171 | 172 |
172 inline bool operator<(const StringPiece& x, const StringPiece& y) { | 173 inline bool operator<(const StringPiece& x, const StringPiece& y) { |
173 const int r = StringPiece::wordmemcmp( | 174 const int r = StringPiece::wordmemcmp( |
174 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); | 175 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); |
175 return ((r < 0) || ((r == 0) && (x.size() < y.size()))); | 176 return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
176 } | 177 } |
177 | 178 |
178 inline bool operator>(const StringPiece& x, const StringPiece& y) { | 179 inline bool operator>(const StringPiece& x, const StringPiece& y) { |
179 return y < x; | 180 return y < x; |
180 } | 181 } |
181 | 182 |
182 inline bool operator<=(const StringPiece& x, const StringPiece& y) { | 183 inline bool operator<=(const StringPiece& x, const StringPiece& y) { |
183 return !(x > y); | 184 return !(x > y); |
184 } | 185 } |
185 | 186 |
186 inline bool operator>=(const StringPiece& x, const StringPiece& y) { | 187 inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
187 return !(x < y); | 188 return !(x < y); |
188 } | 189 } |
189 | 190 |
190 } // namespace base | 191 } // namespace base |
191 | 192 |
192 #endif // BASE_STRING_PIECE_H_ | 193 #endif // BASE_STRING_PIECE_H_ |
OLD | NEW |