Chromium Code Reviews| 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 |
| 10 static bool _isWhitespace(int codePoint) { | |
| 11 return | |
| 12 (codePoint == 32) || // Space. | |
| 13 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | |
| 14 } | |
| 15 | |
| 16 static int _tryParseSmi(String str) { | |
| 17 if (str.isEmpty) return null; | |
| 18 var ix = 0; | |
| 19 var endIx = str.length - 1; | |
| 20 // Find first and last non-whitespace. | |
| 21 while (ix <= endIx) { | |
| 22 if (!_isWhitespace(str.codeUnitAt(ix))) break; | |
| 23 ix++; | |
| 24 } | |
| 25 if (endIx < ix) { | |
| 26 return null; // Empty. | |
| 27 } | |
| 28 while (ix > endIx) { | |
|
siva
2013/04/24 23:59:00
This check seems inverted here, did you mean
while
srdjan
2013/04/25 17:30:07
Yes, thanks.
| |
| 29 if (!_isWhitespace(str.codeUnitAt(endIx))) break; | |
| 30 endIx--; | |
| 31 } | |
|
siva
2013/04/24 23:59:00
Why not use trim() here to remove the leading and
srdjan
2013/04/25 17:30:07
trim creates a new String if needed. This leads to
| |
| 32 | |
| 33 bool isNegative = false; | |
| 34 var c = str.codeUnitAt(ix); | |
| 35 // Check for leading '+' or '-'. | |
| 36 if ((c == 0x2b) || (c == 0x2d)) { | |
| 37 ix++; | |
| 38 isNegative = (c == 0x2d); | |
| 39 if (ix > endIx) { | |
| 40 return null; // Empty. | |
| 41 } | |
| 42 } | |
| 43 if ((endIx - ix) >= 9) { | |
| 44 return null; // May not fit into a Smi. | |
| 45 } | |
| 46 | |
| 47 int result = 0; | |
|
siva
2013/04/24 23:59:00
var result = 0; ?
Here and above for IsNegative a
| |
| 48 for (int i = ix; i <= endIx; i++) { | |
| 49 var c = str.codeUnitAt(i) - 0x30; | |
| 50 if ((c > 9) || (c < 0)) { | |
| 51 return null; | |
| 52 } | |
| 53 result = result * 10 + c; | |
| 54 } | |
| 55 return isNegative ? -result : result; | |
| 56 } | |
| 57 | |
| 58 static int _parse(String str) { | |
| 59 int res = _tryParseSmi(str); | |
| 60 if (res == null) { | |
| 61 res = _native_parse(str); | |
| 62 } | |
| 63 return res; | |
| 64 } | |
| 65 | |
| 66 static int _native_parse(String str) native "Integer_parse"; | |
| 10 | 67 |
| 11 static int _throwFormatException(String source) { | 68 static int _throwFormatException(String source) { |
| 12 throw new FormatException(source); | 69 throw new FormatException(source); |
| 13 } | 70 } |
| 14 | 71 |
| 15 /* patch */ static int parse(String source, | 72 /* patch */ static int parse(String source, |
| 16 { int radix, | 73 { int radix, |
| 17 int onError(String str) }) { | 74 int onError(String str) }) { |
| 18 if ((radix == null) && (onError == null)) return _parse(source); | 75 if ((radix == null) && (onError == null)) return _parse(source); |
| 19 return _slowParse(source, radix, onError); | 76 return _slowParse(source, radix, onError); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 int digit = digits[code - 0x30]; | 124 int digit = digits[code - 0x30]; |
| 68 if (digit >= radix) return onError(source); | 125 if (digit >= radix) return onError(source); |
| 69 result = result * radix + digit; | 126 result = result * radix + digit; |
| 70 i++; | 127 i++; |
| 71 if (i == source.length) break; | 128 if (i == source.length) break; |
| 72 code = source.codeUnitAt(i); | 129 code = source.codeUnitAt(i); |
| 73 } while (true); | 130 } while (true); |
| 74 return negative ? -result : result; | 131 return negative ? -result : result; |
| 75 } | 132 } |
| 76 } | 133 } |
| OLD | NEW |