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

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: Updated StringPiece constructor interface to use iterators. 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
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> 295 typedef BaseIteratorRangeToNumberTraits<StringPiece::const_iterator, int, 10>
296 IteratorRangeToIntTraits; 296 IteratorRangeToIntTraits;
erikwright (departed) 2011/12/13 20:20:45 How about: namespace { template <typename STRING
297 typedef BaseIteratorRangeToNumberTraits<string16::const_iterator, int, 10> 297 typedef BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator, int, 10>
298 WideIteratorRangeToIntTraits; 298 WideIteratorRangeToIntTraits;
299 typedef BaseIteratorRangeToNumberTraits<std::string::const_iterator, int64, 10> 299 typedef BaseIteratorRangeToNumberTraits<StringPiece::const_iterator, int64, 10>
300 IteratorRangeToInt64Traits; 300 IteratorRangeToInt64Traits;
301 typedef BaseIteratorRangeToNumberTraits<string16::const_iterator, int64, 10> 301 typedef BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator,
302 int64,
303 10>
302 WideIteratorRangeToInt64Traits; 304 WideIteratorRangeToInt64Traits;
303 305
304 typedef BaseIteratorRangeToNumberTraits<const char*, int, 10> 306 typedef BaseIteratorRangeToNumberTraits<const char*, int, 10>
305 CharBufferToIntTraits; 307 CharBufferToIntTraits;
306 typedef BaseIteratorRangeToNumberTraits<const char16*, int, 10> 308 typedef BaseIteratorRangeToNumberTraits<const char16*, int, 10>
307 WideCharBufferToIntTraits; 309 WideCharBufferToIntTraits;
308 typedef BaseIteratorRangeToNumberTraits<const char*, int64, 10> 310 typedef BaseIteratorRangeToNumberTraits<const char*, int64, 10>
309 CharBufferToInt64Traits; 311 CharBufferToInt64Traits;
310 typedef BaseIteratorRangeToNumberTraits<const char16*, int64, 10> 312 typedef BaseIteratorRangeToNumberTraits<const char16*, int64, 10>
311 WideCharBufferToInt64Traits; 313 WideCharBufferToInt64Traits;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 IntToString(value); 385 IntToString(value);
384 } 386 }
385 387
386 std::string DoubleToString(double value) { 388 std::string DoubleToString(double value) {
387 // According to g_fmt.cc, it is sufficient to declare a buffer of size 32. 389 // According to g_fmt.cc, it is sufficient to declare a buffer of size 32.
388 char buffer[32]; 390 char buffer[32];
389 dmg_fp::g_fmt(buffer, value); 391 dmg_fp::g_fmt(buffer, value);
390 return std::string(buffer); 392 return std::string(buffer);
391 } 393 }
392 394
393 bool StringToInt(const std::string& input, int* output) { 395 bool StringToInt(const StringPiece& input, int* output) {
394 return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(input.begin(), 396 return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(input.begin(),
395 input.end(), 397 input.end(),
396 output); 398 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,
401 int* output) {
402 return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(begin,
403 end,
404 output);
405 }
406
407 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER)
408 bool StringToInt(const char* begin, const char* end, int* 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( 402 return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke(
417 input.begin(), input.end(), output); 403 input.begin(), input.end(), output);
418 } 404 }
419 405
420 bool StringToInt(string16::const_iterator begin, 406 bool StringToInt64(const StringPiece& input, int64* output) {
421 string16::const_iterator end,
422 int* output) {
423 return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke(begin,
424 end,
425 output);
426 }
427
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( 407 return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke(
438 input.begin(), input.end(), output); 408 input.begin(), input.end(), output);
439 } 409 }
440 410
441 bool StringToInt64(std::string::const_iterator begin, 411 bool StringToInt64(const StringPiece16& input, int64* output) {
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( 412 return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke(
459 input.begin(), input.end(), output); 413 input.begin(), input.end(), output);
460 } 414 }
461 415
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) { 416 bool StringToDouble(const std::string& input, double* output) {
479 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows. 417 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows.
480 char* endptr = NULL; 418 char* endptr = NULL;
481 *output = dmg_fp::strtod(input.c_str(), &endptr); 419 *output = dmg_fp::strtod(input.c_str(), &endptr);
482 420
483 // Cases to return false: 421 // Cases to return false:
484 // - If errno is ERANGE, there was an overflow or underflow. 422 // - If errno is ERANGE, there was an overflow or underflow.
485 // - If the input string is empty, there was nothing to parse. 423 // - 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 424 // - 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 425 // 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'); 448 std::string ret(size * 2, '\0');
511 449
512 for (size_t i = 0; i < size; ++i) { 450 for (size_t i = 0; i < size; ++i) {
513 char b = reinterpret_cast<const char*>(bytes)[i]; 451 char b = reinterpret_cast<const char*>(bytes)[i];
514 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; 452 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
515 ret[(i * 2) + 1] = kHexChars[b & 0xf]; 453 ret[(i * 2) + 1] = kHexChars[b & 0xf];
516 } 454 }
517 return ret; 455 return ret;
518 } 456 }
519 457
520 bool HexStringToInt(const std::string& input, int* output) { 458 bool HexStringToInt(const std::string& input, int* output) {
erikwright (departed) 2011/12/13 20:20:45 It would be a bonus to also convert these function
521 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( 459 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(
522 input.begin(), input.end(), output); 460 input.begin(), input.end(), output);
523 } 461 }
524 462
525 bool HexStringToInt(std::string::const_iterator begin, 463 bool HexStringToInt(std::string::const_iterator begin,
526 std::string::const_iterator end, 464 std::string::const_iterator end,
527 int* output) { 465 int* output) {
528 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(begin, 466 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(begin,
529 end, 467 end,
530 output); 468 output);
531 } 469 }
532 470
533 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER) 471 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER)
534 bool HexStringToInt(const char* begin, const char* end, int* output) { 472 bool HexStringToInt(const char* begin, const char* end, int* output) {
535 return IteratorRangeToNumber<HexCharBufferToIntTraits>::Invoke(begin, 473 return IteratorRangeToNumber<HexCharBufferToIntTraits>::Invoke(begin,
536 end, 474 end,
537 output); 475 output);
538 } 476 }
539 #endif 477 #endif
540 478
541 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { 479 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) {
542 return HexStringToBytesT(input, output); 480 return HexStringToBytesT(input, output);
543 } 481 }
544 482
545 } // namespace base 483 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698