| 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 static int _parse(String str) native "Integer_parse"; |
| 10 | 10 |
| 11 static int _throwFormatException(String source) { | 11 static int _throwFormatException(String source) { |
| 12 throw new FormatException(source); | 12 throw new FormatException(source); |
| 13 } | 13 } |
| 14 | 14 |
| 15 /* patch */ static int parse(String source, | 15 /* patch */ static int parse(String source, |
| 16 { int radix, | 16 { int radix, |
| 17 int onError(String str) }) { | 17 int onError(String str) }) { |
| 18 if ((radix == null) && (onError == null)) return _parse(source); |
| 19 return _slowParse(source, radix, onError); |
| 20 } |
| 21 |
| 22 static int _slowParse(String source, int radix, int onError(String str)) { |
| 18 if (source is! String) throw new ArgumentError(source); | 23 if (source is! String) throw new ArgumentError(source); |
| 19 if (radix == null) { | 24 if (radix == null) { |
| 20 if (onError == null) return _parse(source); | 25 assert(onError != null); |
| 21 try { | 26 try { |
| 22 return _parse(source); | 27 return _parse(source); |
| 23 } on FormatException { | 28 } on FormatException { |
| 24 return onError(source); | 29 return onError(source); |
| 25 } | 30 } |
| 26 } | 31 } |
| 27 if (radix is! int) throw new ArgumentError("Radix is not an integer"); | 32 if (radix is! int) throw new ArgumentError("Radix is not an integer"); |
| 28 if (radix < 2 || radix > 36) { | 33 if (radix < 2 || radix > 36) { |
| 29 throw new RangeError("Radix $radix not in range 2..36"); | 34 throw new RangeError("Radix $radix not in range 2..36"); |
| 30 } | 35 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 int digit = digits[code - 0x30]; | 67 int digit = digits[code - 0x30]; |
| 63 if (digit >= radix) return onError(source); | 68 if (digit >= radix) return onError(source); |
| 64 result = result * radix + digit; | 69 result = result * radix + digit; |
| 65 i++; | 70 i++; |
| 66 if (i == source.length) break; | 71 if (i == source.length) break; |
| 67 code = source.codeUnitAt(i); | 72 code = source.codeUnitAt(i); |
| 68 } while (true); | 73 } while (true); |
| 69 return negative ? -result : result; | 74 return negative ? -result : result; |
| 70 } | 75 } |
| 71 } | 76 } |
| OLD | NEW |