| 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 | 4 |
| 5 // TODO(srdjan): fix limitations. | 5 // TODO(srdjan): fix limitations. |
| 6 // - shift amount must be a Smi. | 6 // - shift amount must be a Smi. |
| 7 class _IntegerImplementation { | 7 class _IntegerImplementation { |
| 8 factory _IntegerImplementation._uninstantiable() { | 8 factory _IntegerImplementation._uninstantiable() { |
| 9 throw new UnsupportedError( | 9 throw new UnsupportedError( |
| 10 "_IntegerImplementation can only be allocated by the VM"); | 10 "_IntegerImplementation can only be allocated by the VM"); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 String toStringAsFixed(int fractionDigits) { | 180 String toStringAsFixed(int fractionDigits) { |
| 181 return this.toDouble().toStringAsFixed(fractionDigits); | 181 return this.toDouble().toStringAsFixed(fractionDigits); |
| 182 } | 182 } |
| 183 String toStringAsExponential([int fractionDigits]) { | 183 String toStringAsExponential([int fractionDigits]) { |
| 184 return this.toDouble().toStringAsExponential(fractionDigits); | 184 return this.toDouble().toStringAsExponential(fractionDigits); |
| 185 } | 185 } |
| 186 String toStringAsPrecision(int precision) { | 186 String toStringAsPrecision(int precision) { |
| 187 return this.toDouble().toStringAsPrecision(precision); | 187 return this.toDouble().toStringAsPrecision(precision); |
| 188 } | 188 } |
| 189 | 189 |
| 190 static const _digits = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 191 |
| 190 String toRadixString(int radix) { | 192 String toRadixString(int radix) { |
| 191 final table = const ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", | |
| 192 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", | |
| 193 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", | |
| 194 "u", "v", "w", "x", "y", "z"]; | |
| 195 if (radix is! int || radix < 2 || radix > 36) { | 193 if (radix is! int || radix < 2 || radix > 36) { |
| 196 throw new ArgumentError(radix); | 194 throw new ArgumentError(radix); |
| 197 } | 195 } |
| 196 if (radix & (radix - 1) == 0) { |
| 197 return _toPow2String(this, radix); |
| 198 } |
| 199 if (radix == 10) return this.toString(); |
| 198 final bool isNegative = this < 0; | 200 final bool isNegative = this < 0; |
| 199 int value = isNegative ? -this : this; | 201 int value = isNegative ? -this : this; |
| 200 List temp = new List(); | 202 List temp = new List(); |
| 201 while (value > 0) { | 203 do { |
| 202 int digit = value % radix; | 204 int digit = value % radix; |
| 203 value ~/= radix; | 205 value ~/= radix; |
| 204 temp.add(digit); | 206 temp.add(_digits.codeUnitAt(digit)); |
| 207 } while (value > 0); |
| 208 if (isNegative) temp.add(0x2d); // '-'. |
| 209 |
| 210 _OneByteString string = _OneByteString._allocate(temp.length); |
| 211 for (int i = 0, j = temp.length; j > 0; i++) { |
| 212 string._setAt(i, temp[--j]); |
| 205 } | 213 } |
| 206 if (temp.isEmpty) { | 214 return string; |
| 207 return "0"; | 215 } |
| 216 |
| 217 static String _toPow2String(value, radix) { |
| 218 if (value == 0) return "0"; |
| 219 assert(radix & (radix - 1) == 0); |
| 220 var negative = value < 0; |
| 221 var bitsPerDigit = radix.bitLength - 1; |
| 222 var length = 0; |
| 223 if (negative) { |
| 224 value = -value; |
| 225 length = 1; |
| 208 } | 226 } |
| 209 StringBuffer buffer = new StringBuffer(); | 227 // Integer division, rounding up, to find number of _digits. |
| 210 if (isNegative) buffer.write("-"); | 228 length += (value.bitLength + bitsPerDigit - 1) ~/ bitsPerDigit; |
| 211 for (int i = temp.length - 1; i >= 0; i--) { | 229 _OneByteString string = _OneByteString._allocate(length); |
| 212 buffer.write(table[temp[i]]); | 230 string._setAt(0, 0x2d); // '-'. Is overwritten if not negative. |
| 213 } | 231 var mask = radix - 1; |
| 214 return buffer.toString(); | 232 do { |
| 233 string._setAt(--length, _digits.codeUnitAt(value & mask)); |
| 234 value >>= bitsPerDigit; |
| 235 } while (value > 0); |
| 236 return string; |
| 215 } | 237 } |
| 216 | 238 |
| 217 _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32"; | 239 _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32"; |
| 218 } | 240 } |
| 219 | 241 |
| 220 class _Smi extends _IntegerImplementation implements int { | 242 class _Smi extends _IntegerImplementation implements int { |
| 221 factory _Smi._uninstantiable() { | 243 factory _Smi._uninstantiable() { |
| 222 throw new UnsupportedError( | 244 throw new UnsupportedError( |
| 223 "_Smi can only be allocated by the VM"); | 245 "_Smi can only be allocated by the VM"); |
| 224 } | 246 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 } else { | 330 } else { |
| 309 return 0; | 331 return 0; |
| 310 } | 332 } |
| 311 } | 333 } |
| 312 int _shlFromInt(int other) native "Bigint_shlFromInt"; | 334 int _shlFromInt(int other) native "Bigint_shlFromInt"; |
| 313 | 335 |
| 314 int pow(int exponent) { | 336 int pow(int exponent) { |
| 315 throw "Bigint.pow not implemented"; | 337 throw "Bigint.pow not implemented"; |
| 316 } | 338 } |
| 317 } | 339 } |
| OLD | NEW |