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

Side by Side Diff: chrome/browser/sync/notifier/base/string.h

Issue 194065: Initial commit of sync engine code to browser/sync.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Fixes to gtest include path, reverted syncapi. Created 11 years, 3 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 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
5 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_BASE_STRING_H_
6 #define CHROME_BROWSER_SYNC_NOTIFIER_BASE_STRING_H_
7
8 #ifdef COMPILER_MSVC
9 #include <xhash>
10 #elif defined(__GNUC__)
11 #include <ext/hash_map>
12 #endif
13
14 #include <ctype.h>
15 #include <string>
16
17 #include "chrome/browser/sync/notifier/base/fastalloc.h"
18 #include "talk/base/basictypes.h"
19
20 namespace notifier {
21
22 // Does html encoding of strings.
23 std::string HtmlEncode(const std::string& src);
24
25 // Does html decoding of strings.
26 std::string HtmlDecode(const std::string& src);
27
28 // Does utl encoding of strings.
29 std::string UrlEncode(const std::string& src);
30
31 // Does url decoding of strings.
32 std::string UrlDecode(const std::string& src);
33
34 // Convert a character to a digit
35 // if the character is not a digit return -1 (same as CRT)
36 inline int CharToDigit(char c) {
37 return ((c) >= '0' && (c) <= '9' ? (c) - '0' : -1);
38 }
39
40 int CharToHexValue(char hex);
41
42 // ----------------------------------------------------------------------
43 // ParseStringToInt()
44 // ParseStringToUint()
45 // ParseStringToInt64()
46 // ParseStringToDouble()
47 // Convert a string to an int/int64/double
48 // If strict is true, check for the validity and overflow
49 // ----------------------------------------------------------------------
50
51 bool ParseStringToInt(const char* str, int* value, bool strict);
52
53 bool ParseStringToUint(const char* str, uint32* value, bool strict);
54
55 bool ParseStringToInt64(const char* str, int64* value, bool strict);
56
57 bool ParseStringToDouble(const char* str, double* value, bool strict);
58
59 // ----------------------------------------------------------------------
60 // StringToInt()
61 // StringToUint()
62 // StringToInt64()
63 // StringToDouble()
64 // Convert a string to an int/int64/double
65 // Note that these functions do not check for the validity or overflow
66 // ----------------------------------------------------------------------
67
68 int StringToInt(const char* str);
69
70 uint32 StringToUint(const char* str);
71
72 int64 StringToInt64(const char* str);
73
74 double StringToDouble(const char* str);
75
76 // ----------------------------------------------------------------------
77 // FloatToString()
78 // DoubleToString()
79 // IntToString()
80 // UIntToString()
81 // Int64ToString()
82 // UInt64ToString()
83 // Convert various types to their string representation. These
84 // all do the obvious, trivial thing.
85 // ----------------------------------------------------------------------
86
87 std::string FloatToString(float f);
88 std::string DoubleToString(double d);
89
90 std::string IntToString(int i);
91 std::string UIntToString(uint32 i);
92
93 std::string Int64ToString(int64 i64);
94 std::string UInt64ToString(uint64 i64);
95
96 std::string Int64ToHexString(int64 i64);
97
98 // ----------------------------------------------------------------------
99 // StringStartsWith()
100 // StringEndsWith()
101 // Check if a string starts or ends with a pattern
102 // ----------------------------------------------------------------------
103
104 inline bool StringStartsWith(const std::string& s, const char* p) {
105 return s.find(p) == 0;
106 }
107
108 inline bool StringEndsWith(const std::string& s, const char* p) {
109 return s.rfind(p) == (s.length() - strlen(p));
110 }
111
112 // ----------------------------------------------------------------------
113 // MakeStringEndWith()
114 // If the string does not end with a pattern, make it end with it
115 // ----------------------------------------------------------------------
116
117 inline std::string MakeStringEndWith(const std::string& s, const char* p) {
118 if (StringEndsWith(s, p)) {
119 return s;
120 } else {
121 std::string ns(s);
122 ns += p;
123 return ns;
124 }
125 }
126
127 // Convert a lower_case_string to LowerCaseString
128 std::string LowerWithUnderToPascalCase(const char* lower_with_under);
129
130 // Convert a PascalCaseString to pascal_case_string
131 std::string PascalCaseToLowerWithUnder(const char* pascal_case);
132
133 // ----------------------------------------------------------------------
134 // LowerString()
135 // LowerStringToBuf()
136 // Convert the characters in "s" to lowercase.
137 // Changes contents of "s". LowerStringToBuf copies at most
138 // "n" characters (including the terminating '\0') from "s"
139 // to another buffer.
140 // ----------------------------------------------------------------------
141
142 inline void LowerString(char* s) {
143 for (; *s; ++s) {
144 *s = tolower(*s);
145 }
146 }
147
148 inline void LowerString(std::string* s) {
149 std::string::iterator end = s->end();
150 for (std::string::iterator i = s->begin(); i != end; ++i) {
151 *i = tolower(*i);
152 }
153 }
154
155 inline void LowerStringToBuf(const char* s, char* buf, int n) {
156 for (int i = 0; i < n - 1; ++i) {
157 char c = s[i];
158 buf[i] = tolower(c);
159 if (c == '\0') {
160 return;
161 }
162 }
163 buf[n - 1] = '\0';
164 }
165
166 // ----------------------------------------------------------------------
167 // UpperString()
168 // UpperStringToBuf()
169 // Convert the characters in "s" to uppercase.
170 // UpperString changes "s". UpperStringToBuf copies at most
171 // "n" characters (including the terminating '\0') from "s"
172 // to another buffer.
173 // ----------------------------------------------------------------------
174
175 inline void UpperString(char* s) {
176 for (; *s; ++s) {
177 *s = toupper(*s);
178 }
179 }
180
181 inline void UpperString(std::string* s) {
182 for (std::string::iterator iter = s->begin(); iter != s->end(); ++iter) {
183 *iter = toupper(*iter);
184 }
185 }
186
187 inline void UpperStringToBuf(const char* s, char* buf, int n) {
188 for (int i = 0; i < n - 1; ++i) {
189 char c = s[i];
190 buf[i] = toupper(c);
191 if (c == '\0') {
192 return;
193 }
194 }
195 buf[n - 1] = '\0';
196 }
197
198 // ----------------------------------------------------------------------
199 // TrimStringLeft
200 // Removes any occurrences of the characters in 'remove' from the start
201 // of the string. Returns the number of chars trimmed.
202 // ----------------------------------------------------------------------
203 inline int TrimStringLeft(std::string* s, const char* remove) {
204 int i = 0;
205 for (; i < static_cast<int>(s->size()) && strchr(remove, (*s)[i]); ++i);
206 if (i > 0) s->erase(0, i);
207 return i;
208 }
209
210 // ----------------------------------------------------------------------
211 // TrimStringRight
212 // Removes any occurrences of the characters in 'remove' from the end
213 // of the string. Returns the number of chars trimmed.
214 // ----------------------------------------------------------------------
215 inline int TrimStringRight(std::string* s, const char* remove) {
216 int size = static_cast<int>(s->size());
217 int i = size;
218 for (; i > 0 && strchr(remove, (*s)[i - 1]); --i);
219 if (i < size) {
220 s->erase(i);
221 }
222 return size - i;
223 }
224
225 // ----------------------------------------------------------------------
226 // TrimString
227 // Removes any occurrences of the characters in 'remove' from either
228 // end of the string.
229 // ----------------------------------------------------------------------
230 inline int TrimString(std::string* s, const char* remove) {
231 return TrimStringRight(s, remove) + TrimStringLeft(s, remove);
232 }
233
234 // ----------------------------------------------------------------------
235 // StringReplace()
236 // Replace the "old" pattern with the "new" pattern in a string. If
237 // replace_all is false, it only replaces the first instance of "old."
238 // ----------------------------------------------------------------------
239
240 void StringReplace(std::string* s,
241 const char* old_sub,
242 const char* new_sub,
243 bool replace_all);
244
245 inline size_t HashString(const std::string &value) {
246 #ifdef COMPILER_MSVC
247 return stdext::hash_value(value);
248 #elif defined(__GNUC__)
249 __gnu_cxx::hash<const char*> h;
250 return h(value.c_str());
251 #else
252 // Compile time error because we don't return a value
253 #endif
254 }
255
256 // ----------------------------------------------------------------------
257 // SplitOneStringToken()
258 // Parse a single "delim" delimited string from "*source"
259 // Modify *source to point after the delimiter.
260 // If no delimiter is present after the string, set *source to NULL.
261 //
262 // If the start of *source is a delimiter, return an empty string.
263 // If *source is NULL, return an empty string.
264 // ----------------------------------------------------------------------
265 std::string SplitOneStringToken(const char** source, const char* delim);
266
267 //----------------------------------------------------------------------
268 // CharTraits provides wrappers with common function names for char/wchar_t
269 // specific CRT functions
270 //----------------------------------------------------------------------
271
272 template <class CharT> struct CharTraits {
273 };
274
275 template <>
276 struct CharTraits<char> {
277 static inline size_t length(const char* s) {
278 return strlen(s);
279 }
280 static inline bool copy(char* dst, size_t dst_size, const char* s) {
281 if (s == NULL || dst == NULL)
282 return false;
283 else
284 return copy_num(dst, dst_size, s, strlen(s));
285 }
286 static inline bool copy_num(char* dst, size_t dst_size, const char* s,
287 size_t s_len) {
288 if (dst_size < (s_len + 1))
289 return false;
290 memcpy(dst, s, s_len);
291 dst[s_len] = '\0';
292 return true;
293 }
294 };
295
296 template <>
297 struct CharTraits<wchar_t> {
298 static inline size_t length(const wchar_t* s) {
299 return wcslen(s);
300 }
301 static inline bool copy(wchar_t* dst, size_t dst_size, const wchar_t* s) {
302 if (s == NULL || dst == NULL)
303 return false;
304 else
305 return copy_num(dst, dst_size, s, wcslen(s));
306 }
307 static inline bool copy_num(wchar_t* dst, size_t dst_size, const wchar_t* s,
308 size_t s_len) {
309 if (dst_size < (s_len + 1)) {
310 return false;
311 }
312 memcpy(dst, s, s_len * sizeof(wchar_t));
313 dst[s_len] = '\0';
314 return true;
315 }
316 };
317
318 //----------------------------------------------------------------------
319 // This class manages a fixed-size, null-terminated string buffer. It is
320 // meant to be allocated on the stack, and it makes no use of the heap
321 // internally. In most cases you'll just want to use a std::(w)string, but
322 // when you need to avoid the heap, you can use this class instead.
323 //
324 // Methods are provided to read the null-terminated buffer and to append
325 // data to the buffer, and once the buffer fills-up, it simply discards any
326 // extra append calls.
327 //----------------------------------------------------------------------
328
329 template <class CharT, int MaxSize>
330 class FixedString {
331 public:
332 typedef CharTraits<CharT> char_traits;
333
334 FixedString() : index_(0), truncated_(false) {
335 buf_[0] = CharT(0);
336 }
337
338 ~FixedString() {
339 memset(buf_, 0xCC, sizeof(buf_));
340 }
341
342 // Returns true if the Append ever failed.
343 bool was_truncated() const { return truncated_; }
344
345 // Returns the number of characters in the string, excluding the null
346 // terminator.
347 size_t size() const { return index_; }
348
349 // Returns the null-terminated string.
350 const CharT* get() const { return buf_; }
351 CharT* get() { return buf_; }
352
353 // Append an array of characters. The operation is bounds checked, and if
354 // there is insufficient room, then the was_truncated() flag is set to true.
355 void Append(const CharT* s, size_t n) {
356 if (char_traits::copy_num(buf_ + index_, arraysize(buf_) - index_, s, n)) {
357 index_ += n;
358 } else {
359 truncated_ = true;
360 }
361 }
362
363 // Append a null-terminated string.
364 void Append(const CharT* s) {
365 Append(s, char_traits::length(s));
366 }
367
368 // Append a single character.
369 void Append(CharT c) {
370 Append(&c, 1);
371 }
372
373 private:
374 CharT buf_[MaxSize];
375 size_t index_;
376 bool truncated_;
377 };
378
379 } // namespace notifier
380
381 #endif // CHROME_BROWSER_SYNC_NOTIFIER_BASE_STRING_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698