| 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 int. | 6 // VM implementation of int. |
| 7 | 7 |
| 8 patch class int { | 8 patch class int { |
| 9 static int _parse(String str) native "Integer_parse"; | 9 |
| 10 static bool _isWhitespace(int codePoint) { |
| 11 return |
| 12 (codePoint == 32) || // Space. |
| 13 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| 14 } |
| 15 |
| 16 static int _tryParseSmi(String str) { |
| 17 if (str.isEmpty) return null; |
| 18 var ix = 0; |
| 19 var endIx = str.length - 1; |
| 20 // Find first and last non-whitespace. |
| 21 while (ix <= endIx) { |
| 22 if (!_isWhitespace(str.codeUnitAt(ix))) break; |
| 23 ix++; |
| 24 } |
| 25 if (endIx < ix) { |
| 26 return null; // Empty. |
| 27 } |
| 28 while (endIx > ix) { |
| 29 if (!_isWhitespace(str.codeUnitAt(endIx))) break; |
| 30 endIx--; |
| 31 } |
| 32 |
| 33 var isNegative = false; |
| 34 var c = str.codeUnitAt(ix); |
| 35 // Check for leading '+' or '-'. |
| 36 if ((c == 0x2b) || (c == 0x2d)) { |
| 37 ix++; |
| 38 isNegative = (c == 0x2d); |
| 39 if (ix > endIx) { |
| 40 return null; // Empty. |
| 41 } |
| 42 } |
| 43 if ((endIx - ix) >= 9) { |
| 44 return null; // May not fit into a Smi. |
| 45 } |
| 46 var result = 0; |
| 47 for (int i = ix; i <= endIx; i++) { |
| 48 var c = str.codeUnitAt(i) - 0x30; |
| 49 if ((c > 9) || (c < 0)) { |
| 50 return null; |
| 51 } |
| 52 result = result * 10 + c; |
| 53 } |
| 54 return isNegative ? -result : result; |
| 55 } |
| 56 |
| 57 static int _parse(String str) { |
| 58 int res = _tryParseSmi(str); |
| 59 if (res == null) { |
| 60 res = _native_parse(str); |
| 61 } |
| 62 return res; |
| 63 } |
| 64 |
| 65 static int _native_parse(String str) native "Integer_parse"; |
| 10 | 66 |
| 11 static int _throwFormatException(String source) { | 67 static int _throwFormatException(String source) { |
| 12 throw new FormatException(source); | 68 throw new FormatException(source); |
| 13 } | 69 } |
| 14 | 70 |
| 15 /* patch */ static int parse(String source, | 71 /* patch */ static int parse(String source, |
| 16 { int radix, | 72 { int radix, |
| 17 int onError(String str) }) { | 73 int onError(String str) }) { |
| 18 if ((radix == null) && (onError == null)) return _parse(source); | 74 if ((radix == null) && (onError == null)) return _parse(source); |
| 19 return _slowParse(source, radix, onError); | 75 return _slowParse(source, radix, onError); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 int digit = digits[code - 0x30]; | 123 int digit = digits[code - 0x30]; |
| 68 if (digit >= radix) return onError(source); | 124 if (digit >= radix) return onError(source); |
| 69 result = result * radix + digit; | 125 result = result * radix + digit; |
| 70 i++; | 126 i++; |
| 71 if (i == source.length) break; | 127 if (i == source.length) break; |
| 72 code = source.codeUnitAt(i); | 128 code = source.codeUnitAt(i); |
| 73 } while (true); | 129 } while (true); |
| 74 return negative ? -result : result; | 130 return negative ? -result : result; |
| 75 } | 131 } |
| 76 } | 132 } |
| OLD | NEW |