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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 static int _parse(String str) { | 60 static int _parse(String str) { |
61 int res = _tryParseSmi(str); | 61 int res = _tryParseSmi(str); |
62 if (res == null) { | 62 if (res == null) { |
63 res = _native_parse(str); | 63 res = _native_parse(str); |
64 } | 64 } |
65 return res; | 65 return res; |
66 } | 66 } |
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, int position) { |
71 throw new FormatException(source); | 71 throw new FormatException("", source, position); |
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) { | 77 if (radix == null) { |
78 int result = _parse(source); | 78 int result = _parse(source); |
79 if (result == null) { | 79 if (result == null) { |
80 if (onError == null) { | 80 if (onError == null) { |
81 throw new FormatException(source); | 81 throw new FormatException("", source); |
82 } | 82 } |
83 return onError(source); | 83 return onError(source); |
84 } | 84 } |
85 return result; | 85 return result; |
86 } | 86 } |
87 return _slowParse(source, radix, onError); | 87 return _slowParse(source, radix, onError); |
88 } | 88 } |
89 | 89 |
90 /* patch */ const factory int.fromEnvironment(String name, | 90 /* patch */ const factory int.fromEnvironment(String name, |
91 {int defaultValue}) | 91 {int defaultValue}) |
92 native "Integer_fromEnvironment"; | 92 native "Integer_fromEnvironment"; |
93 | 93 |
94 static int _slowParse(String source, int radix, int onError(String str)) { | 94 static int _slowParse(String source, int radix, int onError(String str)) { |
95 if (source is! String) throw new ArgumentError(source); | 95 if (source is! String) throw new ArgumentError(source); |
96 if (radix is! int) throw new ArgumentError("Radix is not an integer"); | 96 if (radix is! int) throw new ArgumentError("Radix is not an integer"); |
97 if (radix < 2 || radix > 36) { | 97 if (radix < 2 || radix > 36) { |
98 throw new RangeError("Radix $radix not in range 2..36"); | 98 throw new RangeError("Radix $radix not in range 2..36"); |
99 } | 99 } |
100 if (onError == null) { | |
101 onError = _throwFormatException; | |
102 } | |
103 // Remove leading and trailing white space. | 100 // Remove leading and trailing white space. |
104 source = source.trim(); | 101 int start = source._firstNonWhitespace(); |
105 if (source.isEmpty) return onError(source); | 102 int i = start; |
| 103 if (onError == null) onError = (source) { |
| 104 throw new FormatException("Invalid radix-$radix number", source, i); |
| 105 }; |
| 106 if (start == source.length) return onError(source); |
| 107 int end = source._lastNonWhitespace() + 1; |
106 | 108 |
107 bool negative = false; | 109 bool negative = false; |
108 int result = 0; | 110 int result = 0; |
109 | 111 |
110 // The value 99 is used to represent a non-digit. It is too large to be | 112 // The value 99 is used to represent a non-digit. It is too large to be |
111 // a digit value in any of the used bases. | 113 // a digit value in any of the used bases. |
112 const NA = 99; | 114 const NA = 99; |
113 const List<int> digits = const <int>[ | 115 const List<int> digits = const <int>[ |
114 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, NA, NA, NA, NA, NA, NA, // 0x30 | 116 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, NA, NA, NA, NA, NA, NA, // 0x30 |
115 NA, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40 | 117 NA, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40 |
116 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, NA, NA, NA, NA, NA, // 0x50 | 118 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, NA, NA, NA, NA, NA, // 0x50 |
117 NA, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x60 | 119 NA, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x60 |
118 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, NA, NA, NA, NA, NA, // 0x70 | 120 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, NA, NA, NA, NA, NA, // 0x70 |
119 ]; | 121 ]; |
120 | 122 |
121 int i = 0; | |
122 int code = source.codeUnitAt(i); | 123 int code = source.codeUnitAt(i); |
123 if (code == 0x2d || code == 0x2b) { // Starts with a plus or minus-sign. | 124 if (code == 0x2d || code == 0x2b) { // Starts with a plus or minus-sign. |
124 negative = (code == 0x2d); | 125 negative = (code == 0x2d); |
125 if (source.length == 1) return onError(source); | 126 i++; |
126 i = 1; | 127 if (i == end) return onError(source); |
127 code = source.codeUnitAt(i); | 128 code = source.codeUnitAt(i); |
128 } | 129 } |
129 do { | 130 do { |
130 if (code < 0x30 || code > 0x7f) return onError(source); | 131 if (code < 0x30 || code > 0x7f) return onError(source); |
131 int digit = digits[code - 0x30]; | 132 int digit = digits[code - 0x30]; |
132 if (digit >= radix) return onError(source); | 133 if (digit >= radix) return onError(source); |
133 result = result * radix + digit; | 134 result = result * radix + digit; |
134 i++; | 135 i++; |
135 if (i == source.length) break; | 136 if (i == end) break; |
136 code = source.codeUnitAt(i); | 137 code = source.codeUnitAt(i); |
137 } while (true); | 138 } while (true); |
138 return negative ? -result : result; | 139 return negative ? -result : result; |
139 } | 140 } |
140 } | 141 } |
OLD | NEW |