summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/notifier/base/string.cc
blob: 4c441f85fc3856c07995d467a60ac6ef7ea4de4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef OS_MACOSX
#include <CoreFoundation/CoreFoundation.h>
#endif

#include <float.h>
#include <string.h>

#include "base/format_macros.h"
#include "base/string_util.h"
#include "chrome/browser/sync/notifier/base/string.h"
#include "talk/base/common.h"
#include "talk/base/logging.h"
#include "talk/base/stringencode.h"

using base::snprintf;

namespace notifier {

std::string HtmlEncode(const std::string& src) {
  size_t max_length = src.length() * 6 + 1;
  std::string dest;
  dest.resize(max_length);
  size_t new_size = talk_base::html_encode(&dest[0], max_length,
                                           src.data(), src.length());
  dest.resize(new_size);
  return dest;
}

std::string HtmlDecode(const std::string& src) {
  size_t max_length = src.length() + 1;
  std::string dest;
  dest.resize(max_length);
  size_t new_size = talk_base::html_decode(&dest[0], max_length,
                                           src.data(), src.length());
  dest.resize(new_size);
  return dest;
}

std::string UrlEncode(const std::string& src) {
  size_t max_length = src.length() * 6 + 1;
  std::string dest;
  dest.resize(max_length);
  size_t new_size = talk_base::url_encode(&dest[0], max_length,
                                          src.data(), src.length());
  dest.resize(new_size);
  return dest;
}

std::string UrlDecode(const std::string& src) {
  size_t max_length = src.length() + 1;
  std::string dest;
  dest.resize(max_length);
  size_t new_size = talk_base::url_decode(&dest[0], max_length,
                                          src.data(), src.length());
  dest.resize(new_size);
  return dest;
}

int CharToHexValue(char hex) {
  if (hex >= '0' && hex <= '9') {
    return hex - '0';
  } else if (hex >= 'A' && hex <= 'F') {
    return hex - 'A' + 10;
  } else if (hex >= 'a' && hex <= 'f') {
    return hex - 'a' + 10;
  } else {
    return -1;
  }
}

// Template function to convert a string to an int/int64. If strict is true,
// check for the validity and overflow
template<typename T>
bool ParseStringToIntTemplate(const char* str,
                              T* value,
                              bool strict,
                              T min_value) {
  ASSERT(str);
  ASSERT(value);

  // Skip spaces.
  while (*str == ' ') {
    ++str;
  }

  // Process sign.
  int c = static_cast<int>(*str++);  // current char
  int possible_sign = c;  // save sign indication
  if (c == '-' || c == '+') {
    c = static_cast<int>(*str++);
  }

  // Process numbers.
  T total = 0;
  while (c && (c = CharToDigit(static_cast<char>(c))) != -1) {
    // Check for overflow.
    if (strict && (total < min_value / 10 ||
                   (total == min_value / 10 &&
                    c > ((-(min_value + 10)) % 10)))) {
      return false;
    }

    // Accumulate digit.
    // Note that we accumulate in the negative direction so that we will not
    // blow away with the largest negative number
    total = 10 * total - c;

    // Get next char.
    c = static_cast<int>(*str++);
  }

  // Fail if encountering non-numeric character.
  if (strict && c == -1) {
    return false;
  }

  // Negate the number if needed.
  if (possible_sign == '-') {
    *value = total;
  } else {
    // Check for overflow.
    if (strict && total == min_value) {
      return false;
    }

    *value = -total;
  }

  return true;
}

// Convert a string to an int. If strict is true, check for the validity and
// overflow.
bool ParseStringToInt(const char* str, int* value, bool strict) {
  return ParseStringToIntTemplate<int>(str, value, strict, kint32min);
}

// Convert a string to an int. This version does not check for the validity and
// overflow
int StringToInt(const char* str) {
  int value = 0;
  ParseStringToInt(str, &value, false);
  return value;
}

// Convert a string to an unsigned int. If strict is true, check for the
// validity and overflow
bool ParseStringToUint(const char* str, uint32* value, bool strict) {
  ASSERT(str);
  ASSERT(value);

  int64 int64_value;
  if (!ParseStringToInt64(str, &int64_value, strict)) {
    return false;
  }
  if (int64_value < 0 || int64_value > kuint32max) {
    return false;
  }

  *value = static_cast<uint32>(int64_value);
  return true;
}

// Convert a string to an int. This version does not check for the validity and
// overflow.
uint32 StringToUint(const char* str) {
  uint32 value = 0;
  ParseStringToUint(str, &value, false);
  return value;
}

// Convert a string to an int64. If strict is true, check for the validity and
// overflow.
bool ParseStringToInt64(const char* str, int64* value, bool strict) {
  return ParseStringToIntTemplate<int64>(str, value, strict, kint64min);
}

// Convert a string to an int64. This version does not check for the validity
// and overflow.
int64 StringToInt64(const char* str) {
  int64 value = 0;
  ParseStringToInt64(str, &value, false);
  return value;
}

// Convert a string to a double. If strict is true, check for the validity and
// overflow.
bool ParseStringToDouble(const char* str, double* value, bool strict) {
  ASSERT(str);
  ASSERT(value);

  // Skip spaces.
  while (*str == ' ') {
    ++str;
  }

  // Process sign.
  int c = static_cast<int>(*str++);  // Current char.
  int sign = c;  // save sign indication
  if (c == '-' || c == '+') {
    c = static_cast<int>(*str++);
  }

  // Process numbers before ".".
  double total = 0.0;
  while (c && (c != '.') && (c = CharToDigit(static_cast<char>(c))) != -1) {
    // Check for overflow.
    if (strict && total >= DBL_MAX / 10) {
      return false;
    }

    // Accumulate digit.
    total = 10.0 * total + c;

    // Get next char.
    c = static_cast<int>(*str++);
  }

  // Process ".".
  if (c == '.') {
    c = static_cast<int>(*str++);
  } else {
    // Fail if encountering non-numeric character.
    if (strict && c == -1) {
      return false;
    }
  }

  // Process numbers after ".".
  double power = 1.0;
  while ((c = CharToDigit(static_cast<char>(c))) != -1) {
    // Check for overflow.
    if (strict && total >= DBL_MAX / 10) {
      return false;
    }

    // Accumulate digit.
    total = 10.0 * total + c;
    power *= 10.0;

    // Get next char.
    c = static_cast<int>(*str++);
  }

  // Get the final number.
  *value = total / power;
  if (sign == '-') {
    *value = -(*value);
  }

  return true;
}

// Convert a string to a double. This version does not check for the validity
// and overflow.
double StringToDouble(const char* str) {
  double value = 0;
  ParseStringToDouble(str, &value, false);
  return value;
}

// Convert a float to a string.
std::string FloatToString(float f) {
  char buf[80];
  snprintf(buf, sizeof(buf), "%f", f);
  return std::string(buf);
}

std::string DoubleToString(double d) {
  char buf[160];
  snprintf(buf, sizeof(buf), "%.17g", d);
  return std::string(buf);
}

std::string UIntToString(uint32 i) {
  char buf[80];
  snprintf(buf, sizeof(buf), "%lu", i);
  return std::string(buf);
}

// Convert an int to a string
std::string IntToString(int i) {
  char buf[80];
  snprintf(buf, sizeof(buf), "%d", i);
  return std::string(buf);
}

// Convert an int64 to a string
std::string Int64ToString(int64 i64) {
  char buf[80];
  snprintf(buf, sizeof(buf), "%" PRId64 "d", i64);
  return std::string(buf);
}

std::string UInt64ToString(uint64 i64) {
  char buf[80];
  snprintf(buf, sizeof(buf), "%" PRId64 "u", i64);
  return std::string(buf);
}

std::string Int64ToHexString(int64 i64) {
  char buf[80];
  snprintf(buf, sizeof(buf), "%" PRId64 "x", i64);
  return std::string(buf);
}

// Parse a single "delim" delimited string from "*source". Modify *source to
// point after the delimiter. If no delimiter is present after the string, set
// *source to NULL.
//
// Mainly a stringified wrapper around strpbrk().
std::string SplitOneStringToken(const char** source, const char* delim) {
  ASSERT(source);
  ASSERT(delim);

  if (!*source) {
    return std::string();
  }
  const char* begin = *source;
  *source = strpbrk(*source, delim);
  if (*source) {
    return std::string(begin, (*source)++);
  } else {
    return std::string(begin);
  }
}

std::string LowerWithUnderToPascalCase(const char* lower_with_under) {
  ASSERT(lower_with_under);

  std::string pascal_case;
  bool make_upper = true;
  for (; *lower_with_under != '\0'; lower_with_under++) {
    char current_char = *lower_with_under;
    if (current_char == '_') {
      ASSERT(!make_upper);
      make_upper = true;
      continue;
    }
    if (make_upper) {
      current_char = toupper(current_char);
      make_upper = false;
    }
    pascal_case.append(1, current_char);
  }
  return pascal_case;
}

std::string PascalCaseToLowerWithUnder(const char* pascal_case) {
  ASSERT(pascal_case);

  std::string lower_with_under;
  bool previous_was_upper = true;
  for(; *pascal_case != '\0'; pascal_case++) {
    char current_char = *pascal_case;
    if (isupper(current_char)) {
      // DNSName should be dns_name.
      if ((islower(pascal_case[1]) && !lower_with_under.empty()) ||
          !previous_was_upper) {
        lower_with_under.append(1, '_');
      }
      current_char = tolower(current_char);
    } else if (previous_was_upper) {
      previous_was_upper = false;
    }
    lower_with_under.append(1, current_char);
  }
  return lower_with_under;
}
void StringReplace(std::string* s,
                   const char* old_sub,
                   const char* new_sub,
                   bool replace_all) {
  ASSERT(s);

  // If old_sub is empty, nothing to do.
  if (!old_sub || !*old_sub) {
    return;
  }

  int old_sub_size = strlen(old_sub);
  std::string res;
  std::string::size_type start_pos = 0;

  do {
    std::string::size_type pos = s->find(old_sub, start_pos);
    if (pos == std::string::npos) {
      break;
    }
    res.append(*s, start_pos, pos - start_pos);
    res.append(new_sub);
    start_pos = pos + old_sub_size;  // start searching again after the "old".
  } while (replace_all);
  res.append(*s, start_pos, s->length() - start_pos);

  *s = res;
}

}  // namespace notifier