| 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 import 'dart:_internal' as internal; |
| 9 |
| 8 patch class int { | 10 patch class int { |
| 9 | 11 |
| 10 /* patch */ const factory int.fromEnvironment(String name, | 12 /* patch */ const factory int.fromEnvironment(String name, |
| 11 {int defaultValue}) | 13 {int defaultValue}) |
| 12 native "Integer_fromEnvironment"; | 14 native "Integer_fromEnvironment"; |
| 13 | 15 |
| 14 static bool is64Bit() => 1 << 32 is _Smi; | |
| 15 | 16 |
| 16 static int _tryParseSmi(String str, int first, int last) { | 17 static int _tryParseSmi(String str, int first, int last) { |
| 17 assert(first <= last); | 18 assert(first <= last); |
| 18 var ix = first; | 19 var ix = first; |
| 19 var sign = 1; | 20 var sign = 1; |
| 20 var c = str.codeUnitAt(ix); | 21 var c = str.codeUnitAt(ix); |
| 21 // Check for leading '+' or '-'. | 22 // Check for leading '+' or '-'. |
| 22 if ((c == 0x2b) || (c == 0x2d)) { | 23 if ((c == 0x2b) || (c == 0x2d)) { |
| 23 ix++; | 24 ix++; |
| 24 sign = 0x2c - c; // -1 for '-', +1 for '+'. | 25 sign = 0x2c - c; // -1 for '-', +1 for '+'. |
| 25 if (ix > last) { | 26 if (ix > last) { |
| 26 return null; // Empty. | 27 return null; // Empty. |
| 27 } | 28 } |
| 28 } | 29 } |
| 29 var smiLimit = is64Bit() ? 18 : 9; | 30 var smiLimit = internal.is64Bit ? 18 : 9; |
| 30 if ((last - ix) >= smiLimit) { | 31 if ((last - ix) >= smiLimit) { |
| 31 return null; // May not fit into a Smi. | 32 return null; // May not fit into a Smi. |
| 32 } | 33 } |
| 33 var result = 0; | 34 var result = 0; |
| 34 for (int i = ix; i <= last; i++) { | 35 for (int i = ix; i <= last; i++) { |
| 35 var c = 0x30 ^ str.codeUnitAt(i); | 36 var c = 0x30 ^ str.codeUnitAt(i); |
| 36 if (9 < c) { | 37 if (9 < c) { |
| 37 return null; | 38 return null; |
| 38 } | 39 } |
| 39 result = 10 * result + c; | 40 result = 10 * result + c; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 static int _throwFormatException(onError, source, index, radix) { | 106 static int _throwFormatException(onError, source, index, radix) { |
| 106 if (onError != null) return onError(source); | 107 if (onError != null) return onError(source); |
| 107 if (radix == null) { | 108 if (radix == null) { |
| 108 throw new FormatException("Invalid number", source, index); | 109 throw new FormatException("Invalid number", source, index); |
| 109 } | 110 } |
| 110 throw new FormatException("Invalid radix-$radix number", source, index); | 111 throw new FormatException("Invalid radix-$radix number", source, index); |
| 111 } | 112 } |
| 112 | 113 |
| 113 static int _parseRadix(String source, int radix, | 114 static int _parseRadix(String source, int radix, |
| 114 int start, int end, int sign) { | 115 int start, int end, int sign) { |
| 115 int tableIndex = (radix - 2) * 4 + (int.is64Bit() ? 2 : 0); | 116 int tableIndex = (radix - 2) * 4 + (internal.is64Bit ? 2 : 0); |
| 116 int blockSize = _PARSE_LIMITS[tableIndex]; | 117 int blockSize = _PARSE_LIMITS[tableIndex]; |
| 117 int length = end - start; | 118 int length = end - start; |
| 118 if (length <= blockSize) { | 119 if (length <= blockSize) { |
| 119 _Smi smi = _parseBlock(source, radix, start, end); | 120 _Smi smi = _parseBlock(source, radix, start, end); |
| 120 if (smi != null) return sign * smi; | 121 if (smi != null) return sign * smi; |
| 121 return null; | 122 return null; |
| 122 } | 123 } |
| 123 | 124 |
| 124 // Often cheaper than: int smallBlockSize = length % blockSize; | 125 // Often cheaper than: int smallBlockSize = length % blockSize; |
| 125 // because digit count generally tends towards smaller. rather | 126 // because digit count generally tends towards smaller. rather |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 6, 594823321, 12, 353814783205469041, | 204 6, 594823321, 12, 353814783205469041, |
| 204 6, 729000000, 12, 531441000000000000, /* radix: 30 */ | 205 6, 729000000, 12, 531441000000000000, /* radix: 30 */ |
| 205 6, 887503681, 12, 787662783788549761, | 206 6, 887503681, 12, 787662783788549761, |
| 206 6, 1073741824, 12, 1152921504606846976, | 207 6, 1073741824, 12, 1152921504606846976, |
| 207 5, 39135393, 12, 1667889514952984961, | 208 5, 39135393, 12, 1667889514952984961, |
| 208 5, 45435424, 12, 2386420683693101056, | 209 5, 45435424, 12, 2386420683693101056, |
| 209 5, 52521875, 12, 3379220508056640625, /* radix: 35 */ | 210 5, 52521875, 12, 3379220508056640625, /* radix: 35 */ |
| 210 5, 60466176, 11, 131621703842267136, | 211 5, 60466176, 11, 131621703842267136, |
| 211 ]; | 212 ]; |
| 212 } | 213 } |
| OLD | NEW |