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 | 9 |
10 static bool _isWhitespace(int codePoint) { | 10 static bool _isWhitespace(int codePoint) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 static int _native_parse(String str) native "Integer_parse"; | 68 static int _native_parse(String str) native "Integer_parse"; |
69 | 69 |
70 static int _throwFormatException(String source) { | 70 static int _throwFormatException(String source) { |
71 throw new FormatException(source); | 71 throw new FormatException(source); |
72 } | 72 } |
73 | 73 |
74 /* patch */ static int parse(String source, | 74 /* patch */ static int parse(String source, |
75 { int radix, | 75 { int radix, |
76 int onError(String str) }) { | 76 int onError(String str) }) { |
77 if ((radix == null) && (onError == null)) return _parse(source); | 77 if (radix == null) { |
| 78 int result = _parse(source); |
| 79 if (result == null) { |
| 80 if (onError == null) { |
| 81 throw new FormatException(source); |
| 82 } |
| 83 return onError(source); |
| 84 } |
| 85 return result; |
| 86 } |
78 return _slowParse(source, radix, onError); | 87 return _slowParse(source, radix, onError); |
79 } | 88 } |
80 | 89 |
81 static int _slowParse(String source, int radix, int onError(String str)) { | 90 static int _slowParse(String source, int radix, int onError(String str)) { |
82 if (source is! String) throw new ArgumentError(source); | 91 if (source is! String) throw new ArgumentError(source); |
83 if (radix == null) { | |
84 assert(onError != null); | |
85 try { | |
86 return _parse(source); | |
87 } on FormatException { | |
88 return onError(source); | |
89 } | |
90 } | |
91 if (radix is! int) throw new ArgumentError("Radix is not an integer"); | 92 if (radix is! int) throw new ArgumentError("Radix is not an integer"); |
92 if (radix < 2 || radix > 36) { | 93 if (radix < 2 || radix > 36) { |
93 throw new RangeError("Radix $radix not in range 2..36"); | 94 throw new RangeError("Radix $radix not in range 2..36"); |
94 } | 95 } |
95 if (onError == null) { | 96 if (onError == null) { |
96 onError = _throwFormatException; | 97 onError = _throwFormatException; |
97 } | 98 } |
98 // Remove leading and trailing white space. | 99 // Remove leading and trailing white space. |
99 source = source.trim(); | 100 source = source.trim(); |
100 if (source.isEmpty) return onError(source); | 101 if (source.isEmpty) return onError(source); |
(...skipping 25 matching lines...) Expand all Loading... |
126 int digit = digits[code - 0x30]; | 127 int digit = digits[code - 0x30]; |
127 if (digit >= radix) return onError(source); | 128 if (digit >= radix) return onError(source); |
128 result = result * radix + digit; | 129 result = result * radix + digit; |
129 i++; | 130 i++; |
130 if (i == source.length) break; | 131 if (i == source.length) break; |
131 code = source.codeUnitAt(i); | 132 code = source.codeUnitAt(i); |
132 } while (true); | 133 } while (true); |
133 return negative ? -result : result; | 134 return negative ? -result : result; |
134 } | 135 } |
135 } | 136 } |
OLD | NEW |