| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 return c - _CC_0; | 50 return c - _CC_0; |
| 51 } else if (c >= _CC_a && c <= _CC_z) { | 51 } else if (c >= _CC_a && c <= _CC_z) { |
| 52 return c - _CC_a + 10; | 52 return c - _CC_a + 10; |
| 53 } else if (c >= _CC_A && c <= _CC_Z) { | 53 } else if (c >= _CC_A && c <= _CC_Z) { |
| 54 return c - _CC_A + 10; | 54 return c - _CC_A + 10; |
| 55 } else { | 55 } else { |
| 56 return -1; // bad char code | 56 return -1; // bad char code |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 static int _validateRadix(int radix) { |
| 61 if (2 <= radix && radix <= 36) return radix; |
| 62 throw new RangeError.range(radix, 2, 36, 'radix'); |
| 63 } |
| 64 |
| 60 /** | 65 /** |
| 61 * Parses a [String] in a given [radix] between 2 and 16 and returns an | 66 * Parses a [String] in a given [radix] between 2 and 16 and returns an |
| 62 * [Int32]. | 67 * [Int32]. |
| 63 */ | 68 */ |
| 64 // TODO(rice) - Make this faster by converting several digits at once. | 69 // TODO(rice) - Make this faster by converting several digits at once. |
| 65 static Int32 parseRadix(String s, int radix) { | 70 static Int32 parseRadix(String s, int radix) { |
| 66 if ((radix <= 1) || (radix > 16)) { | 71 _validateRadix(radix); |
| 67 throw new ArgumentError("Bad radix: $radix"); | |
| 68 } | |
| 69 Int32 x = ZERO; | 72 Int32 x = ZERO; |
| 70 for (int i = 0; i < s.length; i++) { | 73 for (int i = 0; i < s.length; i++) { |
| 71 int c = s.codeUnitAt(i); | 74 int c = s.codeUnitAt(i); |
| 72 int digit = _decodeDigit(c); | 75 int digit = _decodeDigit(c); |
| 73 if (digit < 0 || digit >= radix) { | 76 if (digit < 0 || digit >= radix) { |
| 74 throw new FormatException("Non-radix code unit: $c"); | 77 throw new FormatException("Non-radix code unit: $c"); |
| 75 } | 78 } |
| 76 x = (x * radix) + digit; | 79 x = (x * radix) + digit; |
| 77 } | 80 } |
| 78 return x; | 81 return x; |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 if (upperLimit is int) return new Int32(upperLimit); | 344 if (upperLimit is int) return new Int32(upperLimit); |
| 342 throw new ArgumentError(upperLimit); | 345 throw new ArgumentError(upperLimit); |
| 343 } | 346 } |
| 344 return this; | 347 return this; |
| 345 } | 348 } |
| 346 | 349 |
| 347 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i); | 350 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i); |
| 348 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i); | 351 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i); |
| 349 | 352 |
| 350 Int32 toSigned(int width) { | 353 Int32 toSigned(int width) { |
| 351 if (width < 1 || width > 32) throw new ArgumentError(width); | 354 if (width < 1 || width > 32) throw new RangeError.range(width, 1, 32); |
| 352 return new Int32(_i.toSigned(width)); | 355 return new Int32(_i.toSigned(width)); |
| 353 } | 356 } |
| 354 | 357 |
| 355 Int32 toUnsigned(int width) { | 358 Int32 toUnsigned(int width) { |
| 356 if (width < 0 || width > 32) throw new ArgumentError(width); | 359 if (width < 0 || width > 32) throw new RangeError.range(width, 0, 32); |
| 357 return new Int32(_i.toUnsigned(width)); | 360 return new Int32(_i.toUnsigned(width)); |
| 358 } | 361 } |
| 359 | 362 |
| 360 List<int> toBytes() { | 363 List<int> toBytes() { |
| 361 List<int> result = new List<int>(4); | 364 List<int> result = new List<int>(4); |
| 362 result[0] = _i & 0xff; | 365 result[0] = _i & 0xff; |
| 363 result[1] = (_i >> 8) & 0xff; | 366 result[1] = (_i >> 8) & 0xff; |
| 364 result[2] = (_i >> 16) & 0xff; | 367 result[2] = (_i >> 16) & 0xff; |
| 365 result[3] = (_i >> 24) & 0xff; | 368 result[3] = (_i >> 24) & 0xff; |
| 366 return result; | 369 return result; |
| 367 } | 370 } |
| 368 | 371 |
| 369 double toDouble() => _i.toDouble(); | 372 double toDouble() => _i.toDouble(); |
| 370 int toInt() => _i; | 373 int toInt() => _i; |
| 371 Int32 toInt32() => this; | 374 Int32 toInt32() => this; |
| 372 Int64 toInt64() => new Int64(_i); | 375 Int64 toInt64() => new Int64(_i); |
| 373 | 376 |
| 374 String toString() => _i.toString(); | 377 String toString() => _i.toString(); |
| 375 String toHexString() => _i.toRadixString(16); | 378 String toHexString() => _i.toRadixString(16); |
| 376 String toRadixString(int radix) => _i.toRadixString(radix); | 379 String toRadixString(int radix) => _i.toRadixString(radix); |
| 377 } | 380 } |
| OLD | NEW |