| 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 class double { |
| 9 | 9 |
| 10 static double _nativeParse(String str, | 10 static double _nativeParse(String str, |
| 11 int start, int end) native "Double_parse"; | 11 int start, int end) native "Double_parse"; |
| 12 | 12 |
| 13 static double _tryParseDouble(var str, var start, var end) { |
| 14 assert(start < end); |
| 15 const int _DOT = 0x2e; // '.' |
| 16 const int _ZERO = 0x30; // '0' |
| 17 const int _MINUS = 0x2d; // '-' |
| 18 const int _N = 0x4e; // 'N' |
| 19 const int _a = 0x61; // 'a' |
| 20 const int _I = 0x49; // 'I' |
| 21 const int _e = 0x65; // 'e' |
| 22 int exponent = 0; |
| 23 // Set to non-zero if a digit is seen. Avoids accepting ".". |
| 24 bool digitsSeen = false; |
| 25 // Added to exponent for each digit. Set to -1 when seeing '.'. |
| 26 int exponentDelta = 0; |
| 27 double doubleValue = 0.0; |
| 28 double sign = 1.0; |
| 29 int firstChar = str.codeUnitAt(start); |
| 30 if (firstChar == _MINUS) { |
| 31 sign = -1.0; |
| 32 start++; |
| 33 if (start == end) return null; |
| 34 firstChar = str.codeUnitAt(start); |
| 35 } |
| 36 if (firstChar == _I) { |
| 37 if (end == start + 8 && str.startsWith("nfinity", start + 1)) { |
| 38 return sign * double.INFINITY; |
| 39 } |
| 40 return null; |
| 41 } |
| 42 if (firstChar == _N) { |
| 43 if (end == start + 3 && |
| 44 str.codeUnitAt(start + 1) == _a && |
| 45 str.codeUnitAt(start + 2) == _N) { |
| 46 return double.NAN; |
| 47 } |
| 48 return null; |
| 49 } |
| 50 |
| 51 int firstDigit = firstChar ^ _ZERO; |
| 52 if (firstDigit <= 9) { |
| 53 start++; |
| 54 doubleValue = firstDigit.toDouble(); |
| 55 digitsSeen = true; |
| 56 } |
| 57 for (int i = start; i < end; i++) { |
| 58 int c = str.codeUnitAt(i); |
| 59 int digit = c ^ _ZERO; // '0'-'9' characters are now 0-9 integers. |
| 60 if (digit <= 9) { |
| 61 doubleValue = 10.0 * doubleValue + digit; |
| 62 // Doubles at or above this value (2**53) might have lost precission. |
| 63 const double MAX_EXACT_DOUBLE = 9007199254740992.0; |
| 64 if (doubleValue >= MAX_EXACT_DOUBLE) return null; |
| 65 exponent += exponentDelta; |
| 66 digitsSeen = true; |
| 67 } else if (c == _DOT && exponentDelta == 0) { |
| 68 exponentDelta = -1; |
| 69 } else if ((c | 0x20) == _e) { |
| 70 i++; |
| 71 if (i == end) return null; |
| 72 // int._tryParseSmi treats its end argument as inclusive. |
| 73 int expPart = int._tryParseSmi(str, i, end - 1); |
| 74 if (expPart == null) return null; |
| 75 exponent += expPart; |
| 76 break; |
| 77 } else { |
| 78 return null; |
| 79 } |
| 80 } |
| 81 if (!digitsSeen) return null; // No digits. |
| 82 if (exponent == 0) return sign * doubleValue; |
| 83 // Powers of 10 up to 10^22 are representable as doubles. |
| 84 // Powers of 10 above that are only approximate due to lack of precission. |
| 85 const P10 = const [ |
| 86 1.0, /* 0 */ |
| 87 10.0, |
| 88 100.0, |
| 89 1000.0, |
| 90 10000.0, |
| 91 100000.0, /* 5 */ |
| 92 1000000.0, |
| 93 10000000.0, |
| 94 100000000.0, |
| 95 1000000000.0, |
| 96 10000000000.0, /* 10 */ |
| 97 100000000000.0, |
| 98 1000000000000.0, |
| 99 10000000000000.0, |
| 100 100000000000000.0, |
| 101 1000000000000000.0, /* 15 */ |
| 102 10000000000000000.0, |
| 103 100000000000000000.0, |
| 104 1000000000000000000.0, |
| 105 10000000000000000000.0, |
| 106 100000000000000000000.0, /* 20 */ |
| 107 1000000000000000000000.0, |
| 108 10000000000000000000000.0, |
| 109 ]; |
| 110 if (exponent < 0) { |
| 111 int negExponent = -exponent; |
| 112 if (negExponent >= P10.length) return null; |
| 113 return sign * (doubleValue / P10[negExponent]); |
| 114 } |
| 115 if (exponent > P10.length) return null; |
| 116 return sign * (doubleValue * P10[exponent]); |
| 117 } |
| 118 |
| 13 static double _parse(var str) { | 119 static double _parse(var str) { |
| 14 int len = str.length; | 120 int len = str.length; |
| 15 int start = str._firstNonWhitespace(); | 121 int start = str._firstNonWhitespace(); |
| 16 if (start == len) return null; // All whitespace. | 122 if (start == len) return null; // All whitespace. |
| 17 int end = str._lastNonWhitespace() + 1; | 123 int end = str._lastNonWhitespace() + 1; |
| 18 assert(start < end); | 124 assert(start < end); |
| 19 | 125 var result = _tryParseDouble(str, start, end); |
| 126 if (result != null) return result; |
| 20 return _nativeParse(str, start, end); | 127 return _nativeParse(str, start, end); |
| 21 } | 128 } |
| 22 | 129 |
| 23 /* patch */ static double parse(String str, | 130 /* patch */ static double parse(String str, |
| 24 [double onError(String str)]) { | 131 [double onError(String str)]) { |
| 25 var result = _parse(str); | 132 var result = _parse(str); |
| 26 if (result == null) { | 133 if (result == null) { |
| 27 if (onError == null) throw new FormatException("Invalid double", str); | 134 if (onError == null) throw new FormatException("Invalid double", str); |
| 28 return onError(str); | 135 return onError(str); |
| 29 } | 136 } |
| 30 return result; | 137 return result; |
| 31 } | 138 } |
| 32 } | 139 } |
| OLD | NEW |