Chromium Code Reviews| Index: runtime/lib/integers_patch.dart |
| diff --git a/runtime/lib/integers_patch.dart b/runtime/lib/integers_patch.dart |
| index 7ce5ef5dab482c0d519f47f5421ac0274e58cb32..bd3370e5d563803e03fbc6b5c0e8efb8fd9f98a4 100644 |
| --- a/runtime/lib/integers_patch.dart |
| +++ b/runtime/lib/integers_patch.dart |
| @@ -7,62 +7,50 @@ |
| patch class int { |
| - static bool _isWhitespace(int codePoint) { |
| - return |
| - (codePoint == 32) || // Space. |
| - ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| - } |
| - |
| static bool is64Bit() => 1 << 32 is _Smi; |
| - static int _tryParseSmi(String str) { |
| - if (str.isEmpty) return null; |
| - var ix = 0; |
| - var endIx = str.length - 1; |
| - // Find first and last non-whitespace. |
| - while (ix <= endIx) { |
| - if (!_isWhitespace(str.codeUnitAt(ix))) break; |
| - ix++; |
| - } |
| - if (endIx < ix) { |
| - return null; // Empty. |
| - } |
| - while (endIx > ix) { |
| - if (!_isWhitespace(str.codeUnitAt(endIx))) break; |
| - endIx--; |
| - } |
| - |
| - var isNegative = false; |
| + static int _tryParseSmi(String str, int first, int last) { |
| + assert(first <= last); |
| + var ix = first; |
|
floitsch
2014/07/17 11:35:06
types?
|
| + var sign = 1; |
| var c = str.codeUnitAt(ix); |
|
floitsch
2014/07/17 11:35:06
In theory the VM is able to change str[ix] == "+"
Lasse Reichstein Nielsen
2014/07/18 07:43:09
VM patch files generally don't use local types.
|
| // Check for leading '+' or '-'. |
| if ((c == 0x2b) || (c == 0x2d)) { |
| ix++; |
| - isNegative = (c == 0x2d); |
| - if (ix > endIx) { |
| + sign = -c + 0x2c; |
|
floitsch
2014/07/17 11:35:06
= 0x2c - sign;
Lasse Reichstein Nielsen
2014/07/18 07:43:09
Done.
|
| + if (ix > last) { |
| return null; // Empty. |
| } |
| } |
| int smiLimit = is64Bit() ? 18 : 9; |
| - if ((endIx - ix) >= smiLimit) { |
| + if ((last - ix) >= smiLimit) { |
| return null; // May not fit into a Smi. |
| } |
| var result = 0; |
| - for (int i = ix; i <= endIx; i++) { |
| + for (int i = ix; i <= last; i++) { |
| var c = str.codeUnitAt(i) - 0x30; |
| if ((c > 9) || (c < 0)) { |
| return null; |
| } |
| result = result * 10 + c; |
| } |
| - return isNegative ? -result : result; |
| + return sign * result; |
| } |
| - static int _parse(String str) { |
| - int res = _tryParseSmi(str); |
| - if (res == null) { |
| - res = _native_parse(str); |
| + static int _tryParseSmiWhitespace(String str) { |
| + int first = str._firstNonWhitespace(); |
| + if (first < str.length) { |
| + int last = str._lastNonWhitespace(); |
| + int res = _tryParseSmi(str, first, last); |
| + if (res != null) return res; |
| } |
| - return res; |
| + return _native_parse(str); |
| + } |
| + |
| + static int _parse(String str) { |
| + int res = _tryParseSmi(str, 0, str.length - 1); |
| + if (res != null) return res; |
| + return _tryParseSmiWhitespace(str); |
| } |
| static int _native_parse(String str) native "Integer_parse"; |
| @@ -75,7 +63,8 @@ patch class int { |
| { int radix, |
| int onError(String str) }) { |
| if (radix == null) { |
| - int result = _parse(source); |
| + int result; |
| + if (source.isNotEmpty) result = _parse(source); |
| if (result == null) { |
| if (onError == null) { |
| throw new FormatException("", source); |