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

Side by Side Diff: base/string_number_conversions.cc

Issue 8921006: Standardize StringToInt{,64} interface. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix call syntax of StringToInt() in Chrome OS code. Created 9 years 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
« no previous file with comments | « base/string_number_conversions.h ('k') | base/string_number_conversions_unittest.cc » ('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 4
5 #include "base/string_number_conversions.h" 5 #include "base/string_number_conversions.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <wctype.h> 10 #include <wctype.h>
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 typedef VALUE value_type; 285 typedef VALUE value_type;
286 static value_type min() { 286 static value_type min() {
287 return std::numeric_limits<value_type>::min(); 287 return std::numeric_limits<value_type>::min();
288 } 288 }
289 static value_type max() { 289 static value_type max() {
290 return std::numeric_limits<value_type>::max(); 290 return std::numeric_limits<value_type>::max();
291 } 291 }
292 static const int kBase = BASE; 292 static const int kBase = BASE;
293 }; 293 };
294 294
295 typedef BaseIteratorRangeToNumberTraits<std::string::const_iterator, int, 10>
296 IteratorRangeToIntTraits;
297 typedef BaseIteratorRangeToNumberTraits<string16::const_iterator, int, 10>
298 WideIteratorRangeToIntTraits;
299 typedef BaseIteratorRangeToNumberTraits<std::string::const_iterator, int64, 10>
300 IteratorRangeToInt64Traits;
301 typedef BaseIteratorRangeToNumberTraits<string16::const_iterator, int64, 10>
302 WideIteratorRangeToInt64Traits;
303
304 typedef BaseIteratorRangeToNumberTraits<const char*, int, 10>
305 CharBufferToIntTraits;
306 typedef BaseIteratorRangeToNumberTraits<const char16*, int, 10>
307 WideCharBufferToIntTraits;
308 typedef BaseIteratorRangeToNumberTraits<const char*, int64, 10>
309 CharBufferToInt64Traits;
310 typedef BaseIteratorRangeToNumberTraits<const char16*, int64, 10>
311 WideCharBufferToInt64Traits;
312
313 template<typename ITERATOR> 295 template<typename ITERATOR>
314 class BaseHexIteratorRangeToIntTraits 296 class BaseHexIteratorRangeToIntTraits
315 : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> { 297 : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> {
316 public: 298 public:
317 // Allow parsing of 0xFFFFFFFF, which is technically an overflow 299 // Allow parsing of 0xFFFFFFFF, which is technically an overflow
318 static unsigned int max() { 300 static unsigned int max() {
319 return std::numeric_limits<unsigned int>::max(); 301 return std::numeric_limits<unsigned int>::max();
320 } 302 }
321 }; 303 };
322 304
323 typedef BaseHexIteratorRangeToIntTraits<std::string::const_iterator> 305 typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator>
324 HexIteratorRangeToIntTraits; 306 HexIteratorRangeToIntTraits;
325 typedef BaseHexIteratorRangeToIntTraits<const char*>
326 HexCharBufferToIntTraits;
327 307
328 template<typename STR> 308 template<typename STR>
329 bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { 309 bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) {
330 DCHECK_EQ(output->size(), 0u); 310 DCHECK_EQ(output->size(), 0u);
331 size_t count = input.size(); 311 size_t count = input.size();
332 if (count == 0 || (count % 2) != 0) 312 if (count == 0 || (count % 2) != 0)
333 return false; 313 return false;
334 for (uintptr_t i = 0; i < count / 2; ++i) { 314 for (uintptr_t i = 0; i < count / 2; ++i) {
335 uint8 msb = 0; // most significant 4 bits 315 uint8 msb = 0; // most significant 4 bits
336 uint8 lsb = 0; // least significant 4 bits 316 uint8 lsb = 0; // least significant 4 bits
337 if (!CharToDigit<16>(input[i * 2], &msb) || 317 if (!CharToDigit<16>(input[i * 2], &msb) ||
338 !CharToDigit<16>(input[i * 2 + 1], &lsb)) 318 !CharToDigit<16>(input[i * 2 + 1], &lsb))
339 return false; 319 return false;
340 output->push_back((msb << 4) | lsb); 320 output->push_back((msb << 4) | lsb);
341 } 321 }
342 return true; 322 return true;
343 } 323 }
344 324
325 template <typename VALUE, int BASE>
326 class StringPieceToNumberTraits
327 : public BaseIteratorRangeToNumberTraits<StringPiece::const_iterator,
328 VALUE,
329 BASE> {};
330
331 template <typename VALUE>
332 bool StringToIntImpl(const StringPiece& input, VALUE* output) {
333 return IteratorRangeToNumber<StringPieceToNumberTraits<VALUE, 10> >::Invoke(
334 input.begin(), input.end(), output);
335 }
336
337 template <typename VALUE, int BASE>
338 class StringPiece16ToNumberTraits
339 : public BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator,
340 VALUE,
341 BASE> {};
342
343 template <typename VALUE>
344 bool String16ToIntImpl(const StringPiece16& input, VALUE* output) {
345 return IteratorRangeToNumber<StringPiece16ToNumberTraits<VALUE, 10> >::Invoke(
346 input.begin(), input.end(), output);
347 }
348
345 } // namespace 349 } // namespace
346 350
347 std::string IntToString(int value) { 351 std::string IntToString(int value) {
348 return IntToStringT<std::string, int, unsigned int, true>:: 352 return IntToStringT<std::string, int, unsigned int, true>::
349 IntToString(value); 353 IntToString(value);
350 } 354 }
351 355
352 string16 IntToString16(int value) { 356 string16 IntToString16(int value) {
353 return IntToStringT<string16, int, unsigned int, true>:: 357 return IntToStringT<string16, int, unsigned int, true>::
354 IntToString(value); 358 IntToString(value);
(...skipping 28 matching lines...) Expand all
383 IntToString(value); 387 IntToString(value);
384 } 388 }
385 389
386 std::string DoubleToString(double value) { 390 std::string DoubleToString(double value) {
387 // According to g_fmt.cc, it is sufficient to declare a buffer of size 32. 391 // According to g_fmt.cc, it is sufficient to declare a buffer of size 32.
388 char buffer[32]; 392 char buffer[32];
389 dmg_fp::g_fmt(buffer, value); 393 dmg_fp::g_fmt(buffer, value);
390 return std::string(buffer); 394 return std::string(buffer);
391 } 395 }
392 396
393 bool StringToInt(const std::string& input, int* output) { 397 bool StringToInt(const StringPiece& input, int* output) {
394 return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(input.begin(), 398 return StringToIntImpl(input, output);
395 input.end(),
396 output);
397 } 399 }
398 400
399 bool StringToInt(std::string::const_iterator begin, 401 bool StringToInt(const StringPiece16& input, int* output) {
400 std::string::const_iterator end, 402 return String16ToIntImpl(input, output);
401 int* output) {
402 return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(begin,
403 end,
404 output);
405 } 403 }
406 404
407 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER) 405 bool StringToInt64(const StringPiece& input, int64* output) {
408 bool StringToInt(const char* begin, const char* end, int* output) { 406 return StringToIntImpl(input, output);
409 return IteratorRangeToNumber<CharBufferToIntTraits>::Invoke(begin,
410 end,
411 output);
412 }
413 #endif
414
415 bool StringToInt(const string16& input, int* output) {
416 return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke(
417 input.begin(), input.end(), output);
418 } 407 }
419 408
420 bool StringToInt(string16::const_iterator begin, 409 bool StringToInt64(const StringPiece16& input, int64* output) {
421 string16::const_iterator end, 410 return String16ToIntImpl(input, output);
422 int* output) {
423 return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke(begin,
424 end,
425 output);
426 } 411 }
427 412
428 #if !defined(BASE_STRING16_ITERATOR_IS_CHAR16_POINTER)
429 bool StringToInt(const char16* begin, const char16* end, int* output) {
430 return IteratorRangeToNumber<WideCharBufferToIntTraits>::Invoke(begin,
431 end,
432 output);
433 }
434 #endif
435
436 bool StringToInt64(const std::string& input, int64* output) {
437 return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke(
438 input.begin(), input.end(), output);
439 }
440
441 bool StringToInt64(std::string::const_iterator begin,
442 std::string::const_iterator end,
443 int64* output) {
444 return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke(begin,
445 end,
446 output);
447 }
448
449 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER)
450 bool StringToInt64(const char* begin, const char* end, int64* output) {
451 return IteratorRangeToNumber<CharBufferToInt64Traits>::Invoke(begin,
452 end,
453 output);
454 }
455 #endif
456
457 bool StringToInt64(const string16& input, int64* output) {
458 return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke(
459 input.begin(), input.end(), output);
460 }
461
462 bool StringToInt64(string16::const_iterator begin,
463 string16::const_iterator end,
464 int64* output) {
465 return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke(begin,
466 end,
467 output);
468 }
469
470 #if !defined(BASE_STRING16_ITERATOR_IS_CHAR16_POINTER)
471 bool StringToInt64(const char16* begin, const char16* end, int64* output) {
472 return IteratorRangeToNumber<WideCharBufferToInt64Traits>::Invoke(begin,
473 end,
474 output);
475 }
476 #endif
477
478 bool StringToDouble(const std::string& input, double* output) { 413 bool StringToDouble(const std::string& input, double* output) {
479 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows. 414 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows.
480 char* endptr = NULL; 415 char* endptr = NULL;
481 *output = dmg_fp::strtod(input.c_str(), &endptr); 416 *output = dmg_fp::strtod(input.c_str(), &endptr);
482 417
483 // Cases to return false: 418 // Cases to return false:
484 // - If errno is ERANGE, there was an overflow or underflow. 419 // - If errno is ERANGE, there was an overflow or underflow.
485 // - If the input string is empty, there was nothing to parse. 420 // - If the input string is empty, there was nothing to parse.
486 // - If endptr does not point to the end of the string, there are either 421 // - If endptr does not point to the end of the string, there are either
487 // characters remaining in the string after a parsed number, or the string 422 // characters remaining in the string after a parsed number, or the string
(...skipping 22 matching lines...) Expand all
510 std::string ret(size * 2, '\0'); 445 std::string ret(size * 2, '\0');
511 446
512 for (size_t i = 0; i < size; ++i) { 447 for (size_t i = 0; i < size; ++i) {
513 char b = reinterpret_cast<const char*>(bytes)[i]; 448 char b = reinterpret_cast<const char*>(bytes)[i];
514 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; 449 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
515 ret[(i * 2) + 1] = kHexChars[b & 0xf]; 450 ret[(i * 2) + 1] = kHexChars[b & 0xf];
516 } 451 }
517 return ret; 452 return ret;
518 } 453 }
519 454
520 bool HexStringToInt(const std::string& input, int* output) { 455 bool HexStringToInt(const StringPiece& input, int* output) {
521 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( 456 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(
522 input.begin(), input.end(), output); 457 input.begin(), input.end(), output);
523 } 458 }
524 459
525 bool HexStringToInt(std::string::const_iterator begin,
526 std::string::const_iterator end,
527 int* output) {
528 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(begin,
529 end,
530 output);
531 }
532
533 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER)
534 bool HexStringToInt(const char* begin, const char* end, int* output) {
535 return IteratorRangeToNumber<HexCharBufferToIntTraits>::Invoke(begin,
536 end,
537 output);
538 }
539 #endif
540
541 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { 460 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) {
542 return HexStringToBytesT(input, output); 461 return HexStringToBytesT(input, output);
543 } 462 }
544 463
545 } // namespace base 464 } // namespace base
OLDNEW
« no previous file with comments | « base/string_number_conversions.h ('k') | base/string_number_conversions_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698