OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Dart core library. | 4 // Dart core library. |
5 | 5 |
6 // VM implementation of double. | 6 // VM implementation of double. |
7 | 7 |
8 @patch class double { | 8 @patch |
9 | 9 class double { |
10 static double _nativeParse(String str, | 10 static double _nativeParse(String str, int start, int end) |
11 int start, int end) native "Double_parse"; | 11 native "Double_parse"; |
12 | 12 |
13 static double _tryParseDouble(var str, var start, var end) { | 13 static double _tryParseDouble(var str, var start, var end) { |
14 assert(start < end); | 14 assert(start < end); |
15 const int _DOT = 0x2e; // '.' | 15 const int _DOT = 0x2e; // '.' |
16 const int _ZERO = 0x30; // '0' | 16 const int _ZERO = 0x30; // '0' |
17 const int _MINUS = 0x2d; // '-' | 17 const int _MINUS = 0x2d; // '-' |
18 const int _N = 0x4e; // 'N' | 18 const int _N = 0x4e; // 'N' |
19 const int _a = 0x61; // 'a' | 19 const int _a = 0x61; // 'a' |
20 const int _I = 0x49; // 'I' | 20 const int _I = 0x49; // 'I' |
21 const int _e = 0x65; // 'e' | 21 const int _e = 0x65; // 'e' |
22 int exponent = 0; | 22 int exponent = 0; |
23 // Set to non-zero if a digit is seen. Avoids accepting ".". | 23 // Set to non-zero if a digit is seen. Avoids accepting ".". |
24 bool digitsSeen = false; | 24 bool digitsSeen = false; |
25 // Added to exponent for each digit. Set to -1 when seeing '.'. | 25 // Added to exponent for each digit. Set to -1 when seeing '.'. |
26 int exponentDelta = 0; | 26 int exponentDelta = 0; |
27 double doubleValue = 0.0; | 27 double doubleValue = 0.0; |
28 double sign = 1.0; | 28 double sign = 1.0; |
29 int firstChar = str.codeUnitAt(start); | 29 int firstChar = str.codeUnitAt(start); |
30 if (firstChar == _MINUS) { | 30 if (firstChar == _MINUS) { |
31 sign = -1.0; | 31 sign = -1.0; |
(...skipping 17 matching lines...) Expand all Loading... |
49 } | 49 } |
50 | 50 |
51 int firstDigit = firstChar ^ _ZERO; | 51 int firstDigit = firstChar ^ _ZERO; |
52 if (firstDigit <= 9) { | 52 if (firstDigit <= 9) { |
53 start++; | 53 start++; |
54 doubleValue = firstDigit.toDouble(); | 54 doubleValue = firstDigit.toDouble(); |
55 digitsSeen = true; | 55 digitsSeen = true; |
56 } | 56 } |
57 for (int i = start; i < end; i++) { | 57 for (int i = start; i < end; i++) { |
58 int c = str.codeUnitAt(i); | 58 int c = str.codeUnitAt(i); |
59 int digit = c ^ _ZERO; // '0'-'9' characters are now 0-9 integers. | 59 int digit = c ^ _ZERO; // '0'-'9' characters are now 0-9 integers. |
60 if (digit <= 9) { | 60 if (digit <= 9) { |
61 doubleValue = 10.0 * doubleValue + digit; | 61 doubleValue = 10.0 * doubleValue + digit; |
62 // Doubles at or above this value (2**53) might have lost precission. | 62 // Doubles at or above this value (2**53) might have lost precission. |
63 const double MAX_EXACT_DOUBLE = 9007199254740992.0; | 63 const double MAX_EXACT_DOUBLE = 9007199254740992.0; |
64 if (doubleValue >= MAX_EXACT_DOUBLE) return null; | 64 if (doubleValue >= MAX_EXACT_DOUBLE) return null; |
65 exponent += exponentDelta; | 65 exponent += exponentDelta; |
66 digitsSeen = true; | 66 digitsSeen = true; |
67 } else if (c == _DOT && exponentDelta == 0) { | 67 } else if (c == _DOT && exponentDelta == 0) { |
68 exponentDelta = -1; | 68 exponentDelta = -1; |
69 } else if ((c | 0x20) == _e) { | 69 } else if ((c | 0x20) == _e) { |
70 i++; | 70 i++; |
71 if (i == end) return null; | 71 if (i == end) return null; |
72 // int._tryParseSmi treats its end argument as inclusive. | 72 // int._tryParseSmi treats its end argument as inclusive. |
73 int expPart = int._tryParseSmi(str, i, end - 1); | 73 int expPart = int._tryParseSmi(str, i, end - 1); |
74 if (expPart == null) return null; | 74 if (expPart == null) return null; |
75 exponent += expPart; | 75 exponent += expPart; |
76 break; | 76 break; |
77 } else { | 77 } else { |
78 return null; | 78 return null; |
79 } | 79 } |
80 } | 80 } |
81 if (!digitsSeen) return null; // No digits. | 81 if (!digitsSeen) return null; // No digits. |
82 if (exponent == 0) return sign * doubleValue; | 82 if (exponent == 0) return sign * doubleValue; |
83 const P10 = POWERS_OF_TEN; // From shared library | 83 const P10 = POWERS_OF_TEN; // From shared library |
84 if (exponent < 0) { | 84 if (exponent < 0) { |
85 int negExponent = -exponent; | 85 int negExponent = -exponent; |
86 if (negExponent >= P10.length) return null; | 86 if (negExponent >= P10.length) return null; |
87 return sign * (doubleValue / P10[negExponent]); | 87 return sign * (doubleValue / P10[negExponent]); |
88 } | 88 } |
89 if (exponent >= P10.length) return null; | 89 if (exponent >= P10.length) return null; |
90 return sign * (doubleValue * P10[exponent]); | 90 return sign * (doubleValue * P10[exponent]); |
91 } | 91 } |
92 | 92 |
93 static double _parse(var str) { | 93 static double _parse(var str) { |
94 int len = str.length; | 94 int len = str.length; |
95 int start = str._firstNonWhitespace(); | 95 int start = str._firstNonWhitespace(); |
96 if (start == len) return null; // All whitespace. | 96 if (start == len) return null; // All whitespace. |
97 int end = str._lastNonWhitespace() + 1; | 97 int end = str._lastNonWhitespace() + 1; |
98 assert(start < end); | 98 assert(start < end); |
99 var result = _tryParseDouble(str, start, end); | 99 var result = _tryParseDouble(str, start, end); |
100 if (result != null) return result; | 100 if (result != null) return result; |
101 return _nativeParse(str, start, end); | 101 return _nativeParse(str, start, end); |
102 } | 102 } |
103 | 103 |
104 @patch | 104 @patch |
105 static double parse(String source, [double onError(String source)]) { | 105 static double parse(String source, [double onError(String source)]) { |
106 var result = _parse(source); | 106 var result = _parse(source); |
107 if (result == null) { | 107 if (result == null) { |
108 if (onError == null) throw new FormatException("Invalid double", source); | 108 if (onError == null) throw new FormatException("Invalid double", source); |
109 return onError(source); | 109 return onError(source); |
110 } | 110 } |
111 return result; | 111 return result; |
112 } | 112 } |
113 } | 113 } |
OLD | NEW |