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 | 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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 return this; | 226 return this; |
| 227 } | 227 } |
| 228 int operator ~() native "Smi_bitNegate"; | 228 int operator ~() native "Smi_bitNegate"; |
| 229 int get bitLength native "Smi_bitLength"; | 229 int get bitLength native "Smi_bitLength"; |
| 230 | 230 |
| 231 int _shrFromInt(int other) native "Smi_shrFromInt"; | 231 int _shrFromInt(int other) native "Smi_shrFromInt"; |
| 232 int _shlFromInt(int other) native "Smi_shlFromInt"; | 232 int _shlFromInt(int other) native "Smi_shlFromInt"; |
| 233 | 233 |
| 234 String toString() { | 234 String toString() { |
| 235 if (this == 0) return "0"; | 235 if (this == 0) return "0"; |
| 236 var reversed = new List(); | 236 var reversed = _toStringBuffer; |
| 237 var val = this < 0 ? -this : this; | 237 var negative = false; |
| 238 var val = this; | |
| 239 int index = 0; | |
| 240 | |
| 241 if (this < 0) { | |
|
kasperl
2013/09/24 11:22:09
Use val < 0?
Lasse Reichstein Nielsen
2013/09/24 19:48:49
good point.
| |
| 242 negative = true; | |
| 243 // Handle the first digit as negative to avoid negating the minimum | |
| 244 // smi, for which the negation is not a smi. | |
| 245 int digit = -(val.remainder(10)); | |
| 246 reversed[index++] = digit + 0x30; | |
| 247 val = -(val ~/ 10); | |
| 248 } | |
| 249 | |
| 238 while (val > 0) { | 250 while (val > 0) { |
| 239 reversed.add((val % 10) + 0x30); | 251 int digit = val % 10; |
| 252 reversed[index++] = (digit + 0x30); | |
| 240 val = val ~/ 10; | 253 val = val ~/ 10; |
| 241 } | 254 } |
| 242 final int numDigits = reversed.length; | 255 if (negative) reversed[index++] = 0x2D; // '-'. |
| 243 List digits; | 256 |
| 244 int i; | 257 _OneByteString string = _OneByteString._allocate(index); |
| 245 if (this < 0) { | 258 for (int i = 0, j = index; i < index; i++) { |
| 246 digits = new List(numDigits + 1); | 259 string._setAt(i, reversed[--j]); |
| 247 digits[0] = 0x2D; // '-'. | |
| 248 i = 1; | |
| 249 } else { | |
| 250 digits = new List(numDigits); | |
| 251 i = 0; | |
| 252 } | 260 } |
| 253 int ri = reversed.length - 1; | 261 return string; |
| 254 for (; i < digits.length; i++, ri--) { | |
| 255 digits[i] = reversed[ri]; | |
| 256 } | |
| 257 return _StringBase.createFromCharCodes(digits); | |
| 258 } | 262 } |
| 259 } | 263 } |
| 260 | 264 |
| 265 // Reusable buffer used by smi.toString. | |
| 266 List _toStringBuffer = new Uint8List(20); | |
| 267 | |
| 261 // Represents integers that cannot be represented by Smi but fit into 64bits. | 268 // Represents integers that cannot be represented by Smi but fit into 64bits. |
| 262 class _Mint extends _IntegerImplementation implements int { | 269 class _Mint extends _IntegerImplementation implements int { |
| 263 factory _Mint._uninstantiable() { | 270 factory _Mint._uninstantiable() { |
| 264 throw new UnsupportedError( | 271 throw new UnsupportedError( |
| 265 "_Mint can only be allocated by the VM"); | 272 "_Mint can only be allocated by the VM"); |
| 266 } | 273 } |
| 267 int get hashCode { | 274 int get hashCode { |
| 268 return this; | 275 return this; |
| 269 } | 276 } |
| 270 int operator ~() native "Mint_bitNegate"; | 277 int operator ~() native "Mint_bitNegate"; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 301 } else { | 308 } else { |
| 302 return 0; | 309 return 0; |
| 303 } | 310 } |
| 304 } | 311 } |
| 305 int _shlFromInt(int other) native "Bigint_shlFromInt"; | 312 int _shlFromInt(int other) native "Bigint_shlFromInt"; |
| 306 | 313 |
| 307 int pow(int exponent) { | 314 int pow(int exponent) { |
| 308 throw "Bigint.pow not implemented"; | 315 throw "Bigint.pow not implemented"; |
| 309 } | 316 } |
| 310 } | 317 } |
| OLD | NEW |