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 /* patch */ static int parse(String str) native "Integer_parse"; | 9 static int _parse(String str) native "Integer_parse"; |
| 10 |
| 11 static void _throwFormatException(String source) { |
| 12 throw new FormatException(source); |
| 13 } |
| 14 |
| 15 /* patch */ static int parse(String source, |
| 16 { int radix, |
| 17 int onError(String str) }) { |
| 18 if (source is! String) throw new ArgumentError(source); |
| 19 if (radix == null) { |
| 20 if (onError == null) return _parse(source); |
| 21 try { |
| 22 return _parse(source); |
| 23 } on FormatException { |
| 24 return onError(source); |
| 25 } |
| 26 } |
| 27 if (radix is! int) throw new ArgumentError("Radix is not an integer"); |
| 28 if (radix < 2 || radix > 36) { |
| 29 throw new RangeError("Radix $radix not in range 2..36"); |
| 30 } |
| 31 if (onError == null) { |
| 32 onError = _throwFormatException; |
| 33 } |
| 34 // Remove leading and trailing white space. |
| 35 source = source.trim(); |
| 36 if (source.isEmpty) return onError(source); |
| 37 |
| 38 bool negative = false; |
| 39 int result = 0; |
| 40 |
| 41 // The value 99 is used to represent a non-digit. It is too large to be |
| 42 // a digit value in any of the used bases. |
| 43 const NA = 99; |
| 44 const List<int> digits = const <int>[ |
| 45 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, NA, NA, NA, NA, NA, NA, // 0x30 |
| 46 NA, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40 |
| 47 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, NA, NA, NA, NA, NA, // 0x50 |
| 48 NA, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x60 |
| 49 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, NA, NA, NA, NA, NA, // 0x70 |
| 50 ]; |
| 51 |
| 52 int i = 0; |
| 53 int code = source.charCodeAt(i); |
| 54 if (code == 0x2d || code == 0x2b) { // Starts with a plus or minus-sign. |
| 55 negative = (code == 0x2d); |
| 56 if (source.length == 1) return onError(source); |
| 57 i = 1; |
| 58 code = source.charCodeAt(i); |
| 59 } |
| 60 do { |
| 61 if (code < 0x30 || code > 0x7f) return onError(source); |
| 62 int digit = digits[code - 0x30]; |
| 63 if (digit >= radix) return onError(source); |
| 64 result = result * radix + digit; |
| 65 i++; |
| 66 if (i == source.length) break; |
| 67 code = source.charCodeAt(i); |
| 68 } while (true); |
| 69 return negative ? -result : result; |
| 70 } |
10 } | 71 } |
OLD | NEW |