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 <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
8 #include <string> | 11 #include <string> |
9 #include <vector> | 12 #include <vector> |
10 | 13 |
11 #include "base/base_export.h" | 14 #include "base/base_export.h" |
12 #include "base/basictypes.h" | |
13 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
14 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
15 | 17 |
16 // ---------------------------------------------------------------------------- | 18 // ---------------------------------------------------------------------------- |
17 // IMPORTANT MESSAGE FROM YOUR SPONSOR | 19 // IMPORTANT MESSAGE FROM YOUR SPONSOR |
18 // | 20 // |
19 // This file contains no "wstring" variants. New code should use string16. If | 21 // This file contains no "wstring" variants. New code should use string16. If |
20 // you need to make old code work, use the UTF8 version and convert. Please do | 22 // you need to make old code work, use the UTF8 version and convert. Please do |
21 // not add wstring variants. | 23 // not add wstring variants. |
22 // | 24 // |
23 // Please do not add "convenience" functions for converting strings to integers | 25 // Please do not add "convenience" functions for converting strings to integers |
24 // that return the value and ignore success/failure. That encourages people to | 26 // that return the value and ignore success/failure. That encourages people to |
25 // write code that doesn't properly handle the error conditions. | 27 // write code that doesn't properly handle the error conditions. |
26 // ---------------------------------------------------------------------------- | 28 // ---------------------------------------------------------------------------- |
27 | 29 |
28 namespace base { | 30 namespace base { |
29 | 31 |
30 // Number -> string conversions ------------------------------------------------ | 32 // Number -> string conversions ------------------------------------------------ |
31 | 33 |
32 BASE_EXPORT std::string IntToString(int value); | 34 BASE_EXPORT std::string IntToString(int value); |
33 BASE_EXPORT string16 IntToString16(int value); | 35 BASE_EXPORT string16 IntToString16(int value); |
34 | 36 |
35 BASE_EXPORT std::string UintToString(unsigned value); | 37 BASE_EXPORT std::string UintToString(unsigned value); |
36 BASE_EXPORT string16 UintToString16(unsigned value); | 38 BASE_EXPORT string16 UintToString16(unsigned value); |
37 | 39 |
38 BASE_EXPORT std::string Int64ToString(int64 value); | 40 BASE_EXPORT std::string Int64ToString(int64_t value); |
39 BASE_EXPORT string16 Int64ToString16(int64 value); | 41 BASE_EXPORT string16 Int64ToString16(int64_t value); |
40 | 42 |
41 BASE_EXPORT std::string Uint64ToString(uint64 value); | 43 BASE_EXPORT std::string Uint64ToString(uint64_t value); |
42 BASE_EXPORT string16 Uint64ToString16(uint64 value); | 44 BASE_EXPORT string16 Uint64ToString16(uint64_t value); |
43 | 45 |
44 BASE_EXPORT std::string SizeTToString(size_t value); | 46 BASE_EXPORT std::string SizeTToString(size_t value); |
45 BASE_EXPORT string16 SizeTToString16(size_t value); | 47 BASE_EXPORT string16 SizeTToString16(size_t value); |
46 | 48 |
47 // DoubleToString converts the double to a string format that ignores the | 49 // DoubleToString converts the double to a string format that ignores the |
48 // locale. If you want to use locale specific formatting, use ICU. | 50 // locale. If you want to use locale specific formatting, use ICU. |
49 BASE_EXPORT std::string DoubleToString(double value); | 51 BASE_EXPORT std::string DoubleToString(double value); |
50 | 52 |
51 // String -> number conversions ------------------------------------------------ | 53 // String -> number conversions ------------------------------------------------ |
52 | 54 |
(...skipping 12 matching lines...) Expand all Loading... |
65 // |*output| will be set to 0. | 67 // |*output| will be set to 0. |
66 // - Empty string. |*output| will be set to 0. | 68 // - Empty string. |*output| will be set to 0. |
67 // WARNING: Will write to |output| even when returning false. | 69 // WARNING: Will write to |output| even when returning false. |
68 // Read the comments above carefully. | 70 // Read the comments above carefully. |
69 BASE_EXPORT bool StringToInt(const StringPiece& input, int* output); | 71 BASE_EXPORT bool StringToInt(const StringPiece& input, int* output); |
70 BASE_EXPORT bool StringToInt(const StringPiece16& input, int* output); | 72 BASE_EXPORT bool StringToInt(const StringPiece16& input, int* output); |
71 | 73 |
72 BASE_EXPORT bool StringToUint(const StringPiece& input, unsigned* output); | 74 BASE_EXPORT bool StringToUint(const StringPiece& input, unsigned* output); |
73 BASE_EXPORT bool StringToUint(const StringPiece16& input, unsigned* output); | 75 BASE_EXPORT bool StringToUint(const StringPiece16& input, unsigned* output); |
74 | 76 |
75 BASE_EXPORT bool StringToInt64(const StringPiece& input, int64* output); | 77 BASE_EXPORT bool StringToInt64(const StringPiece& input, int64_t* output); |
76 BASE_EXPORT bool StringToInt64(const StringPiece16& input, int64* output); | 78 BASE_EXPORT bool StringToInt64(const StringPiece16& input, int64_t* output); |
77 | 79 |
78 BASE_EXPORT bool StringToUint64(const StringPiece& input, uint64* output); | 80 BASE_EXPORT bool StringToUint64(const StringPiece& input, uint64_t* output); |
79 BASE_EXPORT bool StringToUint64(const StringPiece16& input, uint64* output); | 81 BASE_EXPORT bool StringToUint64(const StringPiece16& input, uint64_t* output); |
80 | 82 |
81 BASE_EXPORT bool StringToSizeT(const StringPiece& input, size_t* output); | 83 BASE_EXPORT bool StringToSizeT(const StringPiece& input, size_t* output); |
82 BASE_EXPORT bool StringToSizeT(const StringPiece16& input, size_t* output); | 84 BASE_EXPORT bool StringToSizeT(const StringPiece16& input, size_t* 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-finite values (such as | 88 // numbers in hexadecimal, and strings representing non-finite 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. |
(...skipping 13 matching lines...) Expand all Loading... |
103 | 105 |
104 // Best effort conversion, see StringToInt above for restrictions. | 106 // Best effort conversion, see StringToInt above for restrictions. |
105 // Will only successful parse hex values that will fit into |output|, i.e. | 107 // Will only successful parse hex values that will fit into |output|, i.e. |
106 // -0x80000000 < |input| < 0x7FFFFFFF. | 108 // -0x80000000 < |input| < 0x7FFFFFFF. |
107 BASE_EXPORT bool HexStringToInt(const StringPiece& input, int* output); | 109 BASE_EXPORT bool HexStringToInt(const StringPiece& input, int* output); |
108 | 110 |
109 // Best effort conversion, see StringToInt above for restrictions. | 111 // Best effort conversion, see StringToInt above for restrictions. |
110 // Will only successful parse hex values that will fit into |output|, i.e. | 112 // Will only successful parse hex values that will fit into |output|, i.e. |
111 // 0x00000000 < |input| < 0xFFFFFFFF. | 113 // 0x00000000 < |input| < 0xFFFFFFFF. |
112 // The string is not required to start with 0x. | 114 // The string is not required to start with 0x. |
113 BASE_EXPORT bool HexStringToUInt(const StringPiece& input, uint32* output); | 115 BASE_EXPORT bool HexStringToUInt(const StringPiece& input, uint32_t* output); |
114 | 116 |
115 // Best effort conversion, see StringToInt above for restrictions. | 117 // Best effort conversion, see StringToInt above for restrictions. |
116 // Will only successful parse hex values that will fit into |output|, i.e. | 118 // Will only successful parse hex values that will fit into |output|, i.e. |
117 // -0x8000000000000000 < |input| < 0x7FFFFFFFFFFFFFFF. | 119 // -0x8000000000000000 < |input| < 0x7FFFFFFFFFFFFFFF. |
118 BASE_EXPORT bool HexStringToInt64(const StringPiece& input, int64* output); | 120 BASE_EXPORT bool HexStringToInt64(const StringPiece& input, int64_t* output); |
119 | 121 |
120 // Best effort conversion, see StringToInt above for restrictions. | 122 // Best effort conversion, see StringToInt above for restrictions. |
121 // Will only successful parse hex values that will fit into |output|, i.e. | 123 // Will only successful parse hex values that will fit into |output|, i.e. |
122 // 0x0000000000000000 < |input| < 0xFFFFFFFFFFFFFFFF. | 124 // 0x0000000000000000 < |input| < 0xFFFFFFFFFFFFFFFF. |
123 // The string is not required to start with 0x. | 125 // The string is not required to start with 0x. |
124 BASE_EXPORT bool HexStringToUInt64(const StringPiece& input, uint64* output); | 126 BASE_EXPORT bool HexStringToUInt64(const StringPiece& input, uint64_t* output); |
125 | 127 |
126 // Similar to the previous functions, except that output is a vector of bytes. | 128 // Similar to the previous functions, except that output is a vector of bytes. |
127 // |*output| will contain as many bytes as were successfully parsed prior to the | 129 // |*output| will contain as many bytes as were successfully parsed prior to the |
128 // error. There is no overflow, but input.size() must be evenly divisible by 2. | 130 // error. There is no overflow, but input.size() must be evenly divisible by 2. |
129 // Leading 0x or +/- are not allowed. | 131 // Leading 0x or +/- are not allowed. |
130 BASE_EXPORT bool HexStringToBytes(const std::string& input, | 132 BASE_EXPORT bool HexStringToBytes(const std::string& input, |
131 std::vector<uint8>* output); | 133 std::vector<uint8_t>* output); |
132 | 134 |
133 } // namespace base | 135 } // namespace base |
134 | 136 |
135 #endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ | 137 #endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ |
OLD | NEW |