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

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

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 #ifdef OS_MACOSX
6 #include <CoreFoundation/CoreFoundation.h>
7 #endif
8
9 #include <float.h>
10 #include <string.h>
11
12 #include "base/format_macros.h"
13 #include "base/string_util.h"
14 #include "chrome/browser/sync/notifier/base/string.h"
15 #include "talk/base/common.h"
16 #include "talk/base/logging.h"
17 #include "talk/base/stringencode.h"
18
19 using base::snprintf;
20
21 namespace notifier {
22
23 std::string HtmlEncode(const std::string& src) {
24 size_t max_length = src.length() * 6 + 1;
25 std::string dest;
26 dest.resize(max_length);
27 size_t new_size = talk_base::html_encode(&dest[0], max_length,
28 src.data(), src.length());
29 dest.resize(new_size);
30 return dest;
31 }
32
33 std::string HtmlDecode(const std::string& src) {
34 size_t max_length = src.length() + 1;
35 std::string dest;
36 dest.resize(max_length);
37 size_t new_size = talk_base::html_decode(&dest[0], max_length,
38 src.data(), src.length());
39 dest.resize(new_size);
40 return dest;
41 }
42
43 std::string UrlEncode(const std::string& src) {
44 size_t max_length = src.length() * 6 + 1;
45 std::string dest;
46 dest.resize(max_length);
47 size_t new_size = talk_base::url_encode(&dest[0], max_length,
48 src.data(), src.length());
49 dest.resize(new_size);
50 return dest;
51 }
52
53 std::string UrlDecode(const std::string& src) {
54 size_t max_length = src.length() + 1;
55 std::string dest;
56 dest.resize(max_length);
57 size_t new_size = talk_base::url_decode(&dest[0], max_length,
58 src.data(), src.length());
59 dest.resize(new_size);
60 return dest;
61 }
62
63 int CharToHexValue(char hex) {
64 if (hex >= '0' && hex <= '9') {
65 return hex - '0';
66 } else if (hex >= 'A' && hex <= 'F') {
67 return hex - 'A' + 10;
68 } else if (hex >= 'a' && hex <= 'f') {
69 return hex - 'a' + 10;
70 } else {
71 return -1;
72 }
73 }
74
75 // Template function to convert a string to an int/int64
76 // If strict is true, check for the validity and overflow
77 template<typename T>
78 bool ParseStringToIntTemplate(const char* str,
79 T* value,
80 bool strict,
81 T min_value) {
82 ASSERT(str);
83 ASSERT(value);
84
85 // Skip spaces
86 while (*str == ' ') {
87 ++str;
88 }
89
90 // Process sign
91 int c = static_cast<int>(*str++); // current char
92 int possible_sign = c; // save sign indication
93 if (c == '-' || c == '+') {
94 c = static_cast<int>(*str++);
95 }
96
97 // Process numbers
98 T total = 0;
99 while (c && (c = CharToDigit(static_cast<char>(c))) != -1) {
100 // Check for overflow
101 if (strict && (total < min_value / 10 ||
102 (total == min_value / 10 &&
103 c > ((-(min_value + 10)) % 10)))) {
104 return false;
105 }
106
107 // Accumulate digit
108 // Note that we accumulate in the negative direction so that we will not
109 // blow away with the largest negative number
110 total = 10 * total - c;
111
112 // Get next char
113 c = static_cast<int>(*str++);
114 }
115
116 // Fail if encountering non-numeric character
117 if (strict && c == -1) {
118 return false;
119 }
120
121 // Negate the number if needed
122 if (possible_sign == '-') {
123 *value = total;
124 } else {
125 // Check for overflow
126 if (strict && total == min_value) {
127 return false;
128 }
129
130 *value = -total;
131 }
132
133 return true;
134 }
135
136 // Convert a string to an int
137 // If strict is true, check for the validity and overflow
138 bool ParseStringToInt(const char* str, int* value, bool strict) {
139 return ParseStringToIntTemplate<int>(str, value, strict, kint32min);
140 }
141
142 // Convert a string to an int
143 // This version does not check for the validity and overflow
144 int StringToInt(const char* str) {
145 int value = 0;
146 ParseStringToInt(str, &value, false);
147 return value;
148 }
149
150 // Convert a string to an unsigned int.
151 // If strict is true, check for the validity and overflow
152 bool ParseStringToUint(const char* str, uint32* value, bool strict) {
153 ASSERT(str);
154 ASSERT(value);
155
156 int64 int64_value;
157 if (!ParseStringToInt64(str, &int64_value, strict)) {
158 return false;
159 }
160 if (int64_value < 0 || int64_value > kuint32max) {
161 return false;
162 }
163
164 *value = static_cast<uint32>(int64_value);
165 return true;
166 }
167
168 // Convert a string to an int
169 // This version does not check for the validity and overflow
170 uint32 StringToUint(const char* str) {
171 uint32 value = 0;
172 ParseStringToUint(str, &value, false);
173 return value;
174 }
175
176 // Convert a string to an int64
177 // If strict is true, check for the validity and overflow
178 bool ParseStringToInt64(const char* str, int64* value, bool strict) {
179 return ParseStringToIntTemplate<int64>(str, value, strict, kint64min);
180 }
181
182 // Convert a string to an int64
183 // This version does not check for the validity and overflow
184 int64 StringToInt64(const char* str) {
185 int64 value = 0;
186 ParseStringToInt64(str, &value, false);
187 return value;
188 }
189
190 // Convert a string to a double
191 // If strict is true, check for the validity and overflow
192 bool ParseStringToDouble(const char* str, double* value, bool strict) {
193 ASSERT(str);
194 ASSERT(value);
195
196 // Skip spaces
197 while (*str == ' ') {
198 ++str;
199 }
200
201 // Process sign
202 int c = static_cast<int>(*str++); // current char
203 int sign = c; // save sign indication
204 if (c == '-' || c == '+') {
205 c = static_cast<int>(*str++);
206 }
207
208 // Process numbers before "."
209 double total = 0.0;
210 while (c && (c != '.') && (c = CharToDigit(static_cast<char>(c))) != -1) {
211 // Check for overflow
212 if (strict && total >= DBL_MAX / 10) {
213 return false;
214 }
215
216 // Accumulate digit
217 total = 10.0 * total + c;
218
219 // Get next char
220 c = static_cast<int>(*str++);
221 }
222
223 // Process "."
224 if (c == '.') {
225 c = static_cast<int>(*str++);
226 } else {
227 // Fail if encountering non-numeric character
228 if (strict && c == -1) {
229 return false;
230 }
231 }
232
233 // Process numbers after "."
234 double power = 1.0;
235 while ((c = CharToDigit(static_cast<char>(c))) != -1) {
236 // Check for overflow
237 if (strict && total >= DBL_MAX / 10) {
238 return false;
239 }
240
241 // Accumulate digit
242 total = 10.0 * total + c;
243 power *= 10.0;
244
245 // Get next char
246 c = static_cast<int>(*str++);
247 }
248
249 // Get the final number
250 *value = total / power;
251 if (sign == '-') {
252 *value = -(*value);
253 }
254
255 return true;
256 }
257
258 // Convert a string to a double
259 // This version does not check for the validity and overflow
260 double StringToDouble(const char* str) {
261 double value = 0;
262 ParseStringToDouble(str, &value, false);
263 return value;
264 }
265
266 // Convert a float to a string
267 std::string FloatToString(float f) {
268 char buf[80];
269 snprintf(buf, sizeof(buf), "%f", f);
270 return std::string(buf);
271 }
272
273 std::string DoubleToString(double d) {
274 char buf[160];
275 snprintf(buf, sizeof(buf), "%.17g", d);
276 return std::string(buf);
277 }
278
279 std::string UIntToString(uint32 i) {
280 char buf[80];
281 snprintf(buf, sizeof(buf), "%lu", i);
282 return std::string(buf);
283 }
284
285 // Convert an int to a string
286 std::string IntToString(int i) {
287 char buf[80];
288 snprintf(buf, sizeof(buf), "%d", i);
289 return std::string(buf);
290 }
291
292 // Convert an int64 to a string
293 std::string Int64ToString(int64 i64) {
294 char buf[80];
295 snprintf(buf, sizeof(buf), "%" PRId64 "d", i64);
296 return std::string(buf);
297 }
298
299 std::string UInt64ToString(uint64 i64) {
300 char buf[80];
301 snprintf(buf, sizeof(buf), "%" PRId64 "u", i64);
302 return std::string(buf);
303 }
304
305 std::string Int64ToHexString(int64 i64) {
306 char buf[80];
307 snprintf(buf, sizeof(buf), "%" PRId64 "x", i64);
308 return std::string(buf);
309 }
310
311 // Parse a single "delim" delimited string from "*source"
312 // Modify *source to point after the delimiter.
313 // If no delimiter is present after the string, set *source to NULL.
314 //
315 // Mainly a stringified wrapper around strpbrk()
316 std::string SplitOneStringToken(const char** source, const char* delim) {
317 ASSERT(source);
318 ASSERT(delim);
319
320 if (!*source) {
321 return std::string();
322 }
323 const char* begin = *source;
324 *source = strpbrk(*source, delim);
325 if (*source) {
326 return std::string(begin, (*source)++);
327 } else {
328 return std::string(begin);
329 }
330 }
331
332 std::string LowerWithUnderToPascalCase(const char* lower_with_under) {
333 ASSERT(lower_with_under);
334
335 std::string pascal_case;
336 bool make_upper = true;
337 for (; *lower_with_under != '\0'; lower_with_under++) {
338 char current_char = *lower_with_under;
339 if (current_char == '_') {
340 ASSERT(!make_upper);
341 make_upper = true;
342 continue;
343 }
344 if (make_upper) {
345 current_char = toupper(current_char);
346 make_upper = false;
347 }
348 pascal_case.append(1, current_char);
349 }
350 return pascal_case;
351 }
352
353 std::string PascalCaseToLowerWithUnder(const char* pascal_case) {
354 ASSERT(pascal_case);
355
356 std::string lower_with_under;
357 bool previous_was_upper = true;
358 for(; *pascal_case != '\0'; pascal_case++) {
359 char current_char = *pascal_case;
360 if (isupper(current_char)) {
361 // DNSName should be dns_name
362 if ((islower(pascal_case[1]) && !lower_with_under.empty()) ||
363 !previous_was_upper) {
364 lower_with_under.append(1, '_');
365 }
366 current_char = tolower(current_char);
367 } else if (previous_was_upper) {
368 previous_was_upper = false;
369 }
370 lower_with_under.append(1, current_char);
371 }
372 return lower_with_under;
373 }
374 void StringReplace(std::string* s,
375 const char* old_sub,
376 const char* new_sub,
377 bool replace_all) {
378 ASSERT(s);
379
380 // If old_sub is empty, nothing to do
381 if (!old_sub || !*old_sub) {
382 return;
383 }
384
385 int old_sub_size = strlen(old_sub);
386 std::string res;
387 std::string::size_type start_pos = 0;
388
389 do {
390 std::string::size_type pos = s->find(old_sub, start_pos);
391 if (pos == std::string::npos) {
392 break;
393 }
394 res.append(*s, start_pos, pos - start_pos);
395 res.append(new_sub);
396 start_pos = pos + old_sub_size; // start searching again after the "old"
397 } while (replace_all);
398 res.append(*s, start_pos, s->length() - start_pos);
399
400 *s = res;
401 }
402
403 } // namespace notifier
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698