-
Notifications
You must be signed in to change notification settings - Fork 8
/
int32.dart
467 lines (405 loc) · 12.7 KB
/
int32.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// ignore_for_file: constant_identifier_names
import 'int64.dart';
import 'intx.dart';
import 'utilities.dart' as u;
/// An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1].
/// Arithmetic operations may overflow in order to maintain this range.
class Int32 implements IntX {
/// The maximum positive value attainable by an [Int32], namely
/// 2147483647.
static const Int32 MAX_VALUE = Int32._internal(0x7FFFFFFF);
/// The minimum positive value attainable by an [Int32], namely
/// -2147483648.
static const Int32 MIN_VALUE = Int32._internal(-0x80000000);
/// An [Int32] constant equal to 0.
static const Int32 ZERO = Int32._internal(0);
/// An [Int32] constant equal to 1.
static const Int32 ONE = Int32._internal(1);
/// An [Int32] constant equal to 2.
static const Int32 TWO = Int32._internal(2);
// Mask to 32-bits.
static const int _MASK_U32 = 0xFFFFFFFF;
/// Parses [source] in a given [radix] between 2 and 36.
///
/// Returns an [Int32] with the numerical value of [source].
/// If the numerical value of [source] does not fit
/// in a signed 32 bit integer,
/// the numerical value is truncated to the lowest 32 bits
/// of the value's binary representation,
/// interpreted as a 32-bit two's complement integer.
///
/// The [source] string must contain a sequence of base-[radix]
/// digits (using letters from `a` to `z` as digits with values 10 through
/// 25 for radixes above 10), possibly prefixed by a `-` sign.
///
/// Throws a [FormatException] if the input is not a valid
/// integer numeral in base [radix].
static Int32 parseRadix(String source, int radix) =>
_parseRadix(source, u.validateRadix(radix), true)!;
/// Parses [source] in a given [radix] between 2 and 36.
///
/// Returns an [Int32] with the numerical value of [source].
/// If the numerical value of [source] does not fit
/// in a signed 32 bit integer,
/// the numerical value is truncated to the lowest 32 bits
/// of the value's binary representation,
/// interpreted as a 32-bit two's complement integer.
///
/// The [source] string must contain a sequence of base-[radix]
/// digits (using letters from `a` to `z` as digits with values 10 through
/// 25 for radixes above 10), possibly prefixed by a `-` sign.
///
/// Throws a [FormatException] if the input is not a valid
/// integer numeral in base [radix].
static Int32? tryParseRadix(String source, int radix) =>
_parseRadix(source, u.validateRadix(radix), false);
// TODO(rice) - Make this faster by converting several digits at once.
static Int32? _parseRadix(String s, int radix, bool throwOnError) {
var index = 0;
var negative = false;
if (s.startsWith('-')) {
negative = true;
index = 1;
}
if (index == s.length) {
if (!throwOnError) return null;
throw FormatException('No digits', s, index);
}
var result = 0;
for (; index < s.length; index++) {
var c = s.codeUnitAt(index);
var digit = u.decodeDigit(c);
if (digit < radix) {
/// Doesn't matter whether the result is unsigned
/// or signed (as on the web), only the bits matter
/// to the [Int32.new] constructor.
result = (result * radix + digit) & _MASK_U32;
} else {
if (!throwOnError) return null;
throw FormatException('Non radix code unit', s, index);
}
}
if (negative) result = -result;
return Int32(result);
}
/// Parses [source] as a decimal numeral.
///
/// Returns an [Int32] with the numerical value of [source].
/// If the numerical value of [source] does not fit
/// in a signed 32 bit integer,
/// the numerical value is truncated to the lowest 32 bits
/// of the value's binary representation,
/// interpreted as a 32-bit two's complement integer.
///
/// The [source] string must contain a sequence of digits (`0`-`9`),
/// possibly prefixed by a `-` sign.
///
/// Throws a [FormatException] if the input is not a valid
/// decimal integer numeral.
static Int32 parseInt(String source) => _parseRadix(source, 10, true)!;
/// Parses [source] as a decimal numeral.
///
/// Returns an [Int32] with the numerical value of [source].
/// If the numerical value of [source] does not fit
/// in a signed 32 bit integer,
/// the numerical value is truncated to the lowest 32 bits
/// of the value's binary representation,
/// interpreted as a 32-bit two's complement integer.
///
/// The [source] string must contain a sequence of digits (`0`-`9`),
/// possibly prefixed by a `-` sign.
///
/// Throws a [FormatException] if the input is not a valid
/// decimal integer numeral.
static Int32? tryParseInt(String source) => _parseRadix(source, 10, false);
/// Parses [source] as a hexadecimal numeral.
///
/// Returns an [Int32] with the numerical value of [source].
/// If the numerical value of [source] does not fit
/// in a signed 32 bit integer,
/// the numerical value is truncated to the lowest 32 bits
/// of the value's binary representation,
/// interpreted as a 32-bit two's complement integer.
///
/// The [source] string must contain a sequence of hexadecimal
/// digits (`0`-`9`, `a`-`f` or `A`-`F`), possibly prefixed by a `-` sign.
///
/// Returns `null` if the input is not a valid
/// hexadecimal integer numeral.
static Int32 parseHex(String source) => _parseRadix(source, 16, true)!;
/// Parses [source] as a hexadecimal numeral.
///
/// Returns an [Int32] with the numerical value of [source].
/// If the numerical value of [source] does not fit
/// in a signed 32 bit integer,
/// the numerical value is truncated to the lowest 32 bits
/// of the value's binary representation,
/// interpreted as a 32-bit two's complement integer.
///
/// The [source] string must contain a sequence of hexadecimal
/// digits (`0`-`9`, `a`-`f` or `A`-`F`), possibly prefixed by a `-` sign.
///
/// Returns `null` if the input is not a valid
/// hexadecimal integer numeral.
static Int32? tryParseHex(String source) => _parseRadix(source, 16, false);
// The internal value, kept in the range [MIN_VALUE, MAX_VALUE].
final int _i;
const Int32._internal(int i) : _i = i;
/// Constructs an [Int32] from an [int]. Only the low 32 bits of the input
/// are used.
Int32([int i = 0]) : _i = (i & 0x7fffffff) - (i & 0x80000000);
// Returns the [int] representation of the specified value. Throws
// [ArgumentError] for non-integer arguments.
int _toInt(Object val) {
if (val is Int32) {
return val._i;
} else if (val is int) {
return val;
}
throw ArgumentError.value(val, 'other', 'Not an int, Int32 or Int64');
}
// The +, -, * , &, |, and ^ operaters deal with types as follows:
//
// Int32 + int => Int32
// Int32 + Int32 => Int32
// Int32 + Int64 => Int64
//
// The %, ~/ and remainder operators return an Int32 even with an Int64
// argument, since the result cannot be greater than the value on the
// left-hand side:
//
// Int32 % int => Int32
// Int32 % Int32 => Int32
// Int32 % Int64 => Int32
@override
IntX operator +(Object other) {
if (other is Int64) {
return toInt64() + other;
}
return Int32(_i + _toInt(other));
}
@override
IntX operator -(Object other) {
if (other is Int64) {
return toInt64() - other;
}
return Int32(_i - _toInt(other));
}
@override
Int32 operator -() => Int32(-_i);
@override
IntX operator *(Object other) {
if (other is Int64) {
return toInt64() * other;
}
// TODO(rice) - optimize
return (toInt64() * other).toInt32();
}
@override
Int32 operator %(Object other) {
if (other is Int64) {
// Result will be Int32
return (toInt64() % other).toInt32();
}
return Int32(_i % _toInt(other));
}
@override
Int32 operator ~/(Object other) {
if (other is Int64) {
return (toInt64() ~/ other).toInt32();
}
return Int32(_i ~/ _toInt(other));
}
@override
Int32 remainder(Object other) {
if (other is Int64) {
var t = toInt64();
return (t - (t ~/ other) * other).toInt32();
}
return this - (this ~/ other) * other as Int32;
}
@override
Int32 operator &(Object other) {
if (other is Int64) {
return (toInt64() & other).toInt32();
}
return Int32(_i & _toInt(other));
}
@override
Int32 operator |(Object other) {
if (other is Int64) {
return (toInt64() | other).toInt32();
}
return Int32(_i | _toInt(other));
}
@override
Int32 operator ^(Object other) {
if (other is Int64) {
return (toInt64() ^ other).toInt32();
}
return Int32(_i ^ _toInt(other));
}
@override
Int32 operator ~() => Int32(~_i);
@override
Int32 operator <<(int n) {
if (n < 0) {
throw ArgumentError(n);
}
if (n >= 32) {
return ZERO;
}
return Int32(_i << n);
}
@override
Int32 operator >>(int n) {
if (n < 0) {
throw ArgumentError(n);
}
if (n >= 32) {
return isNegative ? const Int32._internal(-1) : ZERO;
}
int value;
if (_i >= 0) {
value = _i >> n;
} else {
value = (_i >> n) | (0xffffffff << (32 - n));
}
return Int32(value);
}
@override
Int32 shiftRightUnsigned(int n) {
if (n < 0) {
throw ArgumentError(n);
}
if (n >= 32) {
return ZERO;
}
int value;
if (_i >= 0) {
value = _i >> n;
} else {
value = (_i >> n) & ((1 << (32 - n)) - 1);
}
return Int32(value);
}
/// Returns [:true:] if this [Int32] has the same numeric value as the
/// given object. The argument may be an [int] or an [IntX].
@override
bool operator ==(Object other) {
if (other is Int32) {
return _i == other._i;
} else if (other is Int64) {
return toInt64() == other;
} else if (other is int) {
return _i == other;
}
return false;
}
@override
int compareTo(Object other) {
if (other is Int64) {
return toInt64().compareTo(other);
}
return _i.compareTo(_toInt(other));
}
@override
bool operator <(Object other) {
if (other is Int64) {
return toInt64() < other;
}
return _i < _toInt(other);
}
@override
bool operator <=(Object other) {
if (other is Int64) {
return toInt64() <= other;
}
return _i <= _toInt(other);
}
@override
bool operator >(Object other) {
if (other is Int64) {
return toInt64() > other;
}
return _i > _toInt(other);
}
@override
bool operator >=(Object other) {
if (other is Int64) {
return toInt64() >= other;
}
return _i >= _toInt(other);
}
@override
bool get isEven => (_i & 0x1) == 0;
@override
bool get isMaxValue => _i == 2147483647;
@override
bool get isMinValue => _i == -2147483648;
@override
bool get isNegative => _i < 0;
@override
bool get isOdd => (_i & 0x1) == 1;
@override
bool get isZero => _i == 0;
@override
int get bitLength => _i.bitLength;
@override
int get hashCode => _i;
@override
Int32 abs() => _i < 0 ? Int32(-_i) : this;
@override
Int32 clamp(Object lowerLimit, Object upperLimit) {
if (this < lowerLimit) {
if (lowerLimit is IntX) return lowerLimit.toInt32();
if (lowerLimit is int) return Int32(lowerLimit);
throw ArgumentError(lowerLimit);
} else if (this > upperLimit) {
if (upperLimit is IntX) return upperLimit.toInt32();
if (upperLimit is int) return Int32(upperLimit);
throw ArgumentError(upperLimit);
}
return this;
}
@override
int numberOfLeadingZeros() => u.numberOfLeadingZeros(_i);
@override
int numberOfTrailingZeros() => u.numberOfTrailingZeros(_i);
@override
Int32 toSigned(int width) {
if (width < 1 || width > 32) throw RangeError.range(width, 1, 32);
return Int32(_i.toSigned(width));
}
@override
Int32 toUnsigned(int width) {
if (width < 0 || width > 32) throw RangeError.range(width, 0, 32);
return Int32(_i.toUnsigned(width));
}
@override
List<int> toBytes() {
var result = List<int>.filled(4, 0);
result[0] = _i & 0xff;
result[1] = (_i >> 8) & 0xff;
result[2] = (_i >> 16) & 0xff;
result[3] = (_i >> 24) & 0xff;
return result;
}
@override
double toDouble() => _i.toDouble();
@override
int toInt() => _i;
@override
Int32 toInt32() => this;
@override
Int64 toInt64() => Int64(_i);
@override
String toString() => _i.toString();
@override
String toHexString() => _i.toRadixString(16);
@override
String toRadixString(int radix) => _i.toRadixString(radix);
}