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