Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: base/string_piece.h

Issue 6317016: Change UTF8ToUTF16 to accept const StringPiece&. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/logging.h ('k') | base/utf_string_conversions.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm>
23 #include <iosfwd>
24 #include <string> 22 #include <string>
25 23
26 #include "base/basictypes.h" 24 #include "base/basictypes.h"
27 25
28 namespace base { 26 namespace base {
29 27
30 class StringPiece { 28 class StringPiece {
31 public: 29 public:
32 // standard STL container boilerplate 30 // standard STL container boilerplate
33 typedef size_t size_type; 31 typedef size_t size_type;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 void remove_prefix(size_type n) { 84 void remove_prefix(size_type n) {
87 ptr_ += n; 85 ptr_ += n;
88 length_ -= n; 86 length_ -= n;
89 } 87 }
90 88
91 void remove_suffix(size_type n) { 89 void remove_suffix(size_type n) {
92 length_ -= n; 90 length_ -= n;
93 } 91 }
94 92
95 int compare(const StringPiece& x) const { 93 int compare(const StringPiece& x) const {
96 int r = wordmemcmp(ptr_, x.ptr_, std::min(length_, x.length_)); 94 int r = wordmemcmp(
95 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_));
97 if (r == 0) { 96 if (r == 0) {
98 if (length_ < x.length_) r = -1; 97 if (length_ < x.length_) r = -1;
99 else if (length_ > x.length_) r = +1; 98 else if (length_ > x.length_) r = +1;
100 } 99 }
101 return r; 100 return r;
102 } 101 }
103 102
104 std::string as_string() const { 103 std::string as_string() const {
105 // std::string doesn't like to take a NULL pointer even with a 0 size. 104 // std::string doesn't like to take a NULL pointer even with a 0 size.
106 return std::string(!empty() ? data() : "", size()); 105 return std::string(!empty() ? data() : "", size());
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 size_type length_; 163 size_type length_;
165 }; 164 };
166 165
167 bool operator==(const StringPiece& x, const StringPiece& y); 166 bool operator==(const StringPiece& x, const StringPiece& y);
168 167
169 inline bool operator!=(const StringPiece& x, const StringPiece& y) { 168 inline bool operator!=(const StringPiece& x, const StringPiece& y) {
170 return !(x == y); 169 return !(x == y);
171 } 170 }
172 171
173 inline bool operator<(const StringPiece& x, const StringPiece& y) { 172 inline bool operator<(const StringPiece& x, const StringPiece& y) {
174 const int r = StringPiece::wordmemcmp(x.data(), y.data(), 173 const int r = StringPiece::wordmemcmp(
175 std::min(x.size(), y.size())); 174 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
176 return ((r < 0) || ((r == 0) && (x.size() < y.size()))); 175 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
177 } 176 }
178 177
179 inline bool operator>(const StringPiece& x, const StringPiece& y) { 178 inline bool operator>(const StringPiece& x, const StringPiece& y) {
180 return y < x; 179 return y < x;
181 } 180 }
182 181
183 inline bool operator<=(const StringPiece& x, const StringPiece& y) { 182 inline bool operator<=(const StringPiece& x, const StringPiece& y) {
184 return !(x > y); 183 return !(x > y);
185 } 184 }
186 185
187 inline bool operator>=(const StringPiece& x, const StringPiece& y) { 186 inline bool operator>=(const StringPiece& x, const StringPiece& y) {
188 return !(x < y); 187 return !(x < y);
189 } 188 }
190 189
191 // allow StringPiece to be logged (needed for unit testing).
192 extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
193
194 } // namespace base 190 } // namespace base
195 191
196 #endif // BASE_STRING_PIECE_H_ 192 #endif // BASE_STRING_PIECE_H_
OLDNEW
« no previous file with comments | « base/logging.h ('k') | base/utf_string_conversions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698