OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } | 71 } |
72 return false; | 72 return false; |
73 } | 73 } |
74 | 74 |
75 | 75 |
76 void DoubleToStringConverter::CreateExponentialRepresentation( | 76 void DoubleToStringConverter::CreateExponentialRepresentation( |
77 const char* de
cimal_digits, | 77 const char* de
cimal_digits, |
78 int length, | 78 int length, |
79 int exponent, | 79 int exponent, |
80 StringBuilder*
result_builder) const { | 80 StringBuilder*
result_builder) const { |
81 ASSERT(length != 0); | 81 DCHECK_NE(length, 0); |
82 result_builder->AddCharacter(decimal_digits[0]); | 82 result_builder->AddCharacter(decimal_digits[0]); |
83 if (length != 1) { | 83 if (length != 1) { |
84 result_builder->AddCharacter('.'); | 84 result_builder->AddCharacter('.'); |
85 result_builder->AddSubstring(&decimal_digits[1], length-1); | 85 result_builder->AddSubstring(&decimal_digits[1], length-1); |
86 } | 86 } |
87 result_builder->AddCharacter(exponent_character_); | 87 result_builder->AddCharacter(exponent_character_); |
88 if (exponent < 0) { | 88 if (exponent < 0) { |
89 result_builder->AddCharacter('-'); | 89 result_builder->AddCharacter('-'); |
90 exponent = -exponent; | 90 exponent = -exponent; |
91 } else { | 91 } else { |
92 if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) { | 92 if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) { |
93 result_builder->AddCharacter('+'); | 93 result_builder->AddCharacter('+'); |
94 } | 94 } |
95 } | 95 } |
96 if (exponent == 0) { | 96 if (exponent == 0) { |
97 result_builder->AddCharacter('0'); | 97 result_builder->AddCharacter('0'); |
98 return; | 98 return; |
99 } | 99 } |
100 ASSERT(exponent < 1e4); | 100 DCHECK_LT(exponent, 1e4); |
101 const int kMaxExponentLength = 5; | 101 const int kMaxExponentLength = 5; |
102 char buffer[kMaxExponentLength + 1]; | 102 char buffer[kMaxExponentLength + 1]; |
103 int first_char_pos = kMaxExponentLength; | 103 int first_char_pos = kMaxExponentLength; |
104 buffer[first_char_pos] = '\0'; | 104 buffer[first_char_pos] = '\0'; |
105 while (exponent > 0) { | 105 while (exponent > 0) { |
106 buffer[--first_char_pos] = '0' + (exponent % 10); | 106 buffer[--first_char_pos] = '0' + (exponent % 10); |
107 exponent /= 10; | 107 exponent /= 10; |
108 } | 108 } |
109 result_builder->AddSubstring(&buffer[first_char_pos], | 109 result_builder->AddSubstring(&buffer[first_char_pos], |
110 kMaxExponentLength - first_char_pos); | 110 kMaxExponentLength - first_char_pos); |
111 } | 111 } |
112 | 112 |
113 | 113 |
114 void DoubleToStringConverter::CreateDecimalRepresentation( | 114 void DoubleToStringConverter::CreateDecimalRepresentation( |
115 const char* decima
l_digits, | 115 const char* decima
l_digits, |
116 int length, | 116 int length, |
117 int decimal_point, | 117 int decimal_point, |
118 int digits_after_p
oint, | 118 int digits_after_p
oint, |
119 StringBuilder* res
ult_builder) const { | 119 StringBuilder* res
ult_builder) const { |
120 // Create a representation that is padded with zeros if needed. | 120 // Create a representation that is padded with zeros if needed. |
121 if (decimal_point <= 0) { | 121 if (decimal_point <= 0) { |
122 // "0.00000decimal_rep". | 122 // "0.00000decimal_rep". |
123 result_builder->AddCharacter('0'); | 123 result_builder->AddCharacter('0'); |
124 if (digits_after_point > 0) { | 124 if (digits_after_point > 0) { |
125 result_builder->AddCharacter('.'); | 125 result_builder->AddCharacter('.'); |
126 result_builder->AddPadding('0', -decimal_point); | 126 result_builder->AddPadding('0', -decimal_point); |
127 ASSERT(length <= digits_after_point - (-decimal_point)); | 127 DCHECK_LE(length, digits_after_point - (-decimal_point)); |
128 result_builder->AddSubstring(decimal_digits, length); | 128 result_builder->AddSubstring(decimal_digits, length); |
129 int remaining_digits = digits_after_point - (-decimal_point) - l
ength; | 129 int remaining_digits = digits_after_point - (-decimal_point) - l
ength; |
130 result_builder->AddPadding('0', remaining_digits); | 130 result_builder->AddPadding('0', remaining_digits); |
131 } | 131 } |
132 } else if (decimal_point >= length) { | 132 } else if (decimal_point >= length) { |
133 // "decimal_rep0000.00000" or "decimal_rep.0000" | 133 // "decimal_rep0000.00000" or "decimal_rep.0000" |
134 result_builder->AddSubstring(decimal_digits, length); | 134 result_builder->AddSubstring(decimal_digits, length); |
135 result_builder->AddPadding('0', decimal_point - length); | 135 result_builder->AddPadding('0', decimal_point - length); |
136 if (digits_after_point > 0) { | 136 if (digits_after_point > 0) { |
137 result_builder->AddCharacter('.'); | 137 result_builder->AddCharacter('.'); |
138 result_builder->AddPadding('0', digits_after_point); | 138 result_builder->AddPadding('0', digits_after_point); |
139 } | 139 } |
140 } else { | 140 } else { |
141 // "decima.l_rep000" | 141 // "decima.l_rep000" |
142 ASSERT(digits_after_point > 0); | 142 DCHECK_GT(digits_after_point, 0); |
143 result_builder->AddSubstring(decimal_digits, decimal_point); | 143 result_builder->AddSubstring(decimal_digits, decimal_point); |
144 result_builder->AddCharacter('.'); | 144 result_builder->AddCharacter('.'); |
145 ASSERT(length - decimal_point <= digits_after_point); | 145 DCHECK_LE(length - decimal_point, digits_after_point); |
146 result_builder->AddSubstring(&decimal_digits[decimal_point], | 146 result_builder->AddSubstring(&decimal_digits[decimal_point], |
147 length - decimal_point); | 147 length - decimal_point); |
148 int remaining_digits = digits_after_point - (length - decimal_point)
; | 148 int remaining_digits = digits_after_point - (length - decimal_point)
; |
149 result_builder->AddPadding('0', remaining_digits); | 149 result_builder->AddPadding('0', remaining_digits); |
150 } | 150 } |
151 if (digits_after_point == 0) { | 151 if (digits_after_point == 0) { |
152 if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) { | 152 if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) { |
153 result_builder->AddCharacter('.'); | 153 result_builder->AddCharacter('.'); |
154 } | 154 } |
155 if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) { | 155 if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exp
onent, | 190 CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exp
onent, |
191 result_builder); | 191 result_builder); |
192 } | 192 } |
193 return true; | 193 return true; |
194 } | 194 } |
195 | 195 |
196 | 196 |
197 bool DoubleToStringConverter::ToFixed(double value, | 197 bool DoubleToStringConverter::ToFixed(double value, |
198 int requested_digits, | 198 int requested_digits, |
199 StringBuilder* result_builder) const { | 199 StringBuilder* result_builder) const { |
200 ASSERT(kMaxFixedDigitsBeforePoint == 60); | 200 DCHECK_EQ(kMaxFixedDigitsBeforePoint, 60); |
201 const double kFirstNonFixed = 1e60; | 201 const double kFirstNonFixed = 1e60; |
202 | 202 |
203 if (Double(value).IsSpecial()) { | 203 if (Double(value).IsSpecial()) { |
204 return HandleSpecialValues(value, result_builder); | 204 return HandleSpecialValues(value, result_builder); |
205 } | 205 } |
206 | 206 |
207 if (requested_digits > kMaxFixedDigitsAfterPoint) return false; | 207 if (requested_digits > kMaxFixedDigitsAfterPoint) return false; |
208 if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false; | 208 if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false; |
209 | 209 |
210 // Find a sufficiently precise decimal representation of n. | 210 // Find a sufficiently precise decimal representation of n. |
(...skipping 27 matching lines...) Expand all Loading... |
238 return HandleSpecialValues(value, result_builder); | 238 return HandleSpecialValues(value, result_builder); |
239 } | 239 } |
240 | 240 |
241 if (requested_digits < -1) return false; | 241 if (requested_digits < -1) return false; |
242 if (requested_digits > kMaxExponentialDigits) return false; | 242 if (requested_digits > kMaxExponentialDigits) return false; |
243 | 243 |
244 int decimal_point; | 244 int decimal_point; |
245 bool sign; | 245 bool sign; |
246 // Add space for digit before the decimal point and the '\0' character. | 246 // Add space for digit before the decimal point and the '\0' character. |
247 const int kDecimalRepCapacity = kMaxExponentialDigits + 2; | 247 const int kDecimalRepCapacity = kMaxExponentialDigits + 2; |
248 ASSERT(kDecimalRepCapacity > kBase10MaximalLength); | 248 DCHECK_GT(kDecimalRepCapacity, kBase10MaximalLength); |
249 char decimal_rep[kDecimalRepCapacity]; | 249 char decimal_rep[kDecimalRepCapacity]; |
250 int decimal_rep_length; | 250 int decimal_rep_length; |
251 | 251 |
252 if (requested_digits == -1) { | 252 if (requested_digits == -1) { |
253 DoubleToAscii(value, SHORTEST, 0, | 253 DoubleToAscii(value, SHORTEST, 0, |
254 decimal_rep, kDecimalRepCapacity, | 254 decimal_rep, kDecimalRepCapacity, |
255 &sign, &decimal_rep_length, &decimal_point); | 255 &sign, &decimal_rep_length, &decimal_point); |
256 } else { | 256 } else { |
257 DoubleToAscii(value, PRECISION, requested_digits + 1, | 257 DoubleToAscii(value, PRECISION, requested_digits + 1, |
258 decimal_rep, kDecimalRepCapacity, | 258 decimal_rep, kDecimalRepCapacity, |
259 &sign, &decimal_rep_length, &decimal_point); | 259 &sign, &decimal_rep_length, &decimal_point); |
260 ASSERT(decimal_rep_length <= requested_digits + 1); | 260 DCHECK_LE(decimal_rep_length, requested_digits + 1); |
261 | 261 |
262 for (int i = decimal_rep_length; i < requested_digits + 1; ++i) { | 262 for (int i = decimal_rep_length; i < requested_digits + 1; ++i) { |
263 decimal_rep[i] = '0'; | 263 decimal_rep[i] = '0'; |
264 } | 264 } |
265 decimal_rep_length = requested_digits + 1; | 265 decimal_rep_length = requested_digits + 1; |
266 } | 266 } |
267 | 267 |
268 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); | 268 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); |
269 if (sign && (value != 0.0 || !unique_zero)) { | 269 if (sign && (value != 0.0 || !unique_zero)) { |
270 result_builder->AddCharacter('-'); | 270 result_builder->AddCharacter('-'); |
(...skipping 23 matching lines...) Expand all Loading... |
294 int decimal_point; | 294 int decimal_point; |
295 bool sign; | 295 bool sign; |
296 // Add one for the terminating null character. | 296 // Add one for the terminating null character. |
297 const int kDecimalRepCapacity = kMaxPrecisionDigits + 1; | 297 const int kDecimalRepCapacity = kMaxPrecisionDigits + 1; |
298 char decimal_rep[kDecimalRepCapacity]; | 298 char decimal_rep[kDecimalRepCapacity]; |
299 int decimal_rep_length; | 299 int decimal_rep_length; |
300 | 300 |
301 DoubleToAscii(value, PRECISION, precision, | 301 DoubleToAscii(value, PRECISION, precision, |
302 decimal_rep, kDecimalRepCapacity, | 302 decimal_rep, kDecimalRepCapacity, |
303 &sign, &decimal_rep_length, &decimal_point); | 303 &sign, &decimal_rep_length, &decimal_point); |
304 ASSERT(decimal_rep_length <= precision); | 304 DCHECK_LE(decimal_rep_length, precision); |
305 | 305 |
306 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); | 306 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); |
307 if (sign && (value != 0.0 || !unique_zero)) { | 307 if (sign && (value != 0.0 || !unique_zero)) { |
308 result_builder->AddCharacter('-'); | 308 result_builder->AddCharacter('-'); |
309 } | 309 } |
310 | 310 |
311 // The exponent if we print the number as x.xxeyyy. That is with the | 311 // The exponent if we print the number as x.xxeyyy. That is with the |
312 // decimal point after the first digit. | 312 // decimal point after the first digit. |
313 int exponent = decimal_point - 1; | 313 int exponent = decimal_point - 1; |
314 | 314 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 void DoubleToStringConverter::DoubleToAscii(double v, | 352 void DoubleToStringConverter::DoubleToAscii(double v, |
353 DtoaMode mode, | 353 DtoaMode mode, |
354 int requested_digits, | 354 int requested_digits, |
355 char* buffer, | 355 char* buffer, |
356 int buffer_length, | 356 int buffer_length, |
357 bool* sign, | 357 bool* sign, |
358 int* length, | 358 int* length, |
359 int* point) { | 359 int* point) { |
360 Vector<char> vector(buffer, buffer_length); | 360 Vector<char> vector(buffer, buffer_length); |
361 DCHECK(!Double(v).IsSpecial()); | 361 DCHECK(!Double(v).IsSpecial()); |
362 ASSERT(mode == SHORTEST || requested_digits >= 0); | 362 DCHECK(mode == SHORTEST || requested_digits >= 0); |
363 | 363 |
364 if (Double(v).Sign() < 0) { | 364 if (Double(v).Sign() < 0) { |
365 *sign = true; | 365 *sign = true; |
366 v = -v; | 366 v = -v; |
367 } else { | 367 } else { |
368 *sign = false; | 368 *sign = false; |
369 } | 369 } |
370 | 370 |
371 if (mode == PRECISION && requested_digits == 0) { | 371 if (mode == PRECISION && requested_digits == 0) { |
372 vector[0] = '\0'; | 372 vector[0] = '\0'; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 if (current == end) { | 475 if (current == end) { |
476 *processed_characters_count = current - input; | 476 *processed_characters_count = current - input; |
477 return SignedZero(sign); | 477 return SignedZero(sign); |
478 } | 478 } |
479 } | 479 } |
480 } | 480 } |
481 | 481 |
482 // Copy significant digits of the integer part (if any) to the buffer. | 482 // Copy significant digits of the integer part (if any) to the buffer. |
483 while (*current >= '0' && *current <= '9') { | 483 while (*current >= '0' && *current <= '9') { |
484 if (significant_digits < kMaxSignificantDigits) { | 484 if (significant_digits < kMaxSignificantDigits) { |
485 ASSERT(buffer_pos < kBufferSize); | 485 DCHECK_LT(buffer_pos, kBufferSize); |
486 buffer[buffer_pos++] = static_cast<char>(*current); | 486 buffer[buffer_pos++] = static_cast<char>(*current); |
487 significant_digits++; | 487 significant_digits++; |
488 } else { | 488 } else { |
489 insignificant_digits++; // Move the digit into the exponential
part. | 489 insignificant_digits++; // Move the digit into the exponential
part. |
490 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'
; | 490 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'
; |
491 } | 491 } |
492 ++current; | 492 ++current; |
493 if (current == end) goto parsing_done; | 493 if (current == end) goto parsing_done; |
494 } | 494 } |
495 | 495 |
(...skipping 16 matching lines...) Expand all Loading... |
512 *processed_characters_count = current - input; | 512 *processed_characters_count = current - input; |
513 return SignedZero(sign); | 513 return SignedZero(sign); |
514 } | 514 } |
515 exponent--; // Move this 0 into the exponent. | 515 exponent--; // Move this 0 into the exponent. |
516 } | 516 } |
517 } | 517 } |
518 | 518 |
519 // There is a fractional part. | 519 // There is a fractional part. |
520 while (*current >= '0' && *current <= '9') { | 520 while (*current >= '0' && *current <= '9') { |
521 if (significant_digits < kMaxSignificantDigits) { | 521 if (significant_digits < kMaxSignificantDigits) { |
522 ASSERT(buffer_pos < kBufferSize); | 522 DCHECK_LT(buffer_pos, kBufferSize); |
523 buffer[buffer_pos++] = static_cast<char>(*current); | 523 buffer[buffer_pos++] = static_cast<char>(*current); |
524 significant_digits++; | 524 significant_digits++; |
525 exponent--; | 525 exponent--; |
526 } else { | 526 } else { |
527 // Ignore insignificant digits in the fractional part. | 527 // Ignore insignificant digits in the fractional part. |
528 nonzero_digit_dropped = nonzero_digit_dropped || *current !=
'0'; | 528 nonzero_digit_dropped = nonzero_digit_dropped || *current !=
'0'; |
529 } | 529 } |
530 ++current; | 530 ++current; |
531 if (current == end) goto parsing_done; | 531 if (current == end) goto parsing_done; |
532 } | 532 } |
(...skipping 25 matching lines...) Expand all Loading... |
558 } | 558 } |
559 | 559 |
560 if (*current < '0' || *current > '9') { | 560 if (*current < '0' || *current > '9') { |
561 if (sign) | 561 if (sign) |
562 --current; | 562 --current; |
563 --current; | 563 --current; |
564 goto parsing_done; | 564 goto parsing_done; |
565 } | 565 } |
566 | 566 |
567 const int max_exponent = INT_MAX / 2; | 567 const int max_exponent = INT_MAX / 2; |
568 ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2
); | 568 DCHECK_LE(-max_exponent / 2, exponent); |
| 569 DCHECK_LE(exponent, max_exponent / 2); |
569 int num = 0; | 570 int num = 0; |
570 do { | 571 do { |
571 // Check overflow. | 572 // Check overflow. |
572 int digit = *current - '0'; | 573 int digit = *current - '0'; |
573 if (num >= max_exponent / 10 | 574 if (num >= max_exponent / 10 |
574 && !(num == max_exponent / 10 && digit <= max_exponent % 10)
) { | 575 && !(num == max_exponent / 10 && digit <= max_exponent % 10)
) { |
575 num = max_exponent; | 576 num = max_exponent; |
576 } else { | 577 } else { |
577 num = num * 10 + digit; | 578 num = num * 10 + digit; |
578 } | 579 } |
579 ++current; | 580 ++current; |
580 } while (current != end && *current >= '0' && *current <= '9'); | 581 } while (current != end && *current >= '0' && *current <= '9'); |
581 | 582 |
582 exponent += (sign == '-' ? -num : num); | 583 exponent += (sign == '-' ? -num : num); |
583 } | 584 } |
584 | 585 |
585 parsing_done: | 586 parsing_done: |
586 exponent += insignificant_digits; | 587 exponent += insignificant_digits; |
587 | 588 |
588 if (nonzero_digit_dropped) { | 589 if (nonzero_digit_dropped) { |
589 buffer[buffer_pos++] = '1'; | 590 buffer[buffer_pos++] = '1'; |
590 exponent--; | 591 exponent--; |
591 } | 592 } |
592 | 593 |
593 ASSERT(buffer_pos < kBufferSize); | 594 DCHECK_LT(buffer_pos, kBufferSize); |
594 buffer[buffer_pos] = '\0'; | 595 buffer[buffer_pos] = '\0'; |
595 | 596 |
596 double converted = Strtod(Vector<const char>(buffer, buffer_pos), expone
nt); | 597 double converted = Strtod(Vector<const char>(buffer, buffer_pos), expone
nt); |
597 *processed_characters_count = current - input; | 598 *processed_characters_count = current - input; |
598 return sign? -converted: converted; | 599 return sign? -converted: converted; |
599 } | 600 } |
600 | 601 |
601 } // namespace double_conversion | 602 } // namespace double_conversion |
602 | 603 |
603 } // namespace WTF | 604 } // namespace WTF |
OLD | NEW |