/src/mozilla-central/intl/icu/source/i18n/double-conversion.cpp
Line | Count | Source (jump to first uncovered line) |
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 2010 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 | | #include <limits.h> |
38 | | #include <math.h> |
39 | | |
40 | | // ICU PATCH: Customize header file paths for ICU. |
41 | | // The file fixed-dtoa.h is not needed. |
42 | | |
43 | | #include "double-conversion.h" |
44 | | |
45 | | #include "double-conversion-bignum-dtoa.h" |
46 | | #include "double-conversion-fast-dtoa.h" |
47 | | #include "double-conversion-ieee.h" |
48 | | #include "double-conversion-strtod.h" |
49 | | #include "double-conversion-utils.h" |
50 | | |
51 | | // ICU PATCH: Wrap in ICU namespace |
52 | | U_NAMESPACE_BEGIN |
53 | | |
54 | | namespace double_conversion { |
55 | | |
56 | | #if 0 // not needed for ICU |
57 | | const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() { |
58 | | int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN; |
59 | | static DoubleToStringConverter converter(flags, |
60 | | "Infinity", |
61 | | "NaN", |
62 | | 'e', |
63 | | -6, 21, |
64 | | 6, 0); |
65 | | return converter; |
66 | | } |
67 | | |
68 | | |
69 | | bool DoubleToStringConverter::HandleSpecialValues( |
70 | | double value, |
71 | | StringBuilder* result_builder) const { |
72 | | Double double_inspect(value); |
73 | | if (double_inspect.IsInfinite()) { |
74 | | if (infinity_symbol_ == NULL) return false; |
75 | | if (value < 0) { |
76 | | result_builder->AddCharacter('-'); |
77 | | } |
78 | | result_builder->AddString(infinity_symbol_); |
79 | | return true; |
80 | | } |
81 | | if (double_inspect.IsNan()) { |
82 | | if (nan_symbol_ == NULL) return false; |
83 | | result_builder->AddString(nan_symbol_); |
84 | | return true; |
85 | | } |
86 | | return false; |
87 | | } |
88 | | |
89 | | |
90 | | void DoubleToStringConverter::CreateExponentialRepresentation( |
91 | | const char* decimal_digits, |
92 | | int length, |
93 | | int exponent, |
94 | | StringBuilder* result_builder) const { |
95 | | ASSERT(length != 0); |
96 | | result_builder->AddCharacter(decimal_digits[0]); |
97 | | if (length != 1) { |
98 | | result_builder->AddCharacter('.'); |
99 | | result_builder->AddSubstring(&decimal_digits[1], length-1); |
100 | | } |
101 | | result_builder->AddCharacter(exponent_character_); |
102 | | if (exponent < 0) { |
103 | | result_builder->AddCharacter('-'); |
104 | | exponent = -exponent; |
105 | | } else { |
106 | | if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) { |
107 | | result_builder->AddCharacter('+'); |
108 | | } |
109 | | } |
110 | | if (exponent == 0) { |
111 | | result_builder->AddCharacter('0'); |
112 | | return; |
113 | | } |
114 | | ASSERT(exponent < 1e4); |
115 | | const int kMaxExponentLength = 5; |
116 | | char buffer[kMaxExponentLength + 1]; |
117 | | buffer[kMaxExponentLength] = '\0'; |
118 | | int first_char_pos = kMaxExponentLength; |
119 | | while (exponent > 0) { |
120 | | buffer[--first_char_pos] = '0' + (exponent % 10); |
121 | | exponent /= 10; |
122 | | } |
123 | | result_builder->AddSubstring(&buffer[first_char_pos], |
124 | | kMaxExponentLength - first_char_pos); |
125 | | } |
126 | | |
127 | | |
128 | | void DoubleToStringConverter::CreateDecimalRepresentation( |
129 | | const char* decimal_digits, |
130 | | int length, |
131 | | int decimal_point, |
132 | | int digits_after_point, |
133 | | StringBuilder* result_builder) const { |
134 | | // Create a representation that is padded with zeros if needed. |
135 | | if (decimal_point <= 0) { |
136 | | // "0.00000decimal_rep" or "0.000decimal_rep00". |
137 | | result_builder->AddCharacter('0'); |
138 | | if (digits_after_point > 0) { |
139 | | result_builder->AddCharacter('.'); |
140 | | result_builder->AddPadding('0', -decimal_point); |
141 | | ASSERT(length <= digits_after_point - (-decimal_point)); |
142 | | result_builder->AddSubstring(decimal_digits, length); |
143 | | int remaining_digits = digits_after_point - (-decimal_point) - length; |
144 | | result_builder->AddPadding('0', remaining_digits); |
145 | | } |
146 | | } else if (decimal_point >= length) { |
147 | | // "decimal_rep0000.00000" or "decimal_rep.0000". |
148 | | result_builder->AddSubstring(decimal_digits, length); |
149 | | result_builder->AddPadding('0', decimal_point - length); |
150 | | if (digits_after_point > 0) { |
151 | | result_builder->AddCharacter('.'); |
152 | | result_builder->AddPadding('0', digits_after_point); |
153 | | } |
154 | | } else { |
155 | | // "decima.l_rep000". |
156 | | ASSERT(digits_after_point > 0); |
157 | | result_builder->AddSubstring(decimal_digits, decimal_point); |
158 | | result_builder->AddCharacter('.'); |
159 | | ASSERT(length - decimal_point <= digits_after_point); |
160 | | result_builder->AddSubstring(&decimal_digits[decimal_point], |
161 | | length - decimal_point); |
162 | | int remaining_digits = digits_after_point - (length - decimal_point); |
163 | | result_builder->AddPadding('0', remaining_digits); |
164 | | } |
165 | | if (digits_after_point == 0) { |
166 | | if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) { |
167 | | result_builder->AddCharacter('.'); |
168 | | } |
169 | | if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) { |
170 | | result_builder->AddCharacter('0'); |
171 | | } |
172 | | } |
173 | | } |
174 | | |
175 | | |
176 | | bool DoubleToStringConverter::ToShortestIeeeNumber( |
177 | | double value, |
178 | | StringBuilder* result_builder, |
179 | | DoubleToStringConverter::DtoaMode mode) const { |
180 | | ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE); |
181 | | if (Double(value).IsSpecial()) { |
182 | | return HandleSpecialValues(value, result_builder); |
183 | | } |
184 | | |
185 | | int decimal_point; |
186 | | bool sign; |
187 | | const int kDecimalRepCapacity = kBase10MaximalLength + 1; |
188 | | char decimal_rep[kDecimalRepCapacity]; |
189 | | int decimal_rep_length; |
190 | | |
191 | | DoubleToAscii(value, mode, 0, decimal_rep, kDecimalRepCapacity, |
192 | | &sign, &decimal_rep_length, &decimal_point); |
193 | | |
194 | | bool unique_zero = (flags_ & UNIQUE_ZERO) != 0; |
195 | | if (sign && (value != 0.0 || !unique_zero)) { |
196 | | result_builder->AddCharacter('-'); |
197 | | } |
198 | | |
199 | | int exponent = decimal_point - 1; |
200 | | if ((decimal_in_shortest_low_ <= exponent) && |
201 | | (exponent < decimal_in_shortest_high_)) { |
202 | | CreateDecimalRepresentation(decimal_rep, decimal_rep_length, |
203 | | decimal_point, |
204 | | Max(0, decimal_rep_length - decimal_point), |
205 | | result_builder); |
206 | | } else { |
207 | | CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent, |
208 | | result_builder); |
209 | | } |
210 | | return true; |
211 | | } |
212 | | |
213 | | |
214 | | bool DoubleToStringConverter::ToFixed(double value, |
215 | | int requested_digits, |
216 | | StringBuilder* result_builder) const { |
217 | | ASSERT(kMaxFixedDigitsBeforePoint == 60); |
218 | | const double kFirstNonFixed = 1e60; |
219 | | |
220 | | if (Double(value).IsSpecial()) { |
221 | | return HandleSpecialValues(value, result_builder); |
222 | | } |
223 | | |
224 | | if (requested_digits > kMaxFixedDigitsAfterPoint) return false; |
225 | | if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false; |
226 | | |
227 | | // Find a sufficiently precise decimal representation of n. |
228 | | int decimal_point; |
229 | | bool sign; |
230 | | // Add space for the '\0' byte. |
231 | | const int kDecimalRepCapacity = |
232 | | kMaxFixedDigitsBeforePoint + kMaxFixedDigitsAfterPoint + 1; |
233 | | char decimal_rep[kDecimalRepCapacity]; |
234 | | int decimal_rep_length; |
235 | | DoubleToAscii(value, FIXED, requested_digits, |
236 | | decimal_rep, kDecimalRepCapacity, |
237 | | &sign, &decimal_rep_length, &decimal_point); |
238 | | |
239 | | bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); |
240 | | if (sign && (value != 0.0 || !unique_zero)) { |
241 | | result_builder->AddCharacter('-'); |
242 | | } |
243 | | |
244 | | CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point, |
245 | | requested_digits, result_builder); |
246 | | return true; |
247 | | } |
248 | | |
249 | | |
250 | | bool DoubleToStringConverter::ToExponential( |
251 | | double value, |
252 | | int requested_digits, |
253 | | StringBuilder* result_builder) const { |
254 | | if (Double(value).IsSpecial()) { |
255 | | return HandleSpecialValues(value, result_builder); |
256 | | } |
257 | | |
258 | | if (requested_digits < -1) return false; |
259 | | if (requested_digits > kMaxExponentialDigits) return false; |
260 | | |
261 | | int decimal_point; |
262 | | bool sign; |
263 | | // Add space for digit before the decimal point and the '\0' character. |
264 | | const int kDecimalRepCapacity = kMaxExponentialDigits + 2; |
265 | | ASSERT(kDecimalRepCapacity > kBase10MaximalLength); |
266 | | char decimal_rep[kDecimalRepCapacity]; |
267 | | int decimal_rep_length; |
268 | | |
269 | | if (requested_digits == -1) { |
270 | | DoubleToAscii(value, SHORTEST, 0, |
271 | | decimal_rep, kDecimalRepCapacity, |
272 | | &sign, &decimal_rep_length, &decimal_point); |
273 | | } else { |
274 | | DoubleToAscii(value, PRECISION, requested_digits + 1, |
275 | | decimal_rep, kDecimalRepCapacity, |
276 | | &sign, &decimal_rep_length, &decimal_point); |
277 | | ASSERT(decimal_rep_length <= requested_digits + 1); |
278 | | |
279 | | for (int i = decimal_rep_length; i < requested_digits + 1; ++i) { |
280 | | decimal_rep[i] = '0'; |
281 | | } |
282 | | decimal_rep_length = requested_digits + 1; |
283 | | } |
284 | | |
285 | | bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); |
286 | | if (sign && (value != 0.0 || !unique_zero)) { |
287 | | result_builder->AddCharacter('-'); |
288 | | } |
289 | | |
290 | | int exponent = decimal_point - 1; |
291 | | CreateExponentialRepresentation(decimal_rep, |
292 | | decimal_rep_length, |
293 | | exponent, |
294 | | result_builder); |
295 | | return true; |
296 | | } |
297 | | |
298 | | |
299 | | bool DoubleToStringConverter::ToPrecision(double value, |
300 | | int precision, |
301 | | StringBuilder* result_builder) const { |
302 | | if (Double(value).IsSpecial()) { |
303 | | return HandleSpecialValues(value, result_builder); |
304 | | } |
305 | | |
306 | | if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) { |
307 | | return false; |
308 | | } |
309 | | |
310 | | // Find a sufficiently precise decimal representation of n. |
311 | | int decimal_point; |
312 | | bool sign; |
313 | | // Add one for the terminating null character. |
314 | | const int kDecimalRepCapacity = kMaxPrecisionDigits + 1; |
315 | | char decimal_rep[kDecimalRepCapacity]; |
316 | | int decimal_rep_length; |
317 | | |
318 | | DoubleToAscii(value, PRECISION, precision, |
319 | | decimal_rep, kDecimalRepCapacity, |
320 | | &sign, &decimal_rep_length, &decimal_point); |
321 | | ASSERT(decimal_rep_length <= precision); |
322 | | |
323 | | bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); |
324 | | if (sign && (value != 0.0 || !unique_zero)) { |
325 | | result_builder->AddCharacter('-'); |
326 | | } |
327 | | |
328 | | // The exponent if we print the number as x.xxeyyy. That is with the |
329 | | // decimal point after the first digit. |
330 | | int exponent = decimal_point - 1; |
331 | | |
332 | | int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0; |
333 | | if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) || |
334 | | (decimal_point - precision + extra_zero > |
335 | | max_trailing_padding_zeroes_in_precision_mode_)) { |
336 | | // Fill buffer to contain 'precision' digits. |
337 | | // Usually the buffer is already at the correct length, but 'DoubleToAscii' |
338 | | // is allowed to return less characters. |
339 | | for (int i = decimal_rep_length; i < precision; ++i) { |
340 | | decimal_rep[i] = '0'; |
341 | | } |
342 | | |
343 | | CreateExponentialRepresentation(decimal_rep, |
344 | | precision, |
345 | | exponent, |
346 | | result_builder); |
347 | | } else { |
348 | | CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point, |
349 | | Max(0, precision - decimal_point), |
350 | | result_builder); |
351 | | } |
352 | | return true; |
353 | | } |
354 | | #endif // not needed for ICU |
355 | | |
356 | | |
357 | | static BignumDtoaMode DtoaToBignumDtoaMode( |
358 | 0 | DoubleToStringConverter::DtoaMode dtoa_mode) { |
359 | 0 | switch (dtoa_mode) { |
360 | 0 | case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST; |
361 | 0 | case DoubleToStringConverter::SHORTEST_SINGLE: |
362 | 0 | return BIGNUM_DTOA_SHORTEST_SINGLE; |
363 | 0 | case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED; |
364 | 0 | case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION; |
365 | 0 | default: |
366 | 0 | UNREACHABLE(); |
367 | 0 | } |
368 | 0 | } |
369 | | |
370 | | |
371 | | void DoubleToStringConverter::DoubleToAscii(double v, |
372 | | DtoaMode mode, |
373 | | int requested_digits, |
374 | | char* buffer, |
375 | | int buffer_length, |
376 | | bool* sign, |
377 | | int* length, |
378 | 0 | int* point) { |
379 | 0 | Vector<char> vector(buffer, buffer_length); |
380 | 0 | ASSERT(!Double(v).IsSpecial()); |
381 | 0 | ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0); |
382 | 0 |
|
383 | 0 | if (Double(v).Sign() < 0) { |
384 | 0 | *sign = true; |
385 | 0 | v = -v; |
386 | 0 | } else { |
387 | 0 | *sign = false; |
388 | 0 | } |
389 | 0 |
|
390 | 0 | if (mode == PRECISION && requested_digits == 0) { |
391 | 0 | vector[0] = '\0'; |
392 | 0 | *length = 0; |
393 | 0 | return; |
394 | 0 | } |
395 | 0 | |
396 | 0 | if (v == 0) { |
397 | 0 | vector[0] = '0'; |
398 | 0 | vector[1] = '\0'; |
399 | 0 | *length = 1; |
400 | 0 | *point = 1; |
401 | 0 | return; |
402 | 0 | } |
403 | 0 | |
404 | 0 | bool fast_worked; |
405 | 0 | switch (mode) { |
406 | 0 | case SHORTEST: |
407 | 0 | fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point); |
408 | 0 | break; |
409 | | #if 0 // not needed for ICU |
410 | | case SHORTEST_SINGLE: |
411 | | fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, |
412 | | vector, length, point); |
413 | | break; |
414 | | case FIXED: |
415 | | fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point); |
416 | | break; |
417 | | case PRECISION: |
418 | | fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits, |
419 | | vector, length, point); |
420 | | break; |
421 | | #endif // not needed for ICU |
422 | 0 | default: |
423 | 0 | fast_worked = false; |
424 | 0 | UNREACHABLE(); |
425 | 0 | } |
426 | 0 | if (fast_worked) return; |
427 | 0 | |
428 | 0 | // If the fast dtoa didn't succeed use the slower bignum version. |
429 | 0 | BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode); |
430 | 0 | BignumDtoa(v, bignum_mode, requested_digits, vector, length, point); |
431 | 0 | vector[*length] = '\0'; |
432 | 0 | } |
433 | | |
434 | | |
435 | | // Consumes the given substring from the iterator. |
436 | | // Returns false, if the substring does not match. |
437 | | template <class Iterator> |
438 | | static bool ConsumeSubString(Iterator* current, |
439 | | Iterator end, |
440 | 0 | const char* substring) { |
441 | 0 | ASSERT(**current == *substring); |
442 | 0 | for (substring++; *substring != '\0'; substring++) { |
443 | 0 | ++*current; |
444 | 0 | if (*current == end || **current != *substring) return false; |
445 | 0 | } |
446 | 0 | ++*current; |
447 | 0 | return true; |
448 | 0 | } Unexecuted instantiation: double-conversion.cpp:bool icu_62::double_conversion::ConsumeSubString<char const*>(char const**, char const*, char const*) Unexecuted instantiation: double-conversion.cpp:bool icu_62::double_conversion::ConsumeSubString<unsigned short const*>(unsigned short const**, unsigned short const*, char const*) |
449 | | |
450 | | |
451 | | // Maximum number of significant digits in decimal representation. |
452 | | // The longest possible double in decimal representation is |
453 | | // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 |
454 | | // (768 digits). If we parse a number whose first digits are equal to a |
455 | | // mean of 2 adjacent doubles (that could have up to 769 digits) the result |
456 | | // must be rounded to the bigger one unless the tail consists of zeros, so |
457 | | // we don't need to preserve all the digits. |
458 | | const int kMaxSignificantDigits = 772; |
459 | | |
460 | | |
461 | | static const char kWhitespaceTable7[] = { 32, 13, 10, 9, 11, 12 }; |
462 | | static const int kWhitespaceTable7Length = ARRAY_SIZE(kWhitespaceTable7); |
463 | | |
464 | | |
465 | | static const uc16 kWhitespaceTable16[] = { |
466 | | 160, 8232, 8233, 5760, 6158, 8192, 8193, 8194, 8195, |
467 | | 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8239, 8287, 12288, 65279 |
468 | | }; |
469 | | static const int kWhitespaceTable16Length = ARRAY_SIZE(kWhitespaceTable16); |
470 | | |
471 | | |
472 | | |
473 | 0 | static bool isWhitespace(int x) { |
474 | 0 | if (x < 128) { |
475 | 0 | for (int i = 0; i < kWhitespaceTable7Length; i++) { |
476 | 0 | if (kWhitespaceTable7[i] == x) return true; |
477 | 0 | } |
478 | 0 | } else { |
479 | 0 | for (int i = 0; i < kWhitespaceTable16Length; i++) { |
480 | 0 | if (kWhitespaceTable16[i] == x) return true; |
481 | 0 | } |
482 | 0 | } |
483 | 0 | return false; |
484 | 0 | } |
485 | | |
486 | | |
487 | | // Returns true if a nonspace found and false if the end has reached. |
488 | | template <class Iterator> |
489 | 0 | static inline bool AdvanceToNonspace(Iterator* current, Iterator end) { |
490 | 0 | while (*current != end) { |
491 | 0 | if (!isWhitespace(**current)) return true; |
492 | 0 | ++*current; |
493 | 0 | } |
494 | 0 | return false; |
495 | 0 | } Unexecuted instantiation: double-conversion.cpp:bool icu_62::double_conversion::AdvanceToNonspace<char const*>(char const**, char const*) Unexecuted instantiation: double-conversion.cpp:bool icu_62::double_conversion::AdvanceToNonspace<char*>(char**, char*) Unexecuted instantiation: double-conversion.cpp:bool icu_62::double_conversion::AdvanceToNonspace<unsigned short const*>(unsigned short const**, unsigned short const*) |
496 | | |
497 | | |
498 | 0 | static bool isDigit(int x, int radix) { |
499 | 0 | return (x >= '0' && x <= '9' && x < '0' + radix) |
500 | 0 | || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) |
501 | 0 | || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); |
502 | 0 | } |
503 | | |
504 | | |
505 | 0 | static double SignedZero(bool sign) { |
506 | 0 | return sign ? -0.0 : 0.0; |
507 | 0 | } |
508 | | |
509 | | |
510 | | // Returns true if 'c' is a decimal digit that is valid for the given radix. |
511 | | // |
512 | | // The function is small and could be inlined, but VS2012 emitted a warning |
513 | | // because it constant-propagated the radix and concluded that the last |
514 | | // condition was always true. By moving it into a separate function the |
515 | | // compiler wouldn't warn anymore. |
516 | | #if _MSC_VER |
517 | | #pragma optimize("",off) |
518 | | static bool IsDecimalDigitForRadix(int c, int radix) { |
519 | | return '0' <= c && c <= '9' && (c - '0') < radix; |
520 | | } |
521 | | #pragma optimize("",on) |
522 | | #else |
523 | 0 | static bool inline IsDecimalDigitForRadix(int c, int radix) { |
524 | 0 | return '0' <= c && c <= '9' && (c - '0') < radix; |
525 | 0 | } |
526 | | #endif |
527 | | // Returns true if 'c' is a character digit that is valid for the given radix. |
528 | | // The 'a_character' should be 'a' or 'A'. |
529 | | // |
530 | | // The function is small and could be inlined, but VS2012 emitted a warning |
531 | | // because it constant-propagated the radix and concluded that the first |
532 | | // condition was always false. By moving it into a separate function the |
533 | | // compiler wouldn't warn anymore. |
534 | 0 | static bool IsCharacterDigitForRadix(int c, int radix, char a_character) { |
535 | 0 | return radix > 10 && c >= a_character && c < a_character + radix - 10; |
536 | 0 | } |
537 | | |
538 | | |
539 | | // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. |
540 | | template <int radix_log_2, class Iterator> |
541 | | static double RadixStringToIeee(Iterator* current, |
542 | | Iterator end, |
543 | | bool sign, |
544 | | bool allow_trailing_junk, |
545 | | double junk_string_value, |
546 | | bool read_as_double, |
547 | 0 | bool* result_is_junk) { |
548 | 0 | ASSERT(*current != end); |
549 | 0 |
|
550 | 0 | const int kDoubleSize = Double::kSignificandSize; |
551 | 0 | const int kSingleSize = Single::kSignificandSize; |
552 | 0 | const int kSignificandSize = read_as_double? kDoubleSize: kSingleSize; |
553 | 0 |
|
554 | 0 | *result_is_junk = true; |
555 | 0 |
|
556 | 0 | // Skip leading 0s. |
557 | 0 | while (**current == '0') { |
558 | 0 | ++(*current); |
559 | 0 | if (*current == end) { |
560 | 0 | *result_is_junk = false; |
561 | 0 | return SignedZero(sign); |
562 | 0 | } |
563 | 0 | } |
564 | 0 |
|
565 | 0 | int64_t number = 0; |
566 | 0 | int exponent = 0; |
567 | 0 | const int radix = (1 << radix_log_2); |
568 | 0 |
|
569 | 0 | do { |
570 | 0 | int digit; |
571 | 0 | if (IsDecimalDigitForRadix(**current, radix)) { |
572 | 0 | digit = static_cast<char>(**current) - '0'; |
573 | 0 | } else if (IsCharacterDigitForRadix(**current, radix, 'a')) { |
574 | 0 | digit = static_cast<char>(**current) - 'a' + 10; |
575 | 0 | } else if (IsCharacterDigitForRadix(**current, radix, 'A')) { |
576 | 0 | digit = static_cast<char>(**current) - 'A' + 10; |
577 | 0 | } else { |
578 | 0 | if (allow_trailing_junk || !AdvanceToNonspace(current, end)) { |
579 | 0 | break; |
580 | 0 | } else { |
581 | 0 | return junk_string_value; |
582 | 0 | } |
583 | 0 | } |
584 | 0 | |
585 | 0 | number = number * radix + digit; |
586 | 0 | int overflow = static_cast<int>(number >> kSignificandSize); |
587 | 0 | if (overflow != 0) { |
588 | 0 | // Overflow occurred. Need to determine which direction to round the |
589 | 0 | // result. |
590 | 0 | int overflow_bits_count = 1; |
591 | 0 | while (overflow > 1) { |
592 | 0 | overflow_bits_count++; |
593 | 0 | overflow >>= 1; |
594 | 0 | } |
595 | 0 |
|
596 | 0 | int dropped_bits_mask = ((1 << overflow_bits_count) - 1); |
597 | 0 | int dropped_bits = static_cast<int>(number) & dropped_bits_mask; |
598 | 0 | number >>= overflow_bits_count; |
599 | 0 | exponent = overflow_bits_count; |
600 | 0 |
|
601 | 0 | bool zero_tail = true; |
602 | 0 | for (;;) { |
603 | 0 | ++(*current); |
604 | 0 | if (*current == end || !isDigit(**current, radix)) break; |
605 | 0 | zero_tail = zero_tail && **current == '0'; |
606 | 0 | exponent += radix_log_2; |
607 | 0 | } |
608 | 0 |
|
609 | 0 | if (!allow_trailing_junk && AdvanceToNonspace(current, end)) { |
610 | 0 | return junk_string_value; |
611 | 0 | } |
612 | 0 | |
613 | 0 | int middle_value = (1 << (overflow_bits_count - 1)); |
614 | 0 | if (dropped_bits > middle_value) { |
615 | 0 | number++; // Rounding up. |
616 | 0 | } else if (dropped_bits == middle_value) { |
617 | 0 | // Rounding to even to consistency with decimals: half-way case rounds |
618 | 0 | // up if significant part is odd and down otherwise. |
619 | 0 | if ((number & 1) != 0 || !zero_tail) { |
620 | 0 | number++; // Rounding up. |
621 | 0 | } |
622 | 0 | } |
623 | 0 |
|
624 | 0 | // Rounding up may cause overflow. |
625 | 0 | if ((number & ((int64_t)1 << kSignificandSize)) != 0) { |
626 | 0 | exponent++; |
627 | 0 | number >>= 1; |
628 | 0 | } |
629 | 0 | break; |
630 | 0 | } |
631 | 0 | ++(*current); |
632 | 0 | } while (*current != end); |
633 | 0 |
|
634 | 0 | ASSERT(number < ((int64_t)1 << kSignificandSize)); |
635 | 0 | ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number); |
636 | 0 |
|
637 | 0 | *result_is_junk = false; |
638 | 0 |
|
639 | 0 | if (exponent == 0) { |
640 | 0 | if (sign) { |
641 | 0 | if (number == 0) return -0.0; |
642 | 0 | number = -number; |
643 | 0 | } |
644 | 0 | return static_cast<double>(number); |
645 | 0 | } |
646 | 0 | |
647 | 0 | ASSERT(number != 0); |
648 | 0 | return Double(DiyFp(number, exponent)).value(); |
649 | 0 | } Unexecuted instantiation: double-conversion.cpp:double icu_62::double_conversion::RadixStringToIeee<4, char const*>(char const**, char const*, bool, bool, double, bool, bool*) Unexecuted instantiation: double-conversion.cpp:double icu_62::double_conversion::RadixStringToIeee<3, char*>(char**, char*, bool, bool, double, bool, bool*) Unexecuted instantiation: double-conversion.cpp:double icu_62::double_conversion::RadixStringToIeee<4, unsigned short const*>(unsigned short const**, unsigned short const*, bool, bool, double, bool, bool*) |
650 | | |
651 | | template <class Iterator> |
652 | | double StringToDoubleConverter::StringToIeee( |
653 | | Iterator input, |
654 | | int length, |
655 | | bool read_as_double, |
656 | 0 | int* processed_characters_count) const { |
657 | 0 | Iterator current = input; |
658 | 0 | Iterator end = input + length; |
659 | 0 |
|
660 | 0 | *processed_characters_count = 0; |
661 | 0 |
|
662 | 0 | const bool allow_trailing_junk = (flags_ & ALLOW_TRAILING_JUNK) != 0; |
663 | 0 | const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0; |
664 | 0 | const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0; |
665 | 0 | const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0; |
666 | 0 |
|
667 | 0 | // To make sure that iterator dereferencing is valid the following |
668 | 0 | // convention is used: |
669 | 0 | // 1. Each '++current' statement is followed by check for equality to 'end'. |
670 | 0 | // 2. If AdvanceToNonspace returned false then current == end. |
671 | 0 | // 3. If 'current' becomes equal to 'end' the function returns or goes to |
672 | 0 | // 'parsing_done'. |
673 | 0 | // 4. 'current' is not dereferenced after the 'parsing_done' label. |
674 | 0 | // 5. Code before 'parsing_done' may rely on 'current != end'. |
675 | 0 | if (current == end) return empty_string_value_; |
676 | 0 | |
677 | 0 | if (allow_leading_spaces || allow_trailing_spaces) { |
678 | 0 | if (!AdvanceToNonspace(¤t, end)) { |
679 | 0 | *processed_characters_count = static_cast<int>(current - input); |
680 | 0 | return empty_string_value_; |
681 | 0 | } |
682 | 0 | if (!allow_leading_spaces && (input != current)) { |
683 | 0 | // No leading spaces allowed, but AdvanceToNonspace moved forward. |
684 | 0 | return junk_string_value_; |
685 | 0 | } |
686 | 0 | } |
687 | 0 | |
688 | 0 | // The longest form of simplified number is: "-<significant digits>.1eXXX\0". |
689 | 0 | const int kBufferSize = kMaxSignificantDigits + 10; |
690 | 0 | char buffer[kBufferSize]; // NOLINT: size is known at compile time. |
691 | 0 | int buffer_pos = 0; |
692 | 0 |
|
693 | 0 | // Exponent will be adjusted if insignificant digits of the integer part |
694 | 0 | // or insignificant leading zeros of the fractional part are dropped. |
695 | 0 | int exponent = 0; |
696 | 0 | int significant_digits = 0; |
697 | 0 | int insignificant_digits = 0; |
698 | 0 | bool nonzero_digit_dropped = false; |
699 | 0 |
|
700 | 0 | bool sign = false; |
701 | 0 |
|
702 | 0 | if (*current == '+' || *current == '-') { |
703 | 0 | sign = (*current == '-'); |
704 | 0 | ++current; |
705 | 0 | Iterator next_non_space = current; |
706 | 0 | // Skip following spaces (if allowed). |
707 | 0 | if (!AdvanceToNonspace(&next_non_space, end)) return junk_string_value_; |
708 | 0 | if (!allow_spaces_after_sign && (current != next_non_space)) { |
709 | 0 | return junk_string_value_; |
710 | 0 | } |
711 | 0 | current = next_non_space; |
712 | 0 | } |
713 | 0 |
|
714 | 0 | if (infinity_symbol_ != NULL) { |
715 | 0 | if (*current == infinity_symbol_[0]) { |
716 | 0 | if (!ConsumeSubString(¤t, end, infinity_symbol_)) { |
717 | 0 | return junk_string_value_; |
718 | 0 | } |
719 | 0 | |
720 | 0 | if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { |
721 | 0 | return junk_string_value_; |
722 | 0 | } |
723 | 0 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
724 | 0 | return junk_string_value_; |
725 | 0 | } |
726 | 0 | |
727 | 0 | ASSERT(buffer_pos == 0); |
728 | 0 | *processed_characters_count = static_cast<int>(current - input); |
729 | 0 | return sign ? -Double::Infinity() : Double::Infinity(); |
730 | 0 | } |
731 | 0 | } |
732 | 0 |
|
733 | 0 | if (nan_symbol_ != NULL) { |
734 | 0 | if (*current == nan_symbol_[0]) { |
735 | 0 | if (!ConsumeSubString(¤t, end, nan_symbol_)) { |
736 | 0 | return junk_string_value_; |
737 | 0 | } |
738 | 0 | |
739 | 0 | if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { |
740 | 0 | return junk_string_value_; |
741 | 0 | } |
742 | 0 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
743 | 0 | return junk_string_value_; |
744 | 0 | } |
745 | 0 | |
746 | 0 | ASSERT(buffer_pos == 0); |
747 | 0 | *processed_characters_count = static_cast<int>(current - input); |
748 | 0 | return sign ? -Double::NaN() : Double::NaN(); |
749 | 0 | } |
750 | 0 | } |
751 | 0 |
|
752 | 0 | bool leading_zero = false; |
753 | 0 | if (*current == '0') { |
754 | 0 | ++current; |
755 | 0 | if (current == end) { |
756 | 0 | *processed_characters_count = static_cast<int>(current - input); |
757 | 0 | return SignedZero(sign); |
758 | 0 | } |
759 | 0 | |
760 | 0 | leading_zero = true; |
761 | 0 |
|
762 | 0 | // It could be hexadecimal value. |
763 | 0 | if ((flags_ & ALLOW_HEX) && (*current == 'x' || *current == 'X')) { |
764 | 0 | ++current; |
765 | 0 | if (current == end || !isDigit(*current, 16)) { |
766 | 0 | return junk_string_value_; // "0x". |
767 | 0 | } |
768 | 0 | |
769 | 0 | bool result_is_junk; |
770 | 0 | double result = RadixStringToIeee<4>(¤t, |
771 | 0 | end, |
772 | 0 | sign, |
773 | 0 | allow_trailing_junk, |
774 | 0 | junk_string_value_, |
775 | 0 | read_as_double, |
776 | 0 | &result_is_junk); |
777 | 0 | if (!result_is_junk) { |
778 | 0 | if (allow_trailing_spaces) AdvanceToNonspace(¤t, end); |
779 | 0 | *processed_characters_count = static_cast<int>(current - input); |
780 | 0 | } |
781 | 0 | return result; |
782 | 0 | } |
783 | 0 |
|
784 | 0 | // Ignore leading zeros in the integer part. |
785 | 0 | while (*current == '0') { |
786 | 0 | ++current; |
787 | 0 | if (current == end) { |
788 | 0 | *processed_characters_count = static_cast<int>(current - input); |
789 | 0 | return SignedZero(sign); |
790 | 0 | } |
791 | 0 | } |
792 | 0 | } |
793 | 0 |
|
794 | 0 | bool octal = leading_zero && (flags_ & ALLOW_OCTALS) != 0; |
795 | 0 |
|
796 | 0 | // Copy significant digits of the integer part (if any) to the buffer. |
797 | 0 | while (*current >= '0' && *current <= '9') { |
798 | 0 | if (significant_digits < kMaxSignificantDigits) { |
799 | 0 | ASSERT(buffer_pos < kBufferSize); |
800 | 0 | buffer[buffer_pos++] = static_cast<char>(*current); |
801 | 0 | significant_digits++; |
802 | 0 | // Will later check if it's an octal in the buffer. |
803 | 0 | } else { |
804 | 0 | insignificant_digits++; // Move the digit into the exponential part. |
805 | 0 | nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'; |
806 | 0 | } |
807 | 0 | octal = octal && *current < '8'; |
808 | 0 | ++current; |
809 | 0 | if (current == end) goto parsing_done; |
810 | 0 | } |
811 | 0 |
|
812 | 0 | if (significant_digits == 0) { |
813 | 0 | octal = false; |
814 | 0 | } |
815 | 0 |
|
816 | 0 | if (*current == '.') { |
817 | 0 | if (octal && !allow_trailing_junk) return junk_string_value_; |
818 | 0 | if (octal) goto parsing_done; |
819 | 0 | |
820 | 0 | ++current; |
821 | 0 | if (current == end) { |
822 | 0 | if (significant_digits == 0 && !leading_zero) { |
823 | 0 | return junk_string_value_; |
824 | 0 | } else { |
825 | 0 | goto parsing_done; |
826 | 0 | } |
827 | 0 | } |
828 | 0 | |
829 | 0 | if (significant_digits == 0) { |
830 | 0 | // octal = false; |
831 | 0 | // Integer part consists of 0 or is absent. Significant digits start after |
832 | 0 | // leading zeros (if any). |
833 | 0 | while (*current == '0') { |
834 | 0 | ++current; |
835 | 0 | if (current == end) { |
836 | 0 | *processed_characters_count = static_cast<int>(current - input); |
837 | 0 | return SignedZero(sign); |
838 | 0 | } |
839 | 0 | exponent--; // Move this 0 into the exponent. |
840 | 0 | } |
841 | 0 | } |
842 | 0 |
|
843 | 0 | // There is a fractional part. |
844 | 0 | // We don't emit a '.', but adjust the exponent instead. |
845 | 0 | while (*current >= '0' && *current <= '9') { |
846 | 0 | if (significant_digits < kMaxSignificantDigits) { |
847 | 0 | ASSERT(buffer_pos < kBufferSize); |
848 | 0 | buffer[buffer_pos++] = static_cast<char>(*current); |
849 | 0 | significant_digits++; |
850 | 0 | exponent--; |
851 | 0 | } else { |
852 | 0 | // Ignore insignificant digits in the fractional part. |
853 | 0 | nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'; |
854 | 0 | } |
855 | 0 | ++current; |
856 | 0 | if (current == end) goto parsing_done; |
857 | 0 | } |
858 | 0 | } |
859 | 0 |
|
860 | 0 | if (!leading_zero && exponent == 0 && significant_digits == 0) { |
861 | 0 | // If leading_zeros is true then the string contains zeros. |
862 | 0 | // If exponent < 0 then string was [+-]\.0*... |
863 | 0 | // If significant_digits != 0 the string is not equal to 0. |
864 | 0 | // Otherwise there are no digits in the string. |
865 | 0 | return junk_string_value_; |
866 | 0 | } |
867 | 0 | |
868 | 0 | // Parse exponential part. |
869 | 0 | if (*current == 'e' || *current == 'E') { |
870 | 0 | if (octal && !allow_trailing_junk) return junk_string_value_; |
871 | 0 | if (octal) goto parsing_done; |
872 | 0 | ++current; |
873 | 0 | if (current == end) { |
874 | 0 | if (allow_trailing_junk) { |
875 | 0 | goto parsing_done; |
876 | 0 | } else { |
877 | 0 | return junk_string_value_; |
878 | 0 | } |
879 | 0 | } |
880 | 0 | char exponen_sign = '+'; |
881 | 0 | if (*current == '+' || *current == '-') { |
882 | 0 | exponen_sign = static_cast<char>(*current); |
883 | 0 | ++current; |
884 | 0 | if (current == end) { |
885 | 0 | if (allow_trailing_junk) { |
886 | 0 | goto parsing_done; |
887 | 0 | } else { |
888 | 0 | return junk_string_value_; |
889 | 0 | } |
890 | 0 | } |
891 | 0 | } |
892 | 0 | |
893 | 0 | if (current == end || *current < '0' || *current > '9') { |
894 | 0 | if (allow_trailing_junk) { |
895 | 0 | goto parsing_done; |
896 | 0 | } else { |
897 | 0 | return junk_string_value_; |
898 | 0 | } |
899 | 0 | } |
900 | 0 | |
901 | 0 | const int max_exponent = INT_MAX / 2; |
902 | 0 | ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2); |
903 | 0 | int num = 0; |
904 | 0 | do { |
905 | 0 | // Check overflow. |
906 | 0 | int digit = *current - '0'; |
907 | 0 | if (num >= max_exponent / 10 |
908 | 0 | && !(num == max_exponent / 10 && digit <= max_exponent % 10)) { |
909 | 0 | num = max_exponent; |
910 | 0 | } else { |
911 | 0 | num = num * 10 + digit; |
912 | 0 | } |
913 | 0 | ++current; |
914 | 0 | } while (current != end && *current >= '0' && *current <= '9'); |
915 | 0 |
|
916 | 0 | exponent += (exponen_sign == '-' ? -num : num); |
917 | 0 | } |
918 | 0 |
|
919 | 0 | if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { |
920 | 0 | return junk_string_value_; |
921 | 0 | } |
922 | 0 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
923 | 0 | return junk_string_value_; |
924 | 0 | } |
925 | 0 | if (allow_trailing_spaces) { |
926 | 0 | AdvanceToNonspace(¤t, end); |
927 | 0 | } |
928 | 0 |
|
929 | 0 | parsing_done: |
930 | 0 | exponent += insignificant_digits; |
931 | 0 |
|
932 | 0 | if (octal) { |
933 | 0 | double result; |
934 | 0 | bool result_is_junk; |
935 | 0 | char* start = buffer; |
936 | 0 | result = RadixStringToIeee<3>(&start, |
937 | 0 | buffer + buffer_pos, |
938 | 0 | sign, |
939 | 0 | allow_trailing_junk, |
940 | 0 | junk_string_value_, |
941 | 0 | read_as_double, |
942 | 0 | &result_is_junk); |
943 | 0 | ASSERT(!result_is_junk); |
944 | 0 | *processed_characters_count = static_cast<int>(current - input); |
945 | 0 | return result; |
946 | 0 | } |
947 | 0 | |
948 | 0 | if (nonzero_digit_dropped) { |
949 | 0 | buffer[buffer_pos++] = '1'; |
950 | 0 | exponent--; |
951 | 0 | } |
952 | 0 |
|
953 | 0 | ASSERT(buffer_pos < kBufferSize); |
954 | 0 | buffer[buffer_pos] = '\0'; |
955 | 0 |
|
956 | 0 | double converted; |
957 | 0 | if (read_as_double) { |
958 | 0 | converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); |
959 | 0 | } else { |
960 | 0 | converted = Strtof(Vector<const char>(buffer, buffer_pos), exponent); |
961 | 0 | } |
962 | 0 | *processed_characters_count = static_cast<int>(current - input); |
963 | 0 | return sign? -converted: converted; |
964 | 0 | } Unexecuted instantiation: double icu_62::double_conversion::StringToDoubleConverter::StringToIeee<char const*>(char const*, int, bool, int*) const Unexecuted instantiation: double icu_62::double_conversion::StringToDoubleConverter::StringToIeee<unsigned short const*>(unsigned short const*, int, bool, int*) const |
965 | | |
966 | | |
967 | | double StringToDoubleConverter::StringToDouble( |
968 | | const char* buffer, |
969 | | int length, |
970 | 0 | int* processed_characters_count) const { |
971 | 0 | return StringToIeee(buffer, length, true, processed_characters_count); |
972 | 0 | } |
973 | | |
974 | | |
975 | | double StringToDoubleConverter::StringToDouble( |
976 | | const uc16* buffer, |
977 | | int length, |
978 | 0 | int* processed_characters_count) const { |
979 | 0 | return StringToIeee(buffer, length, true, processed_characters_count); |
980 | 0 | } |
981 | | |
982 | | |
983 | | float StringToDoubleConverter::StringToFloat( |
984 | | const char* buffer, |
985 | | int length, |
986 | 0 | int* processed_characters_count) const { |
987 | 0 | return static_cast<float>(StringToIeee(buffer, length, false, |
988 | 0 | processed_characters_count)); |
989 | 0 | } |
990 | | |
991 | | |
992 | | float StringToDoubleConverter::StringToFloat( |
993 | | const uc16* buffer, |
994 | | int length, |
995 | 0 | int* processed_characters_count) const { |
996 | 0 | return static_cast<float>(StringToIeee(buffer, length, false, |
997 | 0 | processed_characters_count)); |
998 | 0 | } |
999 | | |
1000 | | } // namespace double_conversion |
1001 | | |
1002 | | // ICU PATCH: Close ICU namespace |
1003 | | U_NAMESPACE_END |
1004 | | #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING |