OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 : public BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator, | 339 : public BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator, |
340 VALUE, | 340 VALUE, |
341 BASE> {}; | 341 BASE> {}; |
342 | 342 |
343 template <typename VALUE> | 343 template <typename VALUE> |
344 bool String16ToIntImpl(const StringPiece16& input, VALUE* output) { | 344 bool String16ToIntImpl(const StringPiece16& input, VALUE* output) { |
345 return IteratorRangeToNumber<StringPiece16ToNumberTraits<VALUE, 10> >::Invoke( | 345 return IteratorRangeToNumber<StringPiece16ToNumberTraits<VALUE, 10> >::Invoke( |
346 input.begin(), input.end(), output); | 346 input.begin(), input.end(), output); |
347 } | 347 } |
348 | 348 |
| 349 // A class template for mapping the size of a pointer to its corresponding |
| 350 // fundamental integer type. |
| 351 template<int PointerSize> |
| 352 class PointerSizeToIntT { |
| 353 }; |
| 354 |
| 355 // A partial specialization of PointerSizeToIntT for 32-bit pointers. |
| 356 template<> |
| 357 class PointerSizeToIntT<sizeof(uint32)> { |
| 358 public: |
| 359 typedef uint32 IntType; |
| 360 }; |
| 361 |
| 362 // A partial specialization of PointerSizeToIntT for 64-bit pointers. |
| 363 template<> |
| 364 class PointerSizeToIntT<sizeof(uint64)> { |
| 365 public: |
| 366 typedef uint64 IntType; |
| 367 }; |
| 368 |
| 369 // A collection of helper functions for round-tripping pointer values. |
| 370 class PointerHelper { |
| 371 public: |
| 372 // The appropriate fundamental integer type for a pointer. |
| 373 typedef PointerSizeToIntT<sizeof(void*)>::IntType IntType; |
| 374 |
| 375 static std::string ToString(void* value) { |
| 376 return IntToStringT<std::string, IntType, IntType, false>:: |
| 377 IntToString(reinterpret_cast<IntType>(value)); |
| 378 } |
| 379 |
| 380 static string16 ToString16(void* value) { |
| 381 return IntToStringT<string16, IntType, IntType, false>:: |
| 382 IntToString(reinterpret_cast<IntType>(value)); |
| 383 } |
| 384 |
| 385 static bool ToPointer(const base::StringPiece& input, void** output) { |
| 386 return StringToIntImpl(input, reinterpret_cast<IntType*>(output)); |
| 387 } |
| 388 |
| 389 static bool ToPointer16(const base::StringPiece16& input, void** output) { |
| 390 return String16ToIntImpl(input, reinterpret_cast<IntType*>(output)); |
| 391 } |
| 392 }; |
| 393 |
349 } // namespace | 394 } // namespace |
350 | 395 |
351 std::string IntToString(int value) { | 396 std::string IntToString(int value) { |
352 return IntToStringT<std::string, int, unsigned int, true>:: | 397 return IntToStringT<std::string, int, unsigned int, true>:: |
353 IntToString(value); | 398 IntToString(value); |
354 } | 399 } |
355 | 400 |
356 string16 IntToString16(int value) { | 401 string16 IntToString16(int value) { |
357 return IntToStringT<string16, int, unsigned int, true>:: | 402 return IntToStringT<string16, int, unsigned int, true>:: |
358 IntToString(value); | 403 IntToString(value); |
(...skipping 21 matching lines...) Expand all Loading... |
380 std::string Uint64ToString(uint64 value) { | 425 std::string Uint64ToString(uint64 value) { |
381 return IntToStringT<std::string, uint64, uint64, false>:: | 426 return IntToStringT<std::string, uint64, uint64, false>:: |
382 IntToString(value); | 427 IntToString(value); |
383 } | 428 } |
384 | 429 |
385 string16 Uint64ToString16(uint64 value) { | 430 string16 Uint64ToString16(uint64 value) { |
386 return IntToStringT<string16, uint64, uint64, false>:: | 431 return IntToStringT<string16, uint64, uint64, false>:: |
387 IntToString(value); | 432 IntToString(value); |
388 } | 433 } |
389 | 434 |
| 435 std::string PointerToString(void* value) { |
| 436 return PointerHelper::ToString(value); |
| 437 } |
| 438 |
| 439 string16 PointerToString16(void* value) { |
| 440 return PointerHelper::ToString16(value); |
| 441 } |
| 442 |
390 std::string DoubleToString(double value) { | 443 std::string DoubleToString(double value) { |
391 // According to g_fmt.cc, it is sufficient to declare a buffer of size 32. | 444 // According to g_fmt.cc, it is sufficient to declare a buffer of size 32. |
392 char buffer[32]; | 445 char buffer[32]; |
393 dmg_fp::g_fmt(buffer, value); | 446 dmg_fp::g_fmt(buffer, value); |
394 return std::string(buffer); | 447 return std::string(buffer); |
395 } | 448 } |
396 | 449 |
397 bool StringToInt(const StringPiece& input, int* output) { | 450 bool StringToInt(const StringPiece& input, int* output) { |
398 return StringToIntImpl(input, output); | 451 return StringToIntImpl(input, output); |
399 } | 452 } |
(...skipping 19 matching lines...) Expand all Loading... |
419 } | 472 } |
420 | 473 |
421 bool StringToUint64(const StringPiece& input, uint64* output) { | 474 bool StringToUint64(const StringPiece& input, uint64* output) { |
422 return StringToIntImpl(input, output); | 475 return StringToIntImpl(input, output); |
423 } | 476 } |
424 | 477 |
425 bool StringToUint64(const StringPiece16& input, uint64* output) { | 478 bool StringToUint64(const StringPiece16& input, uint64* output) { |
426 return String16ToIntImpl(input, output); | 479 return String16ToIntImpl(input, output); |
427 } | 480 } |
428 | 481 |
| 482 bool StringToPointer(const StringPiece& input, void** output) { |
| 483 return PointerHelper::ToPointer(input, output); |
| 484 } |
| 485 |
| 486 bool StringToPointer(const StringPiece16& input, void** output) { |
| 487 return PointerHelper::ToPointer16(input, output); |
| 488 } |
| 489 |
429 bool StringToSizeT(const StringPiece& input, size_t* output) { | 490 bool StringToSizeT(const StringPiece& input, size_t* output) { |
430 return StringToIntImpl(input, output); | 491 return StringToIntImpl(input, output); |
431 } | 492 } |
432 | 493 |
433 bool StringToSizeT(const StringPiece16& input, size_t* output) { | 494 bool StringToSizeT(const StringPiece16& input, size_t* output) { |
434 return String16ToIntImpl(input, output); | 495 return String16ToIntImpl(input, output); |
435 } | 496 } |
436 | 497 |
437 bool StringToDouble(const std::string& input, double* output) { | 498 bool StringToDouble(const std::string& input, double* output) { |
438 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows. | 499 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 bool HexStringToInt(const StringPiece& input, int* output) { | 540 bool HexStringToInt(const StringPiece& input, int* output) { |
480 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( | 541 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( |
481 input.begin(), input.end(), output); | 542 input.begin(), input.end(), output); |
482 } | 543 } |
483 | 544 |
484 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { | 545 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { |
485 return HexStringToBytesT(input, output); | 546 return HexStringToBytesT(input, output); |
486 } | 547 } |
487 | 548 |
488 } // namespace base | 549 } // namespace base |
OLD | NEW |