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 20 matching lines...) Expand all Loading... |
31 * An [int32] constant equal to 1. | 31 * An [int32] constant equal to 1. |
32 */ | 32 */ |
33 static int32 ONE = const int32._internal(1); | 33 static int32 ONE = const int32._internal(1); |
34 | 34 |
35 /** | 35 /** |
36 * An [int32] constant equal to 2. | 36 * An [int32] constant equal to 2. |
37 */ | 37 */ |
38 static int32 TWO = const int32._internal(2); | 38 static int32 TWO = const int32._internal(2); |
39 | 39 |
40 // Hex digit char codes | 40 // Hex digit char codes |
41 static const int _CC_0 = 48; // '0'.charCodeAt(0) | 41 static const int _CC_0 = 48; // '0'.codeUnitAt(0) |
42 static const int _CC_9 = 57; // '9'.charCodeAt(0) | 42 static const int _CC_9 = 57; // '9'.codeUnitAt(0) |
43 static const int _CC_a = 97; // 'a'.charCodeAt(0) | 43 static const int _CC_a = 97; // 'a'.codeUnitAt(0) |
44 static const int _CC_z = 122; // 'z'.charCodeAt(0) | 44 static const int _CC_z = 122; // 'z'.codeUnitAt(0) |
45 static const int _CC_A = 65; // 'A'.charCodeAt(0) | 45 static const int _CC_A = 65; // 'A'.codeUnitAt(0) |
46 static const int _CC_Z = 90; // 'Z'.charCodeAt(0) | 46 static const int _CC_Z = 90; // 'Z'.codeUnitAt(0) |
47 | 47 |
48 static int _decodeHex(int c) { | 48 static int _decodeHex(int c) { |
49 if (c >= _CC_0 && c <= _CC_9) { | 49 if (c >= _CC_0 && c <= _CC_9) { |
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 /** | 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 "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.charCodeAt(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 char code: $c"); | 74 throw new Exception("Non-radix code unit: $c"); |
75 } | 75 } |
76 x = (x * radix) + digit; | 76 x = (x * radix) + digit; |
77 } | 77 } |
78 return x; | 78 return x; |
79 } | 79 } |
80 | 80 |
81 /** | 81 /** |
82 * Parses a decimal [String] and returns an [int32]. | 82 * Parses a decimal [String] and returns an [int32]. |
83 */ | 83 */ |
84 static int32 parseInt(String s) => new int32.fromInt(int.parse(s)); | 84 static int32 parseInt(String s) => new int32.fromInt(int.parse(s)); |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 } | 347 } |
348 | 348 |
349 int toInt() => _i; | 349 int toInt() => _i; |
350 int32 toInt32() => this; | 350 int32 toInt32() => this; |
351 int64 toInt64() => new int64.fromInt(_i); | 351 int64 toInt64() => new int64.fromInt(_i); |
352 | 352 |
353 String toString() => _i.toString(); | 353 String toString() => _i.toString(); |
354 String toHexString() => _i.toRadixString(16); | 354 String toHexString() => _i.toRadixString(16); |
355 String toRadixString(int radix) => _i.toRadixString(radix); | 355 String toRadixString(int radix) => _i.toRadixString(radix); |
356 } | 356 } |
OLD | NEW |