OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 return c - 'a' + 10; | 43 return c - 'a' + 10; |
44 if ('A' <= c && c <= 'F') | 44 if ('A' <= c && c <= 'F') |
45 return c - 'A' + 10; | 45 return c - 'A' + 10; |
46 return -1; | 46 return -1; |
47 } | 47 } |
48 | 48 |
49 | 49 |
50 // Provide a common interface to getting a character at a certain | 50 // Provide a common interface to getting a character at a certain |
51 // index from a char* or a String object. | 51 // index from a char* or a String object. |
52 static inline int GetChar(const char* str, int index) { | 52 static inline int GetChar(const char* str, int index) { |
53 ASSERT(index >= 0 && index < static_cast<int>(strlen(str))); | 53 ASSERT(index >= 0 && index < StrLength(str)); |
54 return str[index]; | 54 return str[index]; |
55 } | 55 } |
56 | 56 |
57 | 57 |
58 static inline int GetChar(String* str, int index) { | 58 static inline int GetChar(String* str, int index) { |
59 return str->Get(index); | 59 return str->Get(index); |
60 } | 60 } |
61 | 61 |
62 | 62 |
63 static inline int GetLength(const char* str) { | 63 static inline int GetLength(const char* str) { |
64 return strlen(str); | 64 return StrLength(str); |
65 } | 65 } |
66 | 66 |
67 | 67 |
68 static inline int GetLength(String* str) { | 68 static inline int GetLength(String* str) { |
69 return str->length(); | 69 return str->length(); |
70 } | 70 } |
71 | 71 |
72 | 72 |
73 static inline const char* GetCString(const char* str, int index) { | 73 static inline const char* GetCString(const char* str, int index) { |
74 return str + index; | 74 return str + index; |
(...skipping 19 matching lines...) Expand all Loading... |
94 static inline void ReleaseCString(const char* original, const char* str) { | 94 static inline void ReleaseCString(const char* original, const char* str) { |
95 } | 95 } |
96 | 96 |
97 | 97 |
98 static inline void ReleaseCString(String* original, const char* str) { | 98 static inline void ReleaseCString(String* original, const char* str) { |
99 DeleteArray(const_cast<char *>(str)); | 99 DeleteArray(const_cast<char *>(str)); |
100 } | 100 } |
101 | 101 |
102 | 102 |
103 static inline bool IsSpace(const char* str, int index) { | 103 static inline bool IsSpace(const char* str, int index) { |
104 ASSERT(index >= 0 && index < static_cast<int>(strlen(str))); | 104 ASSERT(index >= 0 && index < StrLength(str)); |
105 return Scanner::kIsWhiteSpace.get(str[index]); | 105 return Scanner::kIsWhiteSpace.get(str[index]); |
106 } | 106 } |
107 | 107 |
108 | 108 |
109 static inline bool IsSpace(String* str, int index) { | 109 static inline bool IsSpace(String* str, int index) { |
110 return Scanner::kIsWhiteSpace.get(str->Get(index)); | 110 return Scanner::kIsWhiteSpace.get(str->Get(index)); |
111 } | 111 } |
112 | 112 |
113 | 113 |
114 static inline bool SubStringEquals(const char* str, | 114 static inline bool SubStringEquals(const char* str, |
115 int index, | 115 int index, |
116 const char* other) { | 116 const char* other) { |
117 return strncmp(str + index, other, strlen(other)) != 0; | 117 return strncmp(str + index, other, strlen(other)) != 0; |
118 } | 118 } |
119 | 119 |
120 | 120 |
121 static inline bool SubStringEquals(String* str, int index, const char* other) { | 121 static inline bool SubStringEquals(String* str, int index, const char* other) { |
122 HandleScope scope; | 122 HandleScope scope; |
123 int str_length = str->length(); | 123 int str_length = str->length(); |
124 int other_length = strlen(other); | 124 int other_length = StrLength(other); |
125 int end = index + other_length < str_length ? | 125 int end = index + other_length < str_length ? |
126 index + other_length : | 126 index + other_length : |
127 str_length; | 127 str_length; |
128 Handle<String> substring = | 128 Handle<String> substring = |
129 Factory::NewSubString(Handle<String>(str), index, end); | 129 Factory::NewSubString(Handle<String>(str), index, end); |
130 return substring->IsEqualTo(Vector<const char>(other, other_length)); | 130 return substring->IsEqualTo(Vector<const char>(other, other_length)); |
131 } | 131 } |
132 | 132 |
133 | 133 |
134 // Check if a string should be parsed as an octal number. The string | 134 // Check if a string should be parsed as an octal number. The string |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 index = StringToInt(str, index, 8, &result); | 312 index = StringToInt(str, index, 8, &result); |
313 } else { | 313 } else { |
314 const char* cstr = GetCString(str, index); | 314 const char* cstr = GetCString(str, index); |
315 const char* end; | 315 const char* end; |
316 // Optimistically parse the number and then, if that fails, | 316 // Optimistically parse the number and then, if that fails, |
317 // check if it might have been {+,-,}Infinity. | 317 // check if it might have been {+,-,}Infinity. |
318 result = gay_strtod(cstr, &end); | 318 result = gay_strtod(cstr, &end); |
319 ReleaseCString(str, cstr); | 319 ReleaseCString(str, cstr); |
320 if (result != 0.0 || end != cstr) { | 320 if (result != 0.0 || end != cstr) { |
321 // It appears that strtod worked | 321 // It appears that strtod worked |
322 index += end - cstr; | 322 index += static_cast<int>(end - cstr); |
323 } else { | 323 } else { |
324 // Check for {+,-,}Infinity | 324 // Check for {+,-,}Infinity |
325 bool is_negative = (GetChar(str, index) == '-'); | 325 bool is_negative = (GetChar(str, index) == '-'); |
326 if (GetChar(str, index) == '+' || GetChar(str, index) == '-') | 326 if (GetChar(str, index) == '+' || GetChar(str, index) == '-') |
327 index++; | 327 index++; |
328 if (!SubStringEquals(str, index, "Infinity")) | 328 if (!SubStringEquals(str, index, "Infinity")) |
329 return JUNK_STRING_VALUE; | 329 return JUNK_STRING_VALUE; |
330 result = is_negative ? -V8_INFINITY : V8_INFINITY; | 330 result = is_negative ? -V8_INFINITY : V8_INFINITY; |
331 index += 8; | 331 index += 8; |
332 } | 332 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 376 |
377 case FP_ZERO: | 377 case FP_ZERO: |
378 builder.AddCharacter('0'); | 378 builder.AddCharacter('0'); |
379 break; | 379 break; |
380 | 380 |
381 default: { | 381 default: { |
382 int decimal_point; | 382 int decimal_point; |
383 int sign; | 383 int sign; |
384 | 384 |
385 char* decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); | 385 char* decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); |
386 int length = strlen(decimal_rep); | 386 int length = StrLength(decimal_rep); |
387 | 387 |
388 if (sign) builder.AddCharacter('-'); | 388 if (sign) builder.AddCharacter('-'); |
389 | 389 |
390 if (length <= decimal_point && decimal_point <= 21) { | 390 if (length <= decimal_point && decimal_point <= 21) { |
391 // ECMA-262 section 9.8.1 step 6. | 391 // ECMA-262 section 9.8.1 step 6. |
392 builder.AddString(decimal_rep); | 392 builder.AddString(decimal_rep); |
393 builder.AddPadding('0', decimal_point - length); | 393 builder.AddPadding('0', decimal_point - length); |
394 | 394 |
395 } else if (0 < decimal_point && decimal_point <= 21) { | 395 } else if (0 < decimal_point && decimal_point <= 21) { |
396 // ECMA-262 section 9.8.1 step 7. | 396 // ECMA-262 section 9.8.1 step 7. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 if (abs_value >= 1e21) { | 458 if (abs_value >= 1e21) { |
459 char arr[100]; | 459 char arr[100]; |
460 Vector<char> buffer(arr, ARRAY_SIZE(arr)); | 460 Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
461 return StrDup(DoubleToCString(value, buffer)); | 461 return StrDup(DoubleToCString(value, buffer)); |
462 } | 462 } |
463 | 463 |
464 // Find a sufficiently precise decimal representation of n. | 464 // Find a sufficiently precise decimal representation of n. |
465 int decimal_point; | 465 int decimal_point; |
466 int sign; | 466 int sign; |
467 char* decimal_rep = dtoa(abs_value, 3, f, &decimal_point, &sign, NULL); | 467 char* decimal_rep = dtoa(abs_value, 3, f, &decimal_point, &sign, NULL); |
468 int decimal_rep_length = strlen(decimal_rep); | 468 int decimal_rep_length = StrLength(decimal_rep); |
469 | 469 |
470 // Create a representation that is padded with zeros if needed. | 470 // Create a representation that is padded with zeros if needed. |
471 int zero_prefix_length = 0; | 471 int zero_prefix_length = 0; |
472 int zero_postfix_length = 0; | 472 int zero_postfix_length = 0; |
473 | 473 |
474 if (decimal_point <= 0) { | 474 if (decimal_point <= 0) { |
475 zero_prefix_length = -decimal_point + 1; | 475 zero_prefix_length = -decimal_point + 1; |
476 decimal_point = 1; | 476 decimal_point = 1; |
477 } | 477 } |
478 | 478 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 // letter 'e', a minus or a plus depending on the exponent, and a | 519 // letter 'e', a minus or a plus depending on the exponent, and a |
520 // three digit exponent. | 520 // three digit exponent. |
521 unsigned result_size = significant_digits + 7; | 521 unsigned result_size = significant_digits + 7; |
522 StringBuilder builder(result_size + 1); | 522 StringBuilder builder(result_size + 1); |
523 | 523 |
524 if (negative) builder.AddCharacter('-'); | 524 if (negative) builder.AddCharacter('-'); |
525 builder.AddCharacter(decimal_rep[0]); | 525 builder.AddCharacter(decimal_rep[0]); |
526 if (significant_digits != 1) { | 526 if (significant_digits != 1) { |
527 builder.AddCharacter('.'); | 527 builder.AddCharacter('.'); |
528 builder.AddString(decimal_rep + 1); | 528 builder.AddString(decimal_rep + 1); |
529 builder.AddPadding('0', significant_digits - strlen(decimal_rep)); | 529 int rep_length = StrLength(decimal_rep); |
| 530 builder.AddPadding('0', significant_digits - rep_length); |
530 } | 531 } |
531 | 532 |
532 builder.AddCharacter('e'); | 533 builder.AddCharacter('e'); |
533 builder.AddCharacter(negative_exponent ? '-' : '+'); | 534 builder.AddCharacter(negative_exponent ? '-' : '+'); |
534 builder.AddFormatted("%d", exponent); | 535 builder.AddFormatted("%d", exponent); |
535 return builder.Finalize(); | 536 return builder.Finalize(); |
536 } | 537 } |
537 | 538 |
538 | 539 |
539 | 540 |
540 char* DoubleToExponentialCString(double value, int f) { | 541 char* DoubleToExponentialCString(double value, int f) { |
541 // f might be -1 to signal that f was undefined in JavaScript. | 542 // f might be -1 to signal that f was undefined in JavaScript. |
542 ASSERT(f >= -1 && f <= 20); | 543 ASSERT(f >= -1 && f <= 20); |
543 | 544 |
544 bool negative = false; | 545 bool negative = false; |
545 if (value < 0) { | 546 if (value < 0) { |
546 value = -value; | 547 value = -value; |
547 negative = true; | 548 negative = true; |
548 } | 549 } |
549 | 550 |
550 // Find a sufficiently precise decimal representation of n. | 551 // Find a sufficiently precise decimal representation of n. |
551 int decimal_point; | 552 int decimal_point; |
552 int sign; | 553 int sign; |
553 char* decimal_rep = NULL; | 554 char* decimal_rep = NULL; |
554 if (f == -1) { | 555 if (f == -1) { |
555 decimal_rep = dtoa(value, 0, 0, &decimal_point, &sign, NULL); | 556 decimal_rep = dtoa(value, 0, 0, &decimal_point, &sign, NULL); |
556 f = strlen(decimal_rep) - 1; | 557 f = StrLength(decimal_rep) - 1; |
557 } else { | 558 } else { |
558 decimal_rep = dtoa(value, 2, f + 1, &decimal_point, &sign, NULL); | 559 decimal_rep = dtoa(value, 2, f + 1, &decimal_point, &sign, NULL); |
559 } | 560 } |
560 int decimal_rep_length = strlen(decimal_rep); | 561 int decimal_rep_length = StrLength(decimal_rep); |
561 ASSERT(decimal_rep_length > 0); | 562 ASSERT(decimal_rep_length > 0); |
562 ASSERT(decimal_rep_length <= f + 1); | 563 ASSERT(decimal_rep_length <= f + 1); |
563 USE(decimal_rep_length); | 564 USE(decimal_rep_length); |
564 | 565 |
565 int exponent = decimal_point - 1; | 566 int exponent = decimal_point - 1; |
566 char* result = | 567 char* result = |
567 CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1); | 568 CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1); |
568 | 569 |
569 freedtoa(decimal_rep); | 570 freedtoa(decimal_rep); |
570 | 571 |
571 return result; | 572 return result; |
572 } | 573 } |
573 | 574 |
574 | 575 |
575 char* DoubleToPrecisionCString(double value, int p) { | 576 char* DoubleToPrecisionCString(double value, int p) { |
576 ASSERT(p >= 1 && p <= 21); | 577 ASSERT(p >= 1 && p <= 21); |
577 | 578 |
578 bool negative = false; | 579 bool negative = false; |
579 if (value < 0) { | 580 if (value < 0) { |
580 value = -value; | 581 value = -value; |
581 negative = true; | 582 negative = true; |
582 } | 583 } |
583 | 584 |
584 // Find a sufficiently precise decimal representation of n. | 585 // Find a sufficiently precise decimal representation of n. |
585 int decimal_point; | 586 int decimal_point; |
586 int sign; | 587 int sign; |
587 char* decimal_rep = dtoa(value, 2, p, &decimal_point, &sign, NULL); | 588 char* decimal_rep = dtoa(value, 2, p, &decimal_point, &sign, NULL); |
588 int decimal_rep_length = strlen(decimal_rep); | 589 int decimal_rep_length = StrLength(decimal_rep); |
589 ASSERT(decimal_rep_length <= p); | 590 ASSERT(decimal_rep_length <= p); |
590 | 591 |
591 int exponent = decimal_point - 1; | 592 int exponent = decimal_point - 1; |
592 | 593 |
593 char* result = NULL; | 594 char* result = NULL; |
594 | 595 |
595 if (exponent < -6 || exponent >= p) { | 596 if (exponent < -6 || exponent >= p) { |
596 result = | 597 result = |
597 CreateExponentialRepresentation(decimal_rep, exponent, negative, p); | 598 CreateExponentialRepresentation(decimal_rep, exponent, negative, p); |
598 } else { | 599 } else { |
(...skipping 13 matching lines...) Expand all Loading... |
612 builder.AddString(decimal_rep); | 613 builder.AddString(decimal_rep); |
613 builder.AddPadding('0', p - decimal_rep_length); | 614 builder.AddPadding('0', p - decimal_rep_length); |
614 } else { | 615 } else { |
615 const int m = Min(decimal_rep_length, decimal_point); | 616 const int m = Min(decimal_rep_length, decimal_point); |
616 builder.AddSubstring(decimal_rep, m); | 617 builder.AddSubstring(decimal_rep, m); |
617 builder.AddPadding('0', decimal_point - decimal_rep_length); | 618 builder.AddPadding('0', decimal_point - decimal_rep_length); |
618 if (decimal_point < p) { | 619 if (decimal_point < p) { |
619 builder.AddCharacter('.'); | 620 builder.AddCharacter('.'); |
620 const int extra = negative ? 2 : 1; | 621 const int extra = negative ? 2 : 1; |
621 if (decimal_rep_length > decimal_point) { | 622 if (decimal_rep_length > decimal_point) { |
622 const int len = strlen(decimal_rep + decimal_point); | 623 const int len = StrLength(decimal_rep + decimal_point); |
623 const int n = Min(len, p - (builder.position() - extra)); | 624 const int n = Min(len, p - (builder.position() - extra)); |
624 builder.AddSubstring(decimal_rep + decimal_point, n); | 625 builder.AddSubstring(decimal_rep + decimal_point, n); |
625 } | 626 } |
626 builder.AddPadding('0', extra + (p - builder.position())); | 627 builder.AddPadding('0', extra + (p - builder.position())); |
627 } | 628 } |
628 } | 629 } |
629 result = builder.Finalize(); | 630 result = builder.Finalize(); |
630 } | 631 } |
631 | 632 |
632 freedtoa(decimal_rep); | 633 freedtoa(decimal_rep); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 // Allocate result and fill in the parts. | 700 // Allocate result and fill in the parts. |
700 StringBuilder builder(result_size + 1); | 701 StringBuilder builder(result_size + 1); |
701 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 702 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
702 if (decimal_pos > 0) builder.AddCharacter('.'); | 703 if (decimal_pos > 0) builder.AddCharacter('.'); |
703 builder.AddSubstring(decimal_buffer, decimal_pos); | 704 builder.AddSubstring(decimal_buffer, decimal_pos); |
704 return builder.Finalize(); | 705 return builder.Finalize(); |
705 } | 706 } |
706 | 707 |
707 | 708 |
708 } } // namespace v8::internal | 709 } } // namespace v8::internal |
OLD | NEW |