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