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