]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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_DOUBLE_CONVERSION_H_ | |
29 | #define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ | |
30 | ||
31 | #include "utils.h" | |
32 | ||
33 | namespace double_conversion { | |
34 | ||
35 | class DoubleToStringConverter { | |
36 | public: | |
37 | // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint | |
38 | // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the | |
39 | // function returns false. | |
40 | static const int kMaxFixedDigitsBeforePoint = 60; | |
41 | static const int kMaxFixedDigitsAfterPoint = 60; | |
42 | ||
43 | // When calling ToExponential with a requested_digits | |
44 | // parameter > kMaxExponentialDigits then the function returns false. | |
45 | static const int kMaxExponentialDigits = 120; | |
46 | ||
47 | // When calling ToPrecision with a requested_digits | |
48 | // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits | |
49 | // then the function returns false. | |
50 | static const int kMinPrecisionDigits = 1; | |
51 | static const int kMaxPrecisionDigits = 120; | |
52 | ||
53 | enum Flags { | |
54 | NO_FLAGS = 0, | |
55 | EMIT_POSITIVE_EXPONENT_SIGN = 1, | |
56 | EMIT_TRAILING_DECIMAL_POINT = 2, | |
57 | EMIT_TRAILING_ZERO_AFTER_POINT = 4, | |
58 | UNIQUE_ZERO = 8 | |
59 | }; | |
60 | ||
61 | // Flags should be a bit-or combination of the possible Flags-enum. | |
62 | // - NO_FLAGS: no special flags. | |
63 | // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent | |
64 | // form, emits a '+' for positive exponents. Example: 1.2e+2. | |
65 | // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is | |
66 | // converted into decimal format then a trailing decimal point is appended. | |
67 | // Example: 2345.0 is converted to "2345.". | |
68 | // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point | |
69 | // emits a trailing '0'-character. This flag requires the | |
70 | // EXMIT_TRAILING_DECIMAL_POINT flag. | |
71 | // Example: 2345.0 is converted to "2345.0". | |
72 | // - UNIQUE_ZERO: "-0.0" is converted to "0.0". | |
73 | // | |
74 | // Infinity symbol and nan_symbol provide the string representation for these | |
75 | // special values. If the string is NULL and the special value is encountered | |
76 | // then the conversion functions return false. | |
77 | // | |
78 | // The exponent_character is used in exponential representations. It is | |
79 | // usually 'e' or 'E'. | |
80 | // | |
81 | // When converting to the shortest representation the converter will | |
82 | // represent input numbers in decimal format if they are in the interval | |
83 | // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ | |
84 | // (lower boundary included, greater boundary excluded). | |
85 | // Example: with decimal_in_shortest_low = -6 and | |
86 | // decimal_in_shortest_high = 21: | |
87 | // ToShortest(0.000001) -> "0.000001" | |
88 | // ToShortest(0.0000001) -> "1e-7" | |
89 | // ToShortest(111111111111111111111.0) -> "111111111111111110000" | |
90 | // ToShortest(100000000000000000000.0) -> "100000000000000000000" | |
91 | // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" | |
92 | // | |
93 | // When converting to precision mode the converter may add | |
94 | // max_leading_padding_zeroes before returning the number in exponential | |
95 | // format. | |
96 | // Example with max_leading_padding_zeroes_in_precision_mode = 6. | |
97 | // ToPrecision(0.0000012345, 2) -> "0.0000012" | |
98 | // ToPrecision(0.00000012345, 2) -> "1.2e-7" | |
99 | // Similarily the converter may add up to | |
100 | // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid | |
101 | // returning an exponential representation. A zero added by the | |
102 | // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. | |
103 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: | |
104 | // ToPrecision(230.0, 2) -> "230" | |
105 | // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. | |
106 | // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. | |
107 | // | |
108 | // When converting numbers to scientific notation representation, if the mantissa of | |
109 | // the representation is an integer number, the EMIT_TRAILING_DECIMAL_POINT flag will | |
110 | // add a '.' character at the end of the representation: | |
111 | // - With EMIT_TRAILING_DECIMAL_POINT enabled -> 0.0009 => 9.E-4 | |
112 | // - With EMIT_TRAILING_DECIMAL_POINT disabled -> 0.0009 => 9E-4 | |
113 | // | |
114 | // If the mantissa is an integer and the EMIT_TRAILING_ZERO_AFTER_POINT flag is enabled | |
115 | // it will add a '0' character at the end of the mantissa representation. Note that that | |
116 | // flag depends on EMIT_TRAILING_DECIMAL_POINT flag be enabled. | |
117 | // - With EMIT_TRAILING_ZERO_AFTER_POINT enabled -> 0.0009 => 9.0E-4 | |
118 | DoubleToStringConverter(int flags, | |
119 | const char* infinity_symbol, | |
120 | const char* nan_symbol, | |
121 | char exponent_character, | |
122 | int decimal_in_shortest_low, | |
123 | int decimal_in_shortest_high, | |
124 | int max_leading_padding_zeroes_in_precision_mode, | |
125 | int max_trailing_padding_zeroes_in_precision_mode) | |
126 | : flags_(flags), | |
127 | infinity_symbol_(infinity_symbol), | |
128 | nan_symbol_(nan_symbol), | |
129 | exponent_character_(exponent_character), | |
130 | decimal_in_shortest_low_(decimal_in_shortest_low), | |
131 | decimal_in_shortest_high_(decimal_in_shortest_high), | |
132 | max_leading_padding_zeroes_in_precision_mode_( | |
133 | max_leading_padding_zeroes_in_precision_mode), | |
134 | max_trailing_padding_zeroes_in_precision_mode_( | |
135 | max_trailing_padding_zeroes_in_precision_mode) { | |
136 | // When 'trailing zero after the point' is set, then 'trailing point' | |
137 | // must be set too. | |
138 | ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || | |
139 | !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); | |
140 | } | |
141 | ||
142 | // Returns a converter following the EcmaScript specification. | |
143 | static const DoubleToStringConverter& EcmaScriptConverter(); | |
144 | ||
145 | // Computes the shortest string of digits that correctly represent the input | |
146 | // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high | |
147 | // (see constructor) it then either returns a decimal representation, or an | |
148 | // exponential representation. | |
149 | // Example with decimal_in_shortest_low = -6, | |
150 | // decimal_in_shortest_high = 21, | |
151 | // EMIT_POSITIVE_EXPONENT_SIGN activated, and | |
152 | // EMIT_TRAILING_DECIMAL_POINT deactived: | |
153 | // ToShortest(0.000001) -> "0.000001" | |
154 | // ToShortest(0.0000001) -> "1e-7" | |
155 | // ToShortest(111111111111111111111.0) -> "111111111111111110000" | |
156 | // ToShortest(100000000000000000000.0) -> "100000000000000000000" | |
157 | // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" | |
158 | // | |
159 | // Note: the conversion may round the output if the returned string | |
160 | // is accurate enough to uniquely identify the input-number. | |
161 | // For example the most precise representation of the double 9e59 equals | |
162 | // "899999999999999918767229449717619953810131273674690656206848", but | |
163 | // the converter will return the shorter (but still correct) "9e59". | |
164 | // | |
165 | // Returns true if the conversion succeeds. The conversion always succeeds | |
166 | // except when the input value is special and no infinity_symbol or | |
167 | // nan_symbol has been given to the constructor. | |
168 | bool ToShortest(double value, StringBuilder* result_builder) const { | |
169 | return ToShortestIeeeNumber(value, result_builder, SHORTEST); | |
170 | } | |
171 | ||
172 | // Same as ToShortest, but for single-precision floats. | |
173 | bool ToShortestSingle(float value, StringBuilder* result_builder) const { | |
174 | return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE); | |
175 | } | |
176 | ||
177 | ||
178 | // Computes a decimal representation with a fixed number of digits after the | |
179 | // decimal point. The last emitted digit is rounded. | |
180 | // | |
181 | // Examples: | |
182 | // ToFixed(3.12, 1) -> "3.1" | |
183 | // ToFixed(3.1415, 3) -> "3.142" | |
184 | // ToFixed(1234.56789, 4) -> "1234.5679" | |
185 | // ToFixed(1.23, 5) -> "1.23000" | |
186 | // ToFixed(0.1, 4) -> "0.1000" | |
187 | // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" | |
188 | // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" | |
189 | // ToFixed(0.1, 17) -> "0.10000000000000001" | |
190 | // | |
191 | // If requested_digits equals 0, then the tail of the result depends on | |
192 | // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. | |
193 | // Examples, for requested_digits == 0, | |
194 | // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be | |
195 | // - false and false: then 123.45 -> 123 | |
196 | // 0.678 -> 1 | |
197 | // - true and false: then 123.45 -> 123. | |
198 | // 0.678 -> 1. | |
199 | // - true and true: then 123.45 -> 123.0 | |
200 | // 0.678 -> 1.0 | |
201 | // | |
202 | // Returns true if the conversion succeeds. The conversion always succeeds | |
203 | // except for the following cases: | |
204 | // - the input value is special and no infinity_symbol or nan_symbol has | |
205 | // been provided to the constructor, | |
206 | // - 'value' > 10^kMaxFixedDigitsBeforePoint, or | |
207 | // - 'requested_digits' > kMaxFixedDigitsAfterPoint. | |
208 | // The last two conditions imply that the result will never contain more than | |
209 | // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters | |
210 | // (one additional character for the sign, and one for the decimal point). | |
211 | bool ToFixed(double value, | |
212 | int requested_digits, | |
213 | StringBuilder* result_builder) const; | |
214 | ||
215 | // Computes a representation in exponential format with requested_digits | |
216 | // after the decimal point. The last emitted digit is rounded. | |
217 | // If requested_digits equals -1, then the shortest exponential representation | |
218 | // is computed. | |
219 | // | |
220 | // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and | |
221 | // exponent_character set to 'e'. | |
222 | // ToExponential(3.12, 1) -> "3.1e0" | |
223 | // ToExponential(5.0, 3) -> "5.000e0" | |
224 | // ToExponential(0.001, 2) -> "1.00e-3" | |
225 | // ToExponential(3.1415, -1) -> "3.1415e0" | |
226 | // ToExponential(3.1415, 4) -> "3.1415e0" | |
227 | // ToExponential(3.1415, 3) -> "3.142e0" | |
228 | // ToExponential(123456789000000, 3) -> "1.235e14" | |
229 | // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" | |
230 | // ToExponential(1000000000000000019884624838656.0, 32) -> | |
231 | // "1.00000000000000001988462483865600e30" | |
232 | // ToExponential(1234, 0) -> "1e3" | |
233 | // | |
234 | // Returns true if the conversion succeeds. The conversion always succeeds | |
235 | // except for the following cases: | |
236 | // - the input value is special and no infinity_symbol or nan_symbol has | |
237 | // been provided to the constructor, | |
238 | // - 'requested_digits' > kMaxExponentialDigits. | |
239 | // The last condition implies that the result will never contain more than | |
240 | // kMaxExponentialDigits + 8 characters (the sign, the digit before the | |
241 | // decimal point, the decimal point, the exponent character, the | |
242 | // exponent's sign, and at most 3 exponent digits). | |
243 | bool ToExponential(double value, | |
244 | int requested_digits, | |
245 | StringBuilder* result_builder) const; | |
246 | ||
247 | // Computes 'precision' leading digits of the given 'value' and returns them | |
248 | // either in exponential or decimal format, depending on | |
249 | // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the | |
250 | // constructor). | |
251 | // The last computed digit is rounded. | |
252 | // | |
253 | // Example with max_leading_padding_zeroes_in_precision_mode = 6. | |
254 | // ToPrecision(0.0000012345, 2) -> "0.0000012" | |
255 | // ToPrecision(0.00000012345, 2) -> "1.2e-7" | |
256 | // Similarily the converter may add up to | |
257 | // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid | |
258 | // returning an exponential representation. A zero added by the | |
259 | // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. | |
260 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: | |
261 | // ToPrecision(230.0, 2) -> "230" | |
262 | // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. | |
263 | // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. | |
264 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no | |
265 | // EMIT_TRAILING_ZERO_AFTER_POINT: | |
266 | // ToPrecision(123450.0, 6) -> "123450" | |
267 | // ToPrecision(123450.0, 5) -> "123450" | |
268 | // ToPrecision(123450.0, 4) -> "123500" | |
269 | // ToPrecision(123450.0, 3) -> "123000" | |
270 | // ToPrecision(123450.0, 2) -> "1.2e5" | |
271 | // | |
272 | // Returns true if the conversion succeeds. The conversion always succeeds | |
273 | // except for the following cases: | |
274 | // - the input value is special and no infinity_symbol or nan_symbol has | |
275 | // been provided to the constructor, | |
276 | // - precision < kMinPericisionDigits | |
277 | // - precision > kMaxPrecisionDigits | |
278 | // The last condition implies that the result will never contain more than | |
279 | // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the | |
280 | // exponent character, the exponent's sign, and at most 3 exponent digits). | |
281 | bool ToPrecision(double value, | |
282 | int precision, | |
283 | StringBuilder* result_builder) const; | |
284 | ||
285 | enum DtoaMode { | |
286 | // Produce the shortest correct representation. | |
287 | // For example the output of 0.299999999999999988897 is (the less accurate | |
288 | // but correct) 0.3. | |
289 | SHORTEST, | |
290 | // Same as SHORTEST, but for single-precision floats. | |
291 | SHORTEST_SINGLE, | |
292 | // Produce a fixed number of digits after the decimal point. | |
293 | // For instance fixed(0.1, 4) becomes 0.1000 | |
294 | // If the input number is big, the output will be big. | |
295 | FIXED, | |
296 | // Fixed number of digits (independent of the decimal point). | |
297 | PRECISION | |
298 | }; | |
299 | ||
300 | // The maximal number of digits that are needed to emit a double in base 10. | |
301 | // A higher precision can be achieved by using more digits, but the shortest | |
302 | // accurate representation of any double will never use more digits than | |
303 | // kBase10MaximalLength. | |
304 | // Note that DoubleToAscii null-terminates its input. So the given buffer | |
305 | // should be at least kBase10MaximalLength + 1 characters long. | |
306 | static const int kBase10MaximalLength = 17; | |
307 | ||
308 | // Converts the given double 'v' to digit characters. 'v' must not be NaN, | |
309 | // +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also | |
310 | // applies to 'v' after it has been casted to a single-precision float. That | |
311 | // is, in this mode static_cast<float>(v) must not be NaN, +Infinity or | |
312 | // -Infinity. | |
313 | // | |
314 | // The result should be interpreted as buffer * 10^(point-length). | |
315 | // | |
316 | // The digits are written to the buffer in the platform's charset, which is | |
317 | // often UTF-8 (with ASCII-range digits) but may be another charset, such | |
318 | // as EBCDIC. | |
319 | // | |
320 | // The output depends on the given mode: | |
321 | // - SHORTEST: produce the least amount of digits for which the internal | |
322 | // identity requirement is still satisfied. If the digits are printed | |
323 | // (together with the correct exponent) then reading this number will give | |
324 | // 'v' again. The buffer will choose the representation that is closest to | |
325 | // 'v'. If there are two at the same distance, than the one farther away | |
326 | // from 0 is chosen (halfway cases - ending with 5 - are rounded up). | |
327 | // In this mode the 'requested_digits' parameter is ignored. | |
328 | // - SHORTEST_SINGLE: same as SHORTEST but with single-precision. | |
329 | // - FIXED: produces digits necessary to print a given number with | |
330 | // 'requested_digits' digits after the decimal point. The produced digits | |
331 | // might be too short in which case the caller has to fill the remainder | |
332 | // with '0's. | |
333 | // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. | |
334 | // Halfway cases are rounded towards +/-Infinity (away from 0). The call | |
335 | // toFixed(0.15, 2) thus returns buffer="2", point=0. | |
336 | // The returned buffer may contain digits that would be truncated from the | |
337 | // shortest representation of the input. | |
338 | // - PRECISION: produces 'requested_digits' where the first digit is not '0'. | |
339 | // Even though the length of produced digits usually equals | |
340 | // 'requested_digits', the function is allowed to return fewer digits, in | |
341 | // which case the caller has to fill the missing digits with '0's. | |
342 | // Halfway cases are again rounded away from 0. | |
343 | // DoubleToAscii expects the given buffer to be big enough to hold all | |
344 | // digits and a terminating null-character. In SHORTEST-mode it expects a | |
345 | // buffer of at least kBase10MaximalLength + 1. In all other modes the | |
346 | // requested_digits parameter and the padding-zeroes limit the size of the | |
347 | // output. Don't forget the decimal point, the exponent character and the | |
348 | // terminating null-character when computing the maximal output size. | |
349 | // The given length is only used in debug mode to ensure the buffer is big | |
350 | // enough. | |
351 | static void DoubleToAscii(double v, | |
352 | DtoaMode mode, | |
353 | int requested_digits, | |
354 | char* buffer, | |
355 | int buffer_length, | |
356 | bool* sign, | |
357 | int* length, | |
358 | int* point); | |
359 | ||
360 | private: | |
361 | // Implementation for ToShortest and ToShortestSingle. | |
362 | bool ToShortestIeeeNumber(double value, | |
363 | StringBuilder* result_builder, | |
364 | DtoaMode mode) const; | |
365 | ||
366 | // If the value is a special value (NaN or Infinity) constructs the | |
367 | // corresponding string using the configured infinity/nan-symbol. | |
368 | // If either of them is NULL or the value is not special then the | |
369 | // function returns false. | |
370 | bool HandleSpecialValues(double value, StringBuilder* result_builder) const; | |
371 | // Constructs an exponential representation (i.e. 1.234e56). | |
372 | // The given exponent assumes a decimal point after the first decimal digit. | |
373 | void CreateExponentialRepresentation(const char* decimal_digits, | |
374 | int length, | |
375 | int exponent, | |
376 | StringBuilder* result_builder) const; | |
377 | // Creates a decimal representation (i.e 1234.5678). | |
378 | void CreateDecimalRepresentation(const char* decimal_digits, | |
379 | int length, | |
380 | int decimal_point, | |
381 | int digits_after_point, | |
382 | StringBuilder* result_builder) const; | |
383 | ||
384 | const int flags_; | |
385 | const char* const infinity_symbol_; | |
386 | const char* const nan_symbol_; | |
387 | const char exponent_character_; | |
388 | const int decimal_in_shortest_low_; | |
389 | const int decimal_in_shortest_high_; | |
390 | const int max_leading_padding_zeroes_in_precision_mode_; | |
391 | const int max_trailing_padding_zeroes_in_precision_mode_; | |
392 | ||
393 | DC_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter); | |
394 | }; | |
395 | ||
396 | ||
397 | class StringToDoubleConverter { | |
398 | public: | |
399 | // Enumeration for allowing octals and ignoring junk when converting | |
400 | // strings to numbers. | |
401 | enum Flags { | |
402 | NO_FLAGS = 0, | |
403 | ALLOW_HEX = 1, | |
404 | ALLOW_OCTALS = 2, | |
405 | ALLOW_TRAILING_JUNK = 4, | |
406 | ALLOW_LEADING_SPACES = 8, | |
407 | ALLOW_TRAILING_SPACES = 16, | |
408 | ALLOW_SPACES_AFTER_SIGN = 32, | |
409 | ALLOW_CASE_INSENSIBILITY = 64, | |
410 | ALLOW_HEX_FLOATS = 128, | |
411 | }; | |
412 | ||
413 | static const uc16 kNoSeparator = '\0'; | |
414 | ||
415 | // Flags should be a bit-or combination of the possible Flags-enum. | |
416 | // - NO_FLAGS: no special flags. | |
417 | // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers. | |
418 | // Ex: StringToDouble("0x1234") -> 4660.0 | |
419 | // In StringToDouble("0x1234.56") the characters ".56" are trailing | |
420 | // junk. The result of the call is hence dependent on | |
421 | // the ALLOW_TRAILING_JUNK flag and/or the junk value. | |
422 | // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK, | |
423 | // the string will not be parsed as "0" followed by junk. | |
424 | // | |
425 | // - ALLOW_OCTALS: recognizes the prefix "0" for octals: | |
426 | // If a sequence of octal digits starts with '0', then the number is | |
427 | // read as octal integer. Octal numbers may only be integers. | |
428 | // Ex: StringToDouble("01234") -> 668.0 | |
429 | // StringToDouble("012349") -> 12349.0 // Not a sequence of octal | |
430 | // // digits. | |
431 | // In StringToDouble("01234.56") the characters ".56" are trailing | |
432 | // junk. The result of the call is hence dependent on | |
433 | // the ALLOW_TRAILING_JUNK flag and/or the junk value. | |
434 | // In StringToDouble("01234e56") the characters "e56" are trailing | |
435 | // junk, too. | |
436 | // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of | |
437 | // a double literal. | |
438 | // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces, | |
439 | // new-lines, and tabs. | |
440 | // - ALLOW_TRAILING_SPACES: ignore trailing whitespace. | |
441 | // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign. | |
442 | // Ex: StringToDouble("- 123.2") -> -123.2. | |
443 | // StringToDouble("+ 123.2") -> 123.2 | |
444 | // - ALLOW_CASE_INSENSIBILITY: ignore case of characters for special values: | |
445 | // infinity and nan. | |
446 | // - ALLOW_HEX_FLOATS: allows hexadecimal float literals. | |
447 | // This *must* start with "0x" and separate the exponent with "p". | |
448 | // Examples: 0x1.2p3 == 9.0 | |
449 | // 0x10.1p0 == 16.0625 | |
450 | // ALLOW_HEX and ALLOW_HEX_FLOATS are indendent. | |
451 | // | |
452 | // empty_string_value is returned when an empty string is given as input. | |
453 | // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string | |
454 | // containing only spaces is converted to the 'empty_string_value', too. | |
455 | // | |
456 | // junk_string_value is returned when | |
457 | // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not | |
458 | // part of a double-literal) is found. | |
459 | // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a | |
460 | // double literal. | |
461 | // | |
462 | // infinity_symbol and nan_symbol are strings that are used to detect | |
463 | // inputs that represent infinity and NaN. They can be null, in which case | |
464 | // they are ignored. | |
465 | // The conversion routine first reads any possible signs. Then it compares the | |
466 | // following character of the input-string with the first character of | |
467 | // the infinity, and nan-symbol. If either matches, the function assumes, that | |
468 | // a match has been found, and expects the following input characters to match | |
469 | // the remaining characters of the special-value symbol. | |
470 | // This means that the following restrictions apply to special-value symbols: | |
471 | // - they must not start with signs ('+', or '-'), | |
472 | // - they must not have the same first character. | |
473 | // - they must not start with digits. | |
474 | // | |
475 | // If the separator character is not kNoSeparator, then that specific | |
476 | // character is ignored when in between two valid digits of the significant. | |
477 | // It is not allowed to appear in the exponent. | |
478 | // It is not allowed to lead or trail the number. | |
479 | // It is not allowed to appear twice next to each other. | |
480 | // | |
481 | // Examples: | |
482 | // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK, | |
483 | // empty_string_value = 0.0, | |
484 | // junk_string_value = NaN, | |
485 | // infinity_symbol = "infinity", | |
486 | // nan_symbol = "nan": | |
487 | // StringToDouble("0x1234") -> 4660.0. | |
488 | // StringToDouble("0x1234K") -> 4660.0. | |
489 | // StringToDouble("") -> 0.0 // empty_string_value. | |
490 | // StringToDouble(" ") -> NaN // junk_string_value. | |
491 | // StringToDouble(" 1") -> NaN // junk_string_value. | |
492 | // StringToDouble("0x") -> NaN // junk_string_value. | |
493 | // StringToDouble("-123.45") -> -123.45. | |
494 | // StringToDouble("--123.45") -> NaN // junk_string_value. | |
495 | // StringToDouble("123e45") -> 123e45. | |
496 | // StringToDouble("123E45") -> 123e45. | |
497 | // StringToDouble("123e+45") -> 123e45. | |
498 | // StringToDouble("123E-45") -> 123e-45. | |
499 | // StringToDouble("123e") -> 123.0 // trailing junk ignored. | |
500 | // StringToDouble("123e-") -> 123.0 // trailing junk ignored. | |
501 | // StringToDouble("+NaN") -> NaN // NaN string literal. | |
502 | // StringToDouble("-infinity") -> -inf. // infinity literal. | |
503 | // StringToDouble("Infinity") -> NaN // junk_string_value. | |
504 | // | |
505 | // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES, | |
506 | // empty_string_value = 0.0, | |
507 | // junk_string_value = NaN, | |
508 | // infinity_symbol = NULL, | |
509 | // nan_symbol = NULL: | |
510 | // StringToDouble("0x1234") -> NaN // junk_string_value. | |
511 | // StringToDouble("01234") -> 668.0. | |
512 | // StringToDouble("") -> 0.0 // empty_string_value. | |
513 | // StringToDouble(" ") -> 0.0 // empty_string_value. | |
514 | // StringToDouble(" 1") -> 1.0 | |
515 | // StringToDouble("0x") -> NaN // junk_string_value. | |
516 | // StringToDouble("0123e45") -> NaN // junk_string_value. | |
517 | // StringToDouble("01239E45") -> 1239e45. | |
518 | // StringToDouble("-infinity") -> NaN // junk_string_value. | |
519 | // StringToDouble("NaN") -> NaN // junk_string_value. | |
520 | // | |
521 | // flags = NO_FLAGS, | |
522 | // separator = ' ': | |
523 | // StringToDouble("1 2 3 4") -> 1234.0 | |
524 | // StringToDouble("1 2") -> NaN // junk_string_value | |
525 | // StringToDouble("1 000 000.0") -> 1000000.0 | |
526 | // StringToDouble("1.000 000") -> 1.0 | |
527 | // StringToDouble("1.0e1 000") -> NaN // junk_string_value | |
528 | StringToDoubleConverter(int flags, | |
529 | double empty_string_value, | |
530 | double junk_string_value, | |
531 | const char* infinity_symbol, | |
532 | const char* nan_symbol, | |
533 | uc16 separator = kNoSeparator) | |
534 | : flags_(flags), | |
535 | empty_string_value_(empty_string_value), | |
536 | junk_string_value_(junk_string_value), | |
537 | infinity_symbol_(infinity_symbol), | |
538 | nan_symbol_(nan_symbol), | |
539 | separator_(separator) { | |
540 | } | |
541 | ||
542 | // Performs the conversion. | |
543 | // The output parameter 'processed_characters_count' is set to the number | |
544 | // of characters that have been processed to read the number. | |
545 | // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included | |
546 | // in the 'processed_characters_count'. Trailing junk is never included. | |
547 | double StringToDouble(const char* buffer, | |
548 | int length, | |
549 | int* processed_characters_count) const; | |
550 | ||
551 | // Same as StringToDouble above but for 16 bit characters. | |
552 | double StringToDouble(const uc16* buffer, | |
553 | int length, | |
554 | int* processed_characters_count) const; | |
555 | ||
556 | // Same as StringToDouble but reads a float. | |
557 | // Note that this is not equivalent to static_cast<float>(StringToDouble(...)) | |
558 | // due to potential double-rounding. | |
559 | float StringToFloat(const char* buffer, | |
560 | int length, | |
561 | int* processed_characters_count) const; | |
562 | ||
563 | // Same as StringToFloat above but for 16 bit characters. | |
564 | float StringToFloat(const uc16* buffer, | |
565 | int length, | |
566 | int* processed_characters_count) const; | |
567 | ||
568 | private: | |
569 | const int flags_; | |
570 | const double empty_string_value_; | |
571 | const double junk_string_value_; | |
572 | const char* const infinity_symbol_; | |
573 | const char* const nan_symbol_; | |
574 | const uc16 separator_; | |
575 | ||
576 | template <class Iterator> | |
577 | double StringToIeee(Iterator start_pointer, | |
578 | int length, | |
579 | bool read_as_double, | |
580 | int* processed_characters_count) const; | |
581 | ||
582 | DC_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter); | |
583 | }; | |
584 | ||
585 | } // namespace double_conversion | |
586 | ||
587 | #endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ |