string_number_conversions.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_
  5. #define BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. #include <vector>
  10. #include "base/base_export.h"
  11. #include "base/containers/span.h"
  12. #include "base/strings/string16.h"
  13. #include "base/strings/string_piece.h"
  14. #include "build/build_config.h"
  15. // ----------------------------------------------------------------------------
  16. // IMPORTANT MESSAGE FROM YOUR SPONSOR
  17. //
  18. // Please do not add "convenience" functions for converting strings to integers
  19. // that return the value and ignore success/failure. That encourages people to
  20. // write code that doesn't properly handle the error conditions.
  21. //
  22. // DO NOT use these functions in any UI unless it's NOT localized on purpose.
  23. // Instead, use base::MessageFormatter for a complex message with numbers
  24. // (integer, float, double) embedded or base::Format{Number,Double,Percent} to
  25. // just format a single number/percent. Note that some languages use native
  26. // digits instead of ASCII digits while others use a group separator or decimal
  27. // point different from ',' and '.'. Using these functions in the UI would lead
  28. // numbers to be formatted in a non-native way.
  29. // ----------------------------------------------------------------------------
  30. namespace base {
  31. // Number -> string conversions ------------------------------------------------
  32. // Ignores locale! see warning above.
  33. BASE_EXPORT std::string NumberToString(int value);
  34. BASE_EXPORT string16 NumberToString16(int value);
  35. BASE_EXPORT std::string NumberToString(unsigned int value);
  36. BASE_EXPORT string16 NumberToString16(unsigned int value);
  37. BASE_EXPORT std::string NumberToString(long value);
  38. BASE_EXPORT string16 NumberToString16(long value);
  39. BASE_EXPORT std::string NumberToString(unsigned long value);
  40. BASE_EXPORT string16 NumberToString16(unsigned long value);
  41. BASE_EXPORT std::string NumberToString(long long value);
  42. BASE_EXPORT string16 NumberToString16(long long value);
  43. BASE_EXPORT std::string NumberToString(unsigned long long value);
  44. BASE_EXPORT string16 NumberToString16(unsigned long long value);
  45. BASE_EXPORT std::string NumberToString(double value);
  46. BASE_EXPORT string16 NumberToString16(double value);
  47. // String -> number conversions ------------------------------------------------
  48. // Perform a best-effort conversion of the input string to a numeric type,
  49. // setting |*output| to the result of the conversion. Returns true for
  50. // "perfect" conversions; returns false in the following cases:
  51. // - Overflow. |*output| will be set to the maximum value supported
  52. // by the data type.
  53. // - Underflow. |*output| will be set to the minimum value supported
  54. // by the data type.
  55. // - Trailing characters in the string after parsing the number. |*output|
  56. // will be set to the value of the number that was parsed.
  57. // - Leading whitespace in the string before parsing the number. |*output| will
  58. // be set to the value of the number that was parsed.
  59. // - No characters parseable as a number at the beginning of the string.
  60. // |*output| will be set to 0.
  61. // - Empty string. |*output| will be set to 0.
  62. // WARNING: Will write to |output| even when returning false.
  63. // Read the comments above carefully.
  64. BASE_EXPORT bool StringToInt(StringPiece input, int* output);
  65. BASE_EXPORT bool StringToInt(StringPiece16 input, int* output);
  66. BASE_EXPORT bool StringToUint(StringPiece input, unsigned* output);
  67. BASE_EXPORT bool StringToUint(StringPiece16 input, unsigned* output);
  68. BASE_EXPORT bool StringToInt64(StringPiece input, int64_t* output);
  69. BASE_EXPORT bool StringToInt64(StringPiece16 input, int64_t* output);
  70. BASE_EXPORT bool StringToUint64(StringPiece input, uint64_t* output);
  71. BASE_EXPORT bool StringToUint64(StringPiece16 input, uint64_t* output);
  72. BASE_EXPORT bool StringToSizeT(StringPiece input, size_t* output);
  73. BASE_EXPORT bool StringToSizeT(StringPiece16 input, size_t* output);
  74. // For floating-point conversions, only conversions of input strings in decimal
  75. // form are defined to work. Behavior with strings representing floating-point
  76. // numbers in hexadecimal, and strings representing non-finite values (such as
  77. // NaN and inf) is undefined. Otherwise, these behave the same as the integral
  78. // variants. This expects the input string to NOT be specific to the locale.
  79. // If your input is locale specific, use ICU to read the number.
  80. // WARNING: Will write to |output| even when returning false.
  81. // Read the comments here and above StringToInt() carefully.
  82. BASE_EXPORT bool StringToDouble(StringPiece input, double* output);
  83. BASE_EXPORT bool StringToDouble(StringPiece16 input, double* output);
  84. // Hex encoding ----------------------------------------------------------------
  85. // Returns a hex string representation of a binary buffer. The returned hex
  86. // string will be in upper case. This function does not check if |size| is
  87. // within reasonable limits since it's written with trusted data in mind. If
  88. // you suspect that the data you want to format might be large, the absolute
  89. // max size for |size| should be is
  90. // std::numeric_limits<size_t>::max() / 2
  91. BASE_EXPORT std::string HexEncode(const void* bytes, size_t size);
  92. BASE_EXPORT std::string HexEncode(base::span<const uint8_t> bytes);
  93. // Best effort conversion, see StringToInt above for restrictions.
  94. // Will only successful parse hex values that will fit into |output|, i.e.
  95. // -0x80000000 < |input| < 0x7FFFFFFF.
  96. BASE_EXPORT bool HexStringToInt(StringPiece input, int* output);
  97. // Best effort conversion, see StringToInt above for restrictions.
  98. // Will only successful parse hex values that will fit into |output|, i.e.
  99. // 0x00000000 < |input| < 0xFFFFFFFF.
  100. // The string is not required to start with 0x.
  101. BASE_EXPORT bool HexStringToUInt(StringPiece input, uint32_t* output);
  102. // Best effort conversion, see StringToInt above for restrictions.
  103. // Will only successful parse hex values that will fit into |output|, i.e.
  104. // -0x8000000000000000 < |input| < 0x7FFFFFFFFFFFFFFF.
  105. BASE_EXPORT bool HexStringToInt64(StringPiece input, int64_t* output);
  106. // Best effort conversion, see StringToInt above for restrictions.
  107. // Will only successful parse hex values that will fit into |output|, i.e.
  108. // 0x0000000000000000 < |input| < 0xFFFFFFFFFFFFFFFF.
  109. // The string is not required to start with 0x.
  110. BASE_EXPORT bool HexStringToUInt64(StringPiece input, uint64_t* output);
  111. // Similar to the previous functions, except that output is a vector of bytes.
  112. // |*output| will contain as many bytes as were successfully parsed prior to the
  113. // error. There is no overflow, but input.size() must be evenly divisible by 2.
  114. // Leading 0x or +/- are not allowed.
  115. BASE_EXPORT bool HexStringToBytes(StringPiece input,
  116. std::vector<uint8_t>* output);
  117. // Same as HexStringToBytes, but for an std::string.
  118. BASE_EXPORT bool HexStringToString(StringPiece input, std::string* output);
  119. // Decodes the hex string |input| into a presized |output|. The output buffer
  120. // must be sized exactly to |input.size() / 2| or decoding will fail and no
  121. // bytes will be written to |output|. Decoding an empty input is also
  122. // considered a failure. When decoding fails due to encountering invalid input
  123. // characters, |output| will have been filled with the decoded bytes up until
  124. // the failure.
  125. BASE_EXPORT bool HexStringToSpan(StringPiece input, base::span<uint8_t> output);
  126. } // namespace base
  127. #if defined(OS_WIN)
  128. #include "base/strings/string_number_conversions_win.h"
  129. #endif
  130. #endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_