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 #ifndef BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ | 5 #ifndef BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ |
6 #define BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ | 6 #define BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // by the data type. | 57 // by the data type. |
58 // - Underflow. |*output| will be set to the minimum value supported | 58 // - Underflow. |*output| will be set to the minimum value supported |
59 // by the data type. | 59 // by the data type. |
60 // - Trailing characters in the string after parsing the number. |*output| | 60 // - Trailing characters in the string after parsing the number. |*output| |
61 // will be set to the value of the number that was parsed. | 61 // will be set to the value of the number that was parsed. |
62 // - Leading whitespace in the string before parsing the number. |*output| will | 62 // - Leading whitespace in the string before parsing the number. |*output| will |
63 // be set to the value of the number that was parsed. | 63 // be set to the value of the number that was parsed. |
64 // - No characters parseable as a number at the beginning of the string. | 64 // - No characters parseable as a number at the beginning of the string. |
65 // |*output| will be set to 0. | 65 // |*output| will be set to 0. |
66 // - Empty string. |*output| will be set to 0. | 66 // - Empty string. |*output| will be set to 0. |
| 67 // WARNING: Will write to |output| even when returning false. |
| 68 // Read the comments above carefully. |
67 BASE_EXPORT bool StringToInt(const StringPiece& input, int* output); | 69 BASE_EXPORT bool StringToInt(const StringPiece& input, int* output); |
68 BASE_EXPORT bool StringToInt(const StringPiece16& input, int* output); | 70 BASE_EXPORT bool StringToInt(const StringPiece16& input, int* output); |
69 | 71 |
70 BASE_EXPORT bool StringToUint(const StringPiece& input, unsigned* output); | 72 BASE_EXPORT bool StringToUint(const StringPiece& input, unsigned* output); |
71 BASE_EXPORT bool StringToUint(const StringPiece16& input, unsigned* output); | 73 BASE_EXPORT bool StringToUint(const StringPiece16& input, unsigned* output); |
72 | 74 |
73 BASE_EXPORT bool StringToInt64(const StringPiece& input, int64* output); | 75 BASE_EXPORT bool StringToInt64(const StringPiece& input, int64* output); |
74 BASE_EXPORT bool StringToInt64(const StringPiece16& input, int64* output); | 76 BASE_EXPORT bool StringToInt64(const StringPiece16& input, int64* output); |
75 | 77 |
76 BASE_EXPORT bool StringToUint64(const StringPiece& input, uint64* output); | 78 BASE_EXPORT bool StringToUint64(const StringPiece& input, uint64* output); |
77 BASE_EXPORT bool StringToUint64(const StringPiece16& input, uint64* output); | 79 BASE_EXPORT bool StringToUint64(const StringPiece16& input, uint64* output); |
78 | 80 |
79 BASE_EXPORT bool StringToSizeT(const StringPiece& input, size_t* output); | 81 BASE_EXPORT bool StringToSizeT(const StringPiece& input, size_t* output); |
80 BASE_EXPORT bool StringToSizeT(const StringPiece16& input, size_t* output); | 82 BASE_EXPORT bool StringToSizeT(const StringPiece16& input, size_t* output); |
81 | 83 |
82 // For floating-point conversions, only conversions of input strings in decimal | 84 // For floating-point conversions, only conversions of input strings in decimal |
83 // form are defined to work. Behavior with strings representing floating-point | 85 // form are defined to work. Behavior with strings representing floating-point |
84 // numbers in hexadecimal, and strings representing non-fininte values (such as | 86 // numbers in hexadecimal, and strings representing non-finite values (such as |
85 // NaN and inf) is undefined. Otherwise, these behave the same as the integral | 87 // NaN and inf) is undefined. Otherwise, these behave the same as the integral |
86 // variants. This expects the input string to NOT be specific to the locale. | 88 // variants. This expects the input string to NOT be specific to the locale. |
87 // If your input is locale specific, use ICU to read the number. | 89 // If your input is locale specific, use ICU to read the number. |
| 90 // WARNING: Will write to |output| even when returning false. |
| 91 // Read the comments here and above StringToInt() carefully. |
88 BASE_EXPORT bool StringToDouble(const std::string& input, double* output); | 92 BASE_EXPORT bool StringToDouble(const std::string& input, double* output); |
89 | 93 |
90 // Hex encoding ---------------------------------------------------------------- | 94 // Hex encoding ---------------------------------------------------------------- |
91 | 95 |
92 // Returns a hex string representation of a binary buffer. The returned hex | 96 // Returns a hex string representation of a binary buffer. The returned hex |
93 // string will be in upper case. This function does not check if |size| is | 97 // string will be in upper case. This function does not check if |size| is |
94 // within reasonable limits since it's written with trusted data in mind. If | 98 // within reasonable limits since it's written with trusted data in mind. If |
95 // you suspect that the data you want to format might be large, the absolute | 99 // you suspect that the data you want to format might be large, the absolute |
96 // max size for |size| should be is | 100 // max size for |size| should be is |
97 // std::numeric_limits<size_t>::max() / 2 | 101 // std::numeric_limits<size_t>::max() / 2 |
(...skipping 24 matching lines...) Expand all Loading... |
122 // Similar to the previous functions, except that output is a vector of bytes. | 126 // Similar to the previous functions, except that output is a vector of bytes. |
123 // |*output| will contain as many bytes as were successfully parsed prior to the | 127 // |*output| will contain as many bytes as were successfully parsed prior to the |
124 // error. There is no overflow, but input.size() must be evenly divisible by 2. | 128 // error. There is no overflow, but input.size() must be evenly divisible by 2. |
125 // Leading 0x or +/- are not allowed. | 129 // Leading 0x or +/- are not allowed. |
126 BASE_EXPORT bool HexStringToBytes(const std::string& input, | 130 BASE_EXPORT bool HexStringToBytes(const std::string& input, |
127 std::vector<uint8>* output); | 131 std::vector<uint8>* output); |
128 | 132 |
129 } // namespace base | 133 } // namespace base |
130 | 134 |
131 #endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ | 135 #endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ |
OLD | NEW |