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 extends _Num { | 7 class _IntegerImplementation extends _Num { |
8 // The Dart class _Bigint extending _IntegerImplementation requires a | 8 // The Dart class _Bigint extending _IntegerImplementation requires a |
9 // default constructor. | 9 // default constructor. |
10 | 10 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 return this.toDouble().toStringAsPrecision(precision); | 213 return this.toDouble().toStringAsPrecision(precision); |
214 } | 214 } |
215 | 215 |
216 static const _digits = "0123456789abcdefghijklmnopqrstuvwxyz"; | 216 static const _digits = "0123456789abcdefghijklmnopqrstuvwxyz"; |
217 | 217 |
218 String toRadixString(int radix) { | 218 String toRadixString(int radix) { |
219 if (radix is! int || radix < 2 || radix > 36) { | 219 if (radix is! int || radix < 2 || radix > 36) { |
220 throw new ArgumentError(radix); | 220 throw new ArgumentError(radix); |
221 } | 221 } |
222 if (radix & (radix - 1) == 0) { | 222 if (radix & (radix - 1) == 0) { |
223 return _toPow2String(this, radix); | 223 return _toPow2String(radix); |
224 } | 224 } |
225 if (radix == 10) return this.toString(); | 225 if (radix == 10) return this.toString(); |
226 final bool isNegative = this < 0; | 226 final bool isNegative = this < 0; |
227 int value = isNegative ? -this : this; | 227 int value = isNegative ? -this : this; |
228 List temp = new List(); | 228 List temp = new List(); |
229 do { | 229 do { |
230 int digit = value % radix; | 230 int digit = value % radix; |
231 value ~/= radix; | 231 value ~/= radix; |
232 temp.add(_digits.codeUnitAt(digit)); | 232 temp.add(_digits.codeUnitAt(digit)); |
233 } while (value > 0); | 233 } while (value > 0); |
234 if (isNegative) temp.add(0x2d); // '-'. | 234 if (isNegative) temp.add(0x2d); // '-'. |
235 | 235 |
236 _OneByteString string = _OneByteString._allocate(temp.length); | 236 _OneByteString string = _OneByteString._allocate(temp.length); |
237 for (int i = 0, j = temp.length; j > 0; i++) { | 237 for (int i = 0, j = temp.length; j > 0; i++) { |
238 string._setAt(i, temp[--j]); | 238 string._setAt(i, temp[--j]); |
239 } | 239 } |
240 return string; | 240 return string; |
241 } | 241 } |
242 | 242 |
243 static String _toPow2String(value, radix) { | 243 String _toPow2String(int radix) { |
| 244 int value = this; |
244 if (value == 0) return "0"; | 245 if (value == 0) return "0"; |
245 assert(radix & (radix - 1) == 0); | 246 assert(radix & (radix - 1) == 0); |
246 var negative = value < 0; | 247 var negative = value < 0; |
247 var bitsPerDigit = radix.bitLength - 1; | 248 var bitsPerDigit = radix.bitLength - 1; |
248 var length = 0; | 249 var length = 0; |
249 if (negative) { | 250 if (negative) { |
250 value = -value; | 251 value = -value; |
251 length = 1; | 252 length = 1; |
252 } | 253 } |
253 // Integer division, rounding up, to find number of _digits. | 254 // Integer division, rounding up, to find number of _digits. |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 // Shift by mint exceeds range that can be handled by the VM. | 503 // Shift by mint exceeds range that can be handled by the VM. |
503 int _shrFromInt(int other) { | 504 int _shrFromInt(int other) { |
504 if (other < 0) { | 505 if (other < 0) { |
505 return -1; | 506 return -1; |
506 } else { | 507 } else { |
507 return 0; | 508 return 0; |
508 } | 509 } |
509 } | 510 } |
510 int _shlFromInt(int other) native "Mint_shlFromInt"; | 511 int _shlFromInt(int other) native "Mint_shlFromInt"; |
511 } | 512 } |
OLD | NEW |