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