xref: /aosp_15_r20/external/libchrome/base/strings/string_number_conversions.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <string>
12*635a8641SAndroid Build Coastguard Worker #include <vector>
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/strings/string16.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h"
17*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker // ----------------------------------------------------------------------------
20*635a8641SAndroid Build Coastguard Worker // IMPORTANT MESSAGE FROM YOUR SPONSOR
21*635a8641SAndroid Build Coastguard Worker //
22*635a8641SAndroid Build Coastguard Worker // This file contains no "wstring" variants. New code should use string16. If
23*635a8641SAndroid Build Coastguard Worker // you need to make old code work, use the UTF8 version and convert. Please do
24*635a8641SAndroid Build Coastguard Worker // not add wstring variants.
25*635a8641SAndroid Build Coastguard Worker //
26*635a8641SAndroid Build Coastguard Worker // Please do not add "convenience" functions for converting strings to integers
27*635a8641SAndroid Build Coastguard Worker // that return the value and ignore success/failure. That encourages people to
28*635a8641SAndroid Build Coastguard Worker // write code that doesn't properly handle the error conditions.
29*635a8641SAndroid Build Coastguard Worker //
30*635a8641SAndroid Build Coastguard Worker // DO NOT use these functions in any UI unless it's NOT localized on purpose.
31*635a8641SAndroid Build Coastguard Worker // Instead, use base::MessageFormatter for a complex message with numbers
32*635a8641SAndroid Build Coastguard Worker // (integer, float, double) embedded or base::Format{Number,Double,Percent} to
33*635a8641SAndroid Build Coastguard Worker // just format a single number/percent. Note that some languages use native
34*635a8641SAndroid Build Coastguard Worker // digits instead of ASCII digits while others use a group separator or decimal
35*635a8641SAndroid Build Coastguard Worker // point different from ',' and '.'. Using these functions in the UI would lead
36*635a8641SAndroid Build Coastguard Worker // numbers to be formatted in a non-native way.
37*635a8641SAndroid Build Coastguard Worker // ----------------------------------------------------------------------------
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker namespace base {
40*635a8641SAndroid Build Coastguard Worker 
41*635a8641SAndroid Build Coastguard Worker // Number -> string conversions ------------------------------------------------
42*635a8641SAndroid Build Coastguard Worker 
43*635a8641SAndroid Build Coastguard Worker // Ignores locale! see warning above.
44*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string NumberToString(int value);
45*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 NumberToString16(int value);
46*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string NumberToString(unsigned int value);
47*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 NumberToString16(unsigned int value);
48*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string NumberToString(long value);
49*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 NumberToString16(long value);
50*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string NumberToString(unsigned long value);
51*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 NumberToString16(unsigned long value);
52*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string NumberToString(long long value);
53*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 NumberToString16(long long value);
54*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string NumberToString(unsigned long long value);
55*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 NumberToString16(unsigned long long value);
56*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string NumberToString(double value);
57*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 NumberToString16(double value);
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker // Type-specific naming for backwards compatibility.
60*635a8641SAndroid Build Coastguard Worker //
61*635a8641SAndroid Build Coastguard Worker // TODO(brettw) these should be removed and callers converted to the overloaded
62*635a8641SAndroid Build Coastguard Worker // "NumberToString" variant.
IntToString(int value)63*635a8641SAndroid Build Coastguard Worker inline std::string IntToString(int value) {
64*635a8641SAndroid Build Coastguard Worker   return NumberToString(value);
65*635a8641SAndroid Build Coastguard Worker }
IntToString16(int value)66*635a8641SAndroid Build Coastguard Worker inline string16 IntToString16(int value) {
67*635a8641SAndroid Build Coastguard Worker   return NumberToString16(value);
68*635a8641SAndroid Build Coastguard Worker }
UintToString(unsigned value)69*635a8641SAndroid Build Coastguard Worker inline std::string UintToString(unsigned value) {
70*635a8641SAndroid Build Coastguard Worker   return NumberToString(value);
71*635a8641SAndroid Build Coastguard Worker }
UintToString16(unsigned value)72*635a8641SAndroid Build Coastguard Worker inline string16 UintToString16(unsigned value) {
73*635a8641SAndroid Build Coastguard Worker   return NumberToString16(value);
74*635a8641SAndroid Build Coastguard Worker }
Int64ToString(int64_t value)75*635a8641SAndroid Build Coastguard Worker inline std::string Int64ToString(int64_t value) {
76*635a8641SAndroid Build Coastguard Worker   return NumberToString(value);
77*635a8641SAndroid Build Coastguard Worker }
Int64ToString16(int64_t value)78*635a8641SAndroid Build Coastguard Worker inline string16 Int64ToString16(int64_t value) {
79*635a8641SAndroid Build Coastguard Worker   return NumberToString16(value);
80*635a8641SAndroid Build Coastguard Worker }
81*635a8641SAndroid Build Coastguard Worker 
82*635a8641SAndroid Build Coastguard Worker // String -> number conversions ------------------------------------------------
83*635a8641SAndroid Build Coastguard Worker 
84*635a8641SAndroid Build Coastguard Worker // Perform a best-effort conversion of the input string to a numeric type,
85*635a8641SAndroid Build Coastguard Worker // setting |*output| to the result of the conversion.  Returns true for
86*635a8641SAndroid Build Coastguard Worker // "perfect" conversions; returns false in the following cases:
87*635a8641SAndroid Build Coastguard Worker //  - Overflow. |*output| will be set to the maximum value supported
88*635a8641SAndroid Build Coastguard Worker //    by the data type.
89*635a8641SAndroid Build Coastguard Worker //  - Underflow. |*output| will be set to the minimum value supported
90*635a8641SAndroid Build Coastguard Worker //    by the data type.
91*635a8641SAndroid Build Coastguard Worker //  - Trailing characters in the string after parsing the number.  |*output|
92*635a8641SAndroid Build Coastguard Worker //    will be set to the value of the number that was parsed.
93*635a8641SAndroid Build Coastguard Worker //  - Leading whitespace in the string before parsing the number. |*output| will
94*635a8641SAndroid Build Coastguard Worker //    be set to the value of the number that was parsed.
95*635a8641SAndroid Build Coastguard Worker //  - No characters parseable as a number at the beginning of the string.
96*635a8641SAndroid Build Coastguard Worker //    |*output| will be set to 0.
97*635a8641SAndroid Build Coastguard Worker //  - Empty string.  |*output| will be set to 0.
98*635a8641SAndroid Build Coastguard Worker // WARNING: Will write to |output| even when returning false.
99*635a8641SAndroid Build Coastguard Worker //          Read the comments above carefully.
100*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToInt(StringPiece input, int* output);
101*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToInt(StringPiece16 input, int* output);
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToUint(StringPiece input, unsigned* output);
104*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToUint(StringPiece16 input, unsigned* output);
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToInt64(StringPiece input, int64_t* output);
107*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToInt64(StringPiece16 input, int64_t* output);
108*635a8641SAndroid Build Coastguard Worker 
109*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToUint64(StringPiece input, uint64_t* output);
110*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToUint64(StringPiece16 input, uint64_t* output);
111*635a8641SAndroid Build Coastguard Worker 
112*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToSizeT(StringPiece input, size_t* output);
113*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToSizeT(StringPiece16 input, size_t* output);
114*635a8641SAndroid Build Coastguard Worker 
115*635a8641SAndroid Build Coastguard Worker // For floating-point conversions, only conversions of input strings in decimal
116*635a8641SAndroid Build Coastguard Worker // form are defined to work.  Behavior with strings representing floating-point
117*635a8641SAndroid Build Coastguard Worker // numbers in hexadecimal, and strings representing non-finite values (such as
118*635a8641SAndroid Build Coastguard Worker // NaN and inf) is undefined.  Otherwise, these behave the same as the integral
119*635a8641SAndroid Build Coastguard Worker // variants.  This expects the input string to NOT be specific to the locale.
120*635a8641SAndroid Build Coastguard Worker // If your input is locale specific, use ICU to read the number.
121*635a8641SAndroid Build Coastguard Worker // WARNING: Will write to |output| even when returning false.
122*635a8641SAndroid Build Coastguard Worker //          Read the comments here and above StringToInt() carefully.
123*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool StringToDouble(const std::string& input, double* output);
124*635a8641SAndroid Build Coastguard Worker 
125*635a8641SAndroid Build Coastguard Worker // Hex encoding ----------------------------------------------------------------
126*635a8641SAndroid Build Coastguard Worker 
127*635a8641SAndroid Build Coastguard Worker // Returns a hex string representation of a binary buffer. The returned hex
128*635a8641SAndroid Build Coastguard Worker // string will be in upper case. This function does not check if |size| is
129*635a8641SAndroid Build Coastguard Worker // within reasonable limits since it's written with trusted data in mind.  If
130*635a8641SAndroid Build Coastguard Worker // you suspect that the data you want to format might be large, the absolute
131*635a8641SAndroid Build Coastguard Worker // max size for |size| should be is
132*635a8641SAndroid Build Coastguard Worker //   std::numeric_limits<size_t>::max() / 2
133*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string HexEncode(const void* bytes, size_t size);
134*635a8641SAndroid Build Coastguard Worker 
135*635a8641SAndroid Build Coastguard Worker // Best effort conversion, see StringToInt above for restrictions.
136*635a8641SAndroid Build Coastguard Worker // Will only successful parse hex values that will fit into |output|, i.e.
137*635a8641SAndroid Build Coastguard Worker // -0x80000000 < |input| < 0x7FFFFFFF.
138*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool HexStringToInt(StringPiece input, int* output);
139*635a8641SAndroid Build Coastguard Worker 
140*635a8641SAndroid Build Coastguard Worker // Best effort conversion, see StringToInt above for restrictions.
141*635a8641SAndroid Build Coastguard Worker // Will only successful parse hex values that will fit into |output|, i.e.
142*635a8641SAndroid Build Coastguard Worker // 0x00000000 < |input| < 0xFFFFFFFF.
143*635a8641SAndroid Build Coastguard Worker // The string is not required to start with 0x.
144*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool HexStringToUInt(StringPiece input, uint32_t* output);
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker // Best effort conversion, see StringToInt above for restrictions.
147*635a8641SAndroid Build Coastguard Worker // Will only successful parse hex values that will fit into |output|, i.e.
148*635a8641SAndroid Build Coastguard Worker // -0x8000000000000000 < |input| < 0x7FFFFFFFFFFFFFFF.
149*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool HexStringToInt64(StringPiece input, int64_t* output);
150*635a8641SAndroid Build Coastguard Worker 
151*635a8641SAndroid Build Coastguard Worker // Best effort conversion, see StringToInt above for restrictions.
152*635a8641SAndroid Build Coastguard Worker // Will only successful parse hex values that will fit into |output|, i.e.
153*635a8641SAndroid Build Coastguard Worker // 0x0000000000000000 < |input| < 0xFFFFFFFFFFFFFFFF.
154*635a8641SAndroid Build Coastguard Worker // The string is not required to start with 0x.
155*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool HexStringToUInt64(StringPiece input, uint64_t* output);
156*635a8641SAndroid Build Coastguard Worker 
157*635a8641SAndroid Build Coastguard Worker // Similar to the previous functions, except that output is a vector of bytes.
158*635a8641SAndroid Build Coastguard Worker // |*output| will contain as many bytes as were successfully parsed prior to the
159*635a8641SAndroid Build Coastguard Worker // error.  There is no overflow, but input.size() must be evenly divisible by 2.
160*635a8641SAndroid Build Coastguard Worker // Leading 0x or +/- are not allowed.
161*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool HexStringToBytes(StringPiece input,
162*635a8641SAndroid Build Coastguard Worker                                   std::vector<uint8_t>* output);
163*635a8641SAndroid Build Coastguard Worker 
164*635a8641SAndroid Build Coastguard Worker }  // namespace base
165*635a8641SAndroid Build Coastguard Worker 
166*635a8641SAndroid Build Coastguard Worker #endif  // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_
167