OLD | NEW |
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 #ifndef BASE_STRING_NUMBER_CONVERSIONS_H_ | 5 #ifndef BASE_STRING_NUMBER_CONVERSIONS_H_ |
6 #define BASE_STRING_NUMBER_CONVERSIONS_H_ | 6 #define BASE_STRING_NUMBER_CONVERSIONS_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base_api.h" | 11 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
14 | 14 |
15 // ---------------------------------------------------------------------------- | 15 // ---------------------------------------------------------------------------- |
16 // IMPORTANT MESSAGE FROM YOUR SPONSOR | 16 // IMPORTANT MESSAGE FROM YOUR SPONSOR |
17 // | 17 // |
18 // This file contains no "wstring" variants. New code should use string16. If | 18 // This file contains no "wstring" variants. New code should use string16. If |
19 // you need to make old code work, use the UTF8 version and convert. Please do | 19 // you need to make old code work, use the UTF8 version and convert. Please do |
20 // not add wstring variants. | 20 // not add wstring variants. |
21 // | 21 // |
22 // Please do not add "convenience" functions for converting strings to integers | 22 // Please do not add "convenience" functions for converting strings to integers |
23 // that return the value and ignore success/failure. That encourages people to | 23 // that return the value and ignore success/failure. That encourages people to |
24 // write code that doesn't properly handle the error conditions. | 24 // write code that doesn't properly handle the error conditions. |
25 // ---------------------------------------------------------------------------- | 25 // ---------------------------------------------------------------------------- |
26 | 26 |
27 namespace base { | 27 namespace base { |
28 | 28 |
29 // Number -> string conversions ------------------------------------------------ | 29 // Number -> string conversions ------------------------------------------------ |
30 | 30 |
31 BASE_API std::string IntToString(int value); | 31 BASE_EXPORT std::string IntToString(int value); |
32 BASE_API string16 IntToString16(int value); | 32 BASE_EXPORT string16 IntToString16(int value); |
33 | 33 |
34 BASE_API std::string UintToString(unsigned value); | 34 BASE_EXPORT std::string UintToString(unsigned value); |
35 BASE_API string16 UintToString16(unsigned value); | 35 BASE_EXPORT string16 UintToString16(unsigned value); |
36 | 36 |
37 BASE_API std::string Int64ToString(int64 value); | 37 BASE_EXPORT std::string Int64ToString(int64 value); |
38 BASE_API string16 Int64ToString16(int64 value); | 38 BASE_EXPORT string16 Int64ToString16(int64 value); |
39 | 39 |
40 BASE_API std::string Uint64ToString(uint64 value); | 40 BASE_EXPORT std::string Uint64ToString(uint64 value); |
41 BASE_API string16 Uint64ToString16(uint64 value); | 41 BASE_EXPORT string16 Uint64ToString16(uint64 value); |
42 | 42 |
43 // DoubleToString converts the double to a string format that ignores the | 43 // DoubleToString converts the double to a string format that ignores the |
44 // locale. If you want to use locale specific formatting, use ICU. | 44 // locale. If you want to use locale specific formatting, use ICU. |
45 BASE_API std::string DoubleToString(double value); | 45 BASE_EXPORT std::string DoubleToString(double value); |
46 | 46 |
47 // String -> number conversions ------------------------------------------------ | 47 // String -> number conversions ------------------------------------------------ |
48 | 48 |
49 // Perform a best-effort conversion of the input string to a numeric type, | 49 // Perform a best-effort conversion of the input string to a numeric type, |
50 // setting |*output| to the result of the conversion. Returns true for | 50 // setting |*output| to the result of the conversion. Returns true for |
51 // "perfect" conversions; returns false in the following cases: | 51 // "perfect" conversions; returns false in the following cases: |
52 // - Overflow/underflow. |*output| will be set to the maximum value supported | 52 // - Overflow/underflow. |*output| will be set to the maximum value supported |
53 // by the data type. | 53 // by the data type. |
54 // - Trailing characters in the string after parsing the number. |*output| | 54 // - Trailing characters in the string after parsing the number. |*output| |
55 // will be set to the value of the number that was parsed. | 55 // will be set to the value of the number that was parsed. |
56 // - Leading whitespace in the string before parsing the number. |*output| will | 56 // - Leading whitespace in the string before parsing the number. |*output| will |
57 // be set to the value of the number that was parsed. | 57 // be set to the value of the number that was parsed. |
58 // - No characters parseable as a number at the beginning of the string. | 58 // - No characters parseable as a number at the beginning of the string. |
59 // |*output| will be set to 0. | 59 // |*output| will be set to 0. |
60 // - Empty string. |*output| will be set to 0. | 60 // - Empty string. |*output| will be set to 0. |
61 BASE_API bool StringToInt(const std::string& input, int* output); | 61 BASE_EXPORT bool StringToInt(const std::string& input, int* output); |
62 BASE_API bool StringToInt(std::string::const_iterator begin, | 62 BASE_EXPORT bool StringToInt(std::string::const_iterator begin, |
63 std::string::const_iterator end, | 63 std::string::const_iterator end, |
64 int* output); | 64 int* output); |
65 BASE_API bool StringToInt(const char* begin, const char* end, int* output); | 65 BASE_EXPORT bool StringToInt(const char* begin, const char* end, int* output); |
66 | 66 |
67 BASE_API bool StringToInt(const string16& input, int* output); | 67 BASE_EXPORT bool StringToInt(const string16& input, int* output); |
68 BASE_API bool StringToInt(string16::const_iterator begin, | 68 BASE_EXPORT bool StringToInt(string16::const_iterator begin, |
69 string16::const_iterator end, | 69 string16::const_iterator end, |
70 int* output); | 70 int* output); |
71 BASE_API bool StringToInt(const char16* begin, const char16* end, int* output); | 71 BASE_EXPORT bool StringToInt(const char16* begin, const char16* end, |
| 72 int* output); |
72 | 73 |
73 BASE_API bool StringToInt64(const std::string& input, int64* output); | 74 BASE_EXPORT bool StringToInt64(const std::string& input, int64* output); |
74 BASE_API bool StringToInt64(std::string::const_iterator begin, | 75 BASE_EXPORT bool StringToInt64(std::string::const_iterator begin, |
75 std::string::const_iterator end, | 76 std::string::const_iterator end, |
76 int64* output); | 77 int64* output); |
77 BASE_API bool StringToInt64(const char* begin, const char* end, int64* output); | 78 BASE_EXPORT bool StringToInt64(const char* begin, const char* end, |
| 79 int64* output); |
78 | 80 |
79 BASE_API bool StringToInt64(const string16& input, int64* output); | 81 BASE_EXPORT bool StringToInt64(const string16& input, int64* output); |
80 BASE_API bool StringToInt64(string16::const_iterator begin, | 82 BASE_EXPORT bool StringToInt64(string16::const_iterator begin, |
81 string16::const_iterator end, | 83 string16::const_iterator end, |
82 int64* output); | 84 int64* output); |
83 BASE_API bool StringToInt64(const char16* begin, const char16* end, | 85 BASE_EXPORT bool StringToInt64(const char16* begin, const char16* end, |
84 int64* output); | 86 int64* output); |
85 | 87 |
86 // For floating-point conversions, only conversions of input strings in decimal | 88 // For floating-point conversions, only conversions of input strings in decimal |
87 // form are defined to work. Behavior with strings representing floating-point | 89 // form are defined to work. Behavior with strings representing floating-point |
88 // numbers in hexadecimal, and strings representing non-fininte values (such as | 90 // numbers in hexadecimal, and strings representing non-fininte values (such as |
89 // NaN and inf) is undefined. Otherwise, these behave the same as the integral | 91 // NaN and inf) is undefined. Otherwise, these behave the same as the integral |
90 // variants. This expects the input string to NOT be specific to the locale. | 92 // variants. This expects the input string to NOT be specific to the locale. |
91 // If your input is locale specific, use ICU to read the number. | 93 // If your input is locale specific, use ICU to read the number. |
92 BASE_API bool StringToDouble(const std::string& input, double* output); | 94 BASE_EXPORT bool StringToDouble(const std::string& input, double* output); |
93 | 95 |
94 // Hex encoding ---------------------------------------------------------------- | 96 // Hex encoding ---------------------------------------------------------------- |
95 | 97 |
96 // Returns a hex string representation of a binary buffer. The returned hex | 98 // Returns a hex string representation of a binary buffer. The returned hex |
97 // string will be in upper case. This function does not check if |size| is | 99 // string will be in upper case. This function does not check if |size| is |
98 // within reasonable limits since it's written with trusted data in mind. If | 100 // within reasonable limits since it's written with trusted data in mind. If |
99 // you suspect that the data you want to format might be large, the absolute | 101 // you suspect that the data you want to format might be large, the absolute |
100 // max size for |size| should be is | 102 // max size for |size| should be is |
101 // std::numeric_limits<size_t>::max() / 2 | 103 // std::numeric_limits<size_t>::max() / 2 |
102 BASE_API std::string HexEncode(const void* bytes, size_t size); | 104 BASE_EXPORT std::string HexEncode(const void* bytes, size_t size); |
103 | 105 |
104 // Best effort conversion, see StringToInt above for restrictions. | 106 // Best effort conversion, see StringToInt above for restrictions. |
105 BASE_API bool HexStringToInt(const std::string& input, int* output); | 107 BASE_EXPORT bool HexStringToInt(const std::string& input, int* output); |
106 BASE_API bool HexStringToInt(std::string::const_iterator begin, | 108 BASE_EXPORT bool HexStringToInt(std::string::const_iterator begin, |
107 std::string::const_iterator end, | 109 std::string::const_iterator end, |
108 int* output); | 110 int* output); |
109 BASE_API bool HexStringToInt(const char* begin, const char* end, int* output); | 111 BASE_EXPORT bool HexStringToInt(const char* begin, const char* end, |
| 112 int* output); |
110 | 113 |
111 // Similar to the previous functions, except that output is a vector of bytes. | 114 // 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 | 115 // |*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. | 116 // error. There is no overflow, but input.size() must be evenly divisible by 2. |
114 // Leading 0x or +/- are not allowed. | 117 // Leading 0x or +/- are not allowed. |
115 BASE_API bool HexStringToBytes(const std::string& input, | 118 BASE_EXPORT bool HexStringToBytes(const std::string& input, |
116 std::vector<uint8>* output); | 119 std::vector<uint8>* output); |
117 | 120 |
118 } // namespace base | 121 } // namespace base |
119 | 122 |
120 #endif // BASE_STRING_NUMBER_CONVERSIONS_H_ | 123 #endif // BASE_STRING_NUMBER_CONVERSIONS_H_ |
121 | 124 |
OLD | NEW |