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 _native_parse(_OneByteString string) native "Double_parse"; | 10 static double _nativeParse(String str, |
| 11 int start, int end) native "Double_parse"; |
11 | 12 |
12 static double _parse(var str) { | 13 static double _parse(var str) { |
13 str = str.trim(); | 14 int len = str.length; |
| 15 int start = str._firstNonWhitespace(); |
| 16 if (start == len) return null; // All whitespace. |
| 17 int end = str._lastNonWhitespace() + 1; |
| 18 assert(start < end); |
14 | 19 |
15 if (str.length == 0) return null; | 20 return _nativeParse(str, start, end); |
16 | |
17 final ccid = ClassID.getID(str); | |
18 _OneByteString oneByteString; | |
19 // TODO(Srdjan): Allow _ExternalOneByteStrings. | |
20 if (ccid == ClassID.cidOneByteString) { | |
21 oneByteString = str; | |
22 } else { | |
23 int length = str.length; | |
24 var s = _OneByteString._allocate(length); | |
25 for (int i = 0; i < length; i++) { | |
26 int currentUnit = str.codeUnitAt(i); | |
27 // All valid trimmed double strings must be ASCII. | |
28 if (currentUnit < 128) { | |
29 s._setAt(i, currentUnit); | |
30 } else { | |
31 return null; | |
32 } | |
33 } | |
34 oneByteString = s; | |
35 } | |
36 | |
37 return _native_parse(oneByteString); | |
38 } | 21 } |
39 | 22 |
40 /* patch */ static double parse(String str, | 23 /* patch */ static double parse(String str, |
41 [double onError(String str)]) { | 24 [double onError(String str)]) { |
42 var result = _parse(str); | 25 var result = _parse(str); |
43 if (result == null) { | 26 if (result == null) { |
44 if (onError == null) throw new FormatException(str); | 27 if (onError == null) throw new FormatException(str); |
45 return onError(str); | 28 return onError(str); |
46 } | 29 } |
47 return result; | 30 return result; |
48 } | 31 } |
49 } | 32 } |
OLD | NEW |