Chromium Code Reviews| 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 _tryParseShortDouble(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 // Added to exponent for each digit. Set to -1 when seeing '.'. | |
| 24 int exponentDelta = 0; | |
| 25 double doubleValue = 0.0; | |
| 26 double sign = 1.0; | |
| 27 int i = start; | |
| 28 int c = str.codeUnitAt(i); | |
| 29 if (c == _MINUS) { | |
| 30 sign = -1.0; | |
| 31 i = start = start + 1; // Use 'start' as start of digits. | |
| 32 if (i == end) return null; | |
| 33 c = str.codeUnitAt(i); | |
| 34 } | |
| 35 if (c == _I) { | |
| 36 if (end == start + 8 && str.startsWith("nfinity", i + 1)) { | |
| 37 return sign * double.INFINITY; | |
| 38 } | |
| 39 return null; | |
| 40 } | |
| 41 if (c == _N) { | |
| 42 if (end == start + 3 && str.codeUnitAt(i + 1) == _a | |
| 43 && str.codeUnitAt(i + 2) == _N) { | |
| 44 return double.NAN; | |
| 45 } | |
| 46 return null; | |
| 47 } | |
| 48 while (true) { | |
| 49 int digit = c ^ _ZERO; // '0'-'9' characters are now 0-9 integers. | |
| 50 if (digit <= 9) { | |
| 51 doubleValue = doubleValue * 10 + digit; | |
| 52 if (doubleValue >= 9007199254740992.0) return null; // Maybe unprecise. | |
| 53 exponent += exponentDelta; | |
| 54 } else if (c == _DOT && exponentDelta == 0) { | |
| 55 exponentDelta = -1; | |
| 56 } else if (c | 0x20 == _e) { | |
| 57 if (i + 1 == end) return null; | |
| 58 int expPart = int._tryParseSmi(str, i + 1, end - 1); | |
| 59 if (expPart == null) return null; | |
| 60 exponent += expPart; | |
| 61 break; | |
| 62 } else { | |
| 63 return null; | |
| 64 } | |
| 65 i++; | |
| 66 if (i == end) break; | |
| 67 c = str.codeUnitAt(i); | |
| 68 } | |
| 69 if (i - start + exponentDelta == 0) return null; // No digits. | |
| 70 if (exponent == 0) return sign * doubleValue; | |
| 71 // Powers of 10 up to 10^22 are exact doubles. | |
| 72 const P10 = const [ | |
| 73 1.0, /* 0 */ | |
| 74 10.0, | |
| 75 100.0, | |
| 76 1000.0, | |
| 77 10000.0, | |
| 78 100000.0, /* 5 */ | |
| 79 1000000.0, | |
| 80 10000000.0, | |
| 81 100000000.0, | |
| 82 1000000000.0, | |
| 83 10000000000.0, /* 10 */ | |
| 84 100000000000.0, | |
| 85 1000000000000.0, | |
| 86 10000000000000.0, | |
| 87 100000000000000.0, | |
| 88 1000000000000000.0, /* 15 */ | |
| 89 10000000000000000.0, | |
| 90 100000000000000000.0, | |
| 91 1000000000000000000.0, | |
| 92 10000000000000000000.0, | |
| 93 100000000000000000000.0, /* 20 */ | |
| 94 1000000000000000000000.0, | |
| 95 10000000000000000000000.0, | |
| 96 ]; | |
| 97 if (exponent < 0) { | |
| 98 if (exponent < -22) return null; | |
| 99 return sign * (doubleValue / P10[-exponent]); | |
| 100 } | |
| 101 if (exponent > 22) return null; | |
| 102 return sign * (doubleValue * P10[exponent]); | |
| 103 } | |
| 104 | |
| 13 static double _parse(var str) { | 105 static double _parse(var str) { |
| 14 int len = str.length; | 106 int len = str.length; |
| 15 int start = str._firstNonWhitespace(); | 107 int start = str._firstNonWhitespace(); |
| 16 if (start == len) return null; // All whitespace. | 108 if (start == len) return null; // All whitespace. |
| 17 int end = str._lastNonWhitespace() + 1; | 109 int end = str._lastNonWhitespace() + 1; |
| 18 assert(start < end); | 110 assert(start < end); |
| 19 | 111 int length = end - start; |
| 112 if (length < 16) { | |
|
floitsch
2014/09/29 14:51:34
Given that exponents don't count in the limit, you
Lasse Reichstein Nielsen
2014/09/30 05:45:30
Ack, I forgot the limit was even there. I have a v
| |
| 113 var result = _tryParseShortDouble(str, start, end); | |
| 114 if (result != null) return result; | |
| 115 } | |
| 20 return _nativeParse(str, start, end); | 116 return _nativeParse(str, start, end); |
| 21 } | 117 } |
| 22 | 118 |
| 23 /* patch */ static double parse(String str, | 119 /* patch */ static double parse(String str, |
| 24 [double onError(String str)]) { | 120 [double onError(String str)]) { |
| 25 var result = _parse(str); | 121 var result = _parse(str); |
| 26 if (result == null) { | 122 if (result == null) { |
| 27 if (onError == null) throw new FormatException("Invalid double", str); | 123 if (onError == null) throw new FormatException("Invalid double", str); |
| 28 return onError(str); | 124 return onError(str); |
| 29 } | 125 } |
| 30 return result; | 126 return result; |
| 31 } | 127 } |
| 32 } | 128 } |
| OLD | NEW |