| 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 part of fixnum; | 5 part of fixnum; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1]. | 8 * An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1]. |
| 9 * Arithmetic operations may overflow in order to maintain this range. | 9 * Arithmetic operations may overflow in order to maintain this range. |
| 10 */ | 10 */ |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 /** | 60 /** |
| 61 * Parses a [String] in a given [radix] between 2 and 16 and returns an | 61 * Parses a [String] in a given [radix] between 2 and 16 and returns an |
| 62 * [Int32]. | 62 * [Int32]. |
| 63 */ | 63 */ |
| 64 // TODO(rice) - Make this faster by converting several digits at once. | 64 // TODO(rice) - Make this faster by converting several digits at once. |
| 65 static Int32 parseRadix(String s, int radix) { | 65 static Int32 parseRadix(String s, int radix) { |
| 66 if ((radix <= 1) || (radix > 16)) { | 66 if ((radix <= 1) || (radix > 16)) { |
| 67 throw "Bad radix: $radix"; | 67 throw new ArgumentError("Bad radix: $radix"); |
| 68 } | 68 } |
| 69 Int32 x = ZERO; | 69 Int32 x = ZERO; |
| 70 for (int i = 0; i < s.length; i++) { | 70 for (int i = 0; i < s.length; i++) { |
| 71 int c = s.codeUnitAt(i); | 71 int c = s.codeUnitAt(i); |
| 72 int digit = _decodeHex(c); | 72 int digit = _decodeHex(c); |
| 73 if (digit < 0 || digit >= radix) { | 73 if (digit < 0 || digit >= radix) { |
| 74 throw new Exception("Non-radix code unit: $c"); | 74 throw new Exception("Non-radix code unit: $c"); |
| 75 } | 75 } |
| 76 x = (x * radix) + digit; | 76 x = (x * radix) + digit; |
| 77 } | 77 } |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 if (other is Int64) { | 228 if (other is Int64) { |
| 229 return (this.toInt64() ^ other).toInt32(); | 229 return (this.toInt64() ^ other).toInt32(); |
| 230 } | 230 } |
| 231 return new Int32.fromInt(_i ^ _toInt(other)); | 231 return new Int32.fromInt(_i ^ _toInt(other)); |
| 232 } | 232 } |
| 233 | 233 |
| 234 Int32 operator ~() => new Int32.fromInt(~_i); | 234 Int32 operator ~() => new Int32.fromInt(~_i); |
| 235 | 235 |
| 236 Int32 operator <<(int n) { | 236 Int32 operator <<(int n) { |
| 237 if (n < 0) { | 237 if (n < 0) { |
| 238 throw new ArgumentError("$n"); | 238 throw new ArgumentError(n); |
| 239 } | 239 } |
| 240 n &= 31; | 240 n &= 31; |
| 241 return new Int32.fromInt(_i << n); | 241 return new Int32.fromInt(_i << n); |
| 242 } | 242 } |
| 243 | 243 |
| 244 Int32 operator >>(int n) { | 244 Int32 operator >>(int n) { |
| 245 if (n < 0) { | 245 if (n < 0) { |
| 246 throw new ArgumentError("$n"); | 246 throw new ArgumentError(n); |
| 247 } | 247 } |
| 248 n &= 31; | 248 n &= 31; |
| 249 int value; | 249 int value; |
| 250 if (_i >= 0) { | 250 if (_i >= 0) { |
| 251 value = _i >> n; | 251 value = _i >> n; |
| 252 } else { | 252 } else { |
| 253 value = (_i >> n) | (0xffffffff << (32 - n)); | 253 value = (_i >> n) | (0xffffffff << (32 - n)); |
| 254 } | 254 } |
| 255 return new Int32.fromInt(value); | 255 return new Int32.fromInt(value); |
| 256 } | 256 } |
| 257 | 257 |
| 258 Int32 shiftRightUnsigned(int n) { | 258 Int32 shiftRightUnsigned(int n) { |
| 259 if (n < 0) { | 259 if (n < 0) { |
| 260 throw new ArgumentError("$n"); | 260 throw new ArgumentError(n); |
| 261 } | 261 } |
| 262 n &= 31; | 262 n &= 31; |
| 263 int value; | 263 int value; |
| 264 if (_i >= 0) { | 264 if (_i >= 0) { |
| 265 value = _i >> n; | 265 value = _i >> n; |
| 266 } else { | 266 } else { |
| 267 value = (_i >> n) & ((1 << (32 - n)) - 1); | 267 value = (_i >> n) & ((1 << (32 - n)) - 1); |
| 268 } | 268 } |
| 269 return new Int32.fromInt(value); | 269 return new Int32.fromInt(value); |
| 270 } | 270 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 } | 343 } |
| 344 | 344 |
| 345 int toInt() => _i; | 345 int toInt() => _i; |
| 346 Int32 toInt32() => this; | 346 Int32 toInt32() => this; |
| 347 Int64 toInt64() => new Int64.fromInt(_i); | 347 Int64 toInt64() => new Int64.fromInt(_i); |
| 348 | 348 |
| 349 String toString() => _i.toString(); | 349 String toString() => _i.toString(); |
| 350 String toHexString() => _i.toRadixString(16); | 350 String toHexString() => _i.toRadixString(16); |
| 351 String toRadixString(int radix) => _i.toRadixString(radix); | 351 String toRadixString(int radix) => _i.toRadixString(radix); |
| 352 } | 352 } |
| OLD | NEW |