Debian Codegraph @

mfbt/double-conversion/double-conversion/string-to-double.h

raw
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
29 #define DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
30
31 #include "mozilla/Types.h"
32 #include "utils.h"
33
34 namespace double_conversion {
35
36 class StringToDoubleConverter {
37 public:
38 // Enumeration for allowing octals and ignoring junk when converting
39 // strings to numbers.
40 enum Flags {
41 NO_FLAGS = 0,
42 ALLOW_HEX = 1,
43 ALLOW_OCTALS = 2,
44 ALLOW_TRAILING_JUNK = 4,
45 ALLOW_LEADING_SPACES = 8,
46 ALLOW_TRAILING_SPACES = 16,
47 ALLOW_SPACES_AFTER_SIGN = 32,
48 ALLOW_CASE_INSENSITIVITY = 64,
49 ALLOW_CASE_INSENSIBILITY = 64, // Deprecated
50 ALLOW_HEX_FLOATS = 128,
51 };
52
53 static const uc16 kNoSeparator = '\0';
54
55 // Flags should be a bit-or combination of the possible Flags-enum.
56 // - NO_FLAGS: no special flags.
57 // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
58 // Ex: StringToDouble("0x1234") -> 4660.0
59 // In StringToDouble("0x1234.56") the characters ".56" are trailing
60 // junk. The result of the call is hence dependent on
61 // the ALLOW_TRAILING_JUNK flag and/or the junk value.
62 // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
63 // the string will not be parsed as "0" followed by junk.
64 //
65 // - ALLOW_OCTALS: recognizes the prefix "0" for octals:
66 // If a sequence of octal digits starts with '0', then the number is
67 // read as octal integer. Octal numbers may only be integers.
68 // Ex: StringToDouble("01234") -> 668.0
69 // StringToDouble("012349") -> 12349.0 // Not a sequence of octal
70 // // digits.
71 // In StringToDouble("01234.56") the characters ".56" are trailing
72 // junk. The result of the call is hence dependent on
73 // the ALLOW_TRAILING_JUNK flag and/or the junk value.
74 // In StringToDouble("01234e56") the characters "e56" are trailing
75 // junk, too.
76 // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
77 // a double literal.
78 // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
79 // new-lines, and tabs.
80 // - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
81 // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
82 // Ex: StringToDouble("- 123.2") -> -123.2.
83 // StringToDouble("+ 123.2") -> 123.2
84 // - ALLOW_CASE_INSENSITIVITY: ignore case of characters for special values:
85 // infinity and nan.
86 // - ALLOW_HEX_FLOATS: allows hexadecimal float literals.
87 // This *must* start with "0x" and separate the exponent with "p".
88 // Examples: 0x1.2p3 == 9.0
89 // 0x10.1p0 == 16.0625
90 // ALLOW_HEX and ALLOW_HEX_FLOATS are indented.
91 //
92 // empty_string_value is returned when an empty string is given as input.
93 // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
94 // containing only spaces is converted to the 'empty_string_value', too.
95 //
96 // junk_string_value is returned when
97 // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
98 // part of a double-literal) is found.
99 // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
100 // double literal.
101 //
102 // infinity_symbol and nan_symbol are strings that are used to detect
103 // inputs that represent infinity and NaN. They can be null, in which case
104 // they are ignored.
105 // The conversion routine first reads any possible signs. Then it compares the
106 // following character of the input-string with the first character of
107 // the infinity, and nan-symbol. If either matches, the function assumes, that
108 // a match has been found, and expects the following input characters to match
109 // the remaining characters of the special-value symbol.
110 // This means that the following restrictions apply to special-value symbols:
111 // - they must not start with signs ('+', or '-'),
112 // - they must not have the same first character.
113 // - they must not start with digits.
114 //
115 // If the separator character is not kNoSeparator, then that specific
116 // character is ignored when in between two valid digits of the significant.
117 // It is not allowed to appear in the exponent.
118 // It is not allowed to lead or trail the number.
119 // It is not allowed to appear twice next to each other.
120 //
121 // Examples:
122 // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
123 // empty_string_value = 0.0,
124 // junk_string_value = NaN,
125 // infinity_symbol = "infinity",
126 // nan_symbol = "nan":
127 // StringToDouble("0x1234") -> 4660.0.
128 // StringToDouble("0x1234K") -> 4660.0.
129 // StringToDouble("") -> 0.0 // empty_string_value.
130 // StringToDouble(" ") -> NaN // junk_string_value.
131 // StringToDouble(" 1") -> NaN // junk_string_value.
132 // StringToDouble("0x") -> NaN // junk_string_value.
133 // StringToDouble("-123.45") -> -123.45.
134 // StringToDouble("--123.45") -> NaN // junk_string_value.
135 // StringToDouble("123e45") -> 123e45.
136 // StringToDouble("123E45") -> 123e45.
137 // StringToDouble("123e+45") -> 123e45.
138 // StringToDouble("123E-45") -> 123e-45.
139 // StringToDouble("123e") -> 123.0 // trailing junk ignored.
140 // StringToDouble("123e-") -> 123.0 // trailing junk ignored.
141 // StringToDouble("+NaN") -> NaN // NaN string literal.
142 // StringToDouble("-infinity") -> -inf. // infinity literal.
143 // StringToDouble("Infinity") -> NaN // junk_string_value.
144 //
145 // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
146 // empty_string_value = 0.0,
147 // junk_string_value = NaN,
148 // infinity_symbol = NULL,
149 // nan_symbol = NULL:
150 // StringToDouble("0x1234") -> NaN // junk_string_value.
151 // StringToDouble("01234") -> 668.0.
152 // StringToDouble("") -> 0.0 // empty_string_value.
153 // StringToDouble(" ") -> 0.0 // empty_string_value.
154 // StringToDouble(" 1") -> 1.0
155 // StringToDouble("0x") -> NaN // junk_string_value.
156 // StringToDouble("0123e45") -> NaN // junk_string_value.
157 // StringToDouble("01239E45") -> 1239e45.
158 // StringToDouble("-infinity") -> NaN // junk_string_value.
159 // StringToDouble("NaN") -> NaN // junk_string_value.
160 //
161 // flags = NO_FLAGS,
162 // separator = ' ':
163 // StringToDouble("1 2 3 4") -> 1234.0
164 // StringToDouble("1 2") -> NaN // junk_string_value
165 // StringToDouble("1 000 000.0") -> 1000000.0
166 // StringToDouble("1.000 000") -> 1.0
167 // StringToDouble("1.0e1 000") -> NaN // junk_string_value
168 StringToDoubleConverter(int flags,
169 double empty_string_value,
170 double junk_string_value,
171 const char* infinity_symbol,
172 const char* nan_symbol,
173 uc16 separator = kNoSeparator)
174 : flags_(flags),
175 empty_string_value_(empty_string_value),
176 junk_string_value_(junk_string_value),
177 infinity_symbol_(infinity_symbol),
178 nan_symbol_(nan_symbol),
179 separator_(separator) {
180 }
181
182 // Performs the conversion.
183 // The output parameter 'processed_characters_count' is set to the number
184 // of characters that have been processed to read the number.
185 // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
186 // in the 'processed_characters_count'. Trailing junk is never included.
187 MFBT_API double StringToDouble(const char* buffer,
188 int length,
189 int* processed_characters_count) const;
190
191 // Same as StringToDouble above but for 16 bit characters.
192 MFBT_API double StringToDouble(const uc16* buffer,
193 int length,
194 int* processed_characters_count) const;
195
196 // Same as StringToDouble but reads a float.
197 // Note that this is not equivalent to static_cast<float>(StringToDouble(...))
198 // due to potential double-rounding.
199 MFBT_API float StringToFloat(const char* buffer,
200 int length,
201 int* processed_characters_count) const;
202
203 // Same as StringToFloat above but for 16 bit characters.
204 MFBT_API float StringToFloat(const uc16* buffer,
205 int length,
206 int* processed_characters_count) const;
207
208 // Same as StringToDouble for T = double, and StringToFloat for T = float.
209 template <typename T>
210 T StringTo(const char* buffer,
211 int length,
212 int* processed_characters_count) const;
213
214 // Same as StringTo above but for 16 bit characters.
215 template <typename T>
216 T StringTo(const uc16* buffer,
217 int length,
218 int* processed_characters_count) const;
219
220 private:
221 const int flags_;
222 const double empty_string_value_;
223 const double junk_string_value_;
224 const char* const infinity_symbol_;
225 const char* const nan_symbol_;
226 const uc16 separator_;
227
228 template <class Iterator>
229 double StringToIeee(Iterator start_pointer,
230 int length,
231 bool read_as_double,
232 int* processed_characters_count) const;
233
234 DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
235 };
236
237 } // namespace double_conversion
238
239 #endif // DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_