Index: pkg/fixnum/lib/src/int32.dart |
diff --git a/pkg/fixnum/lib/src/int32.dart b/pkg/fixnum/lib/src/int32.dart |
index 6c8a6f4c443e712fc7e6545486f9060232fe4583..333c47572892287422b7175c2df90f5a229a09c2 100644 |
--- a/pkg/fixnum/lib/src/int32.dart |
+++ b/pkg/fixnum/lib/src/int32.dart |
@@ -138,18 +138,15 @@ class int32 implements intx { |
*/ |
int32.fromInt(int i) : _i = (i & 0x7fffffff) - (i & 0x80000000); |
- // Convert an [int] or [intx] to an [int32]. Note that an [int64] |
- // will be truncated. |
- int _convert(other) { |
- if (other == null) { |
- throw new ArgumentError(null); |
- } else if (other is intx) { |
- return other.toInt32()._i; |
- } else if (other is int) { |
- return other; |
- } else { |
- throw new Exception("Can't retrieve 32-bit int from $other"); |
+ // Returns the [int] representation of the specified value or null for |
+ // incompatible types. |
+ int _toInt(val) { |
justinfagnani
2013/07/26 23:44:40
all uses of this exception the one in ==() throw a
Chris Bracken
2013/07/27 01:07:11
Done.
|
+ if (val is int32) { |
+ return val._i; |
+ } else if (val is int) { |
+ return val; |
} |
+ return null; |
} |
// The +, -, * , &, |, and ^ operaters deal with types as follows: |
@@ -170,14 +167,22 @@ class int32 implements intx { |
if (other is int64) { |
return this.toInt64() + other; |
justinfagnani
2013/07/26 23:44:40
for all these "this.toInt64()" calls, can you inst
Chris Bracken
2013/07/27 01:07:11
Done.
Chris Bracken
2013/07/27 01:07:11
As the code currently stands, it won't help. int64
|
} |
- return new int32.fromInt(_i + _convert(other)); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return new int32.fromInt(_i + intVal); |
+ } |
+ throw new ArgumentError(other); |
} |
intx operator -(other) { |
if (other is int64) { |
return this.toInt64() - other; |
} |
- return new int32.fromInt(_i - _convert(other)); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return new int32.fromInt(_i - intVal); |
+ } |
+ throw new ArgumentError(other); |
} |
int32 operator -() => new int32.fromInt(-_i); |
@@ -195,20 +200,26 @@ class int32 implements intx { |
// Result will be int32 |
return (this.toInt64() % other).toInt32(); |
} |
- return new int32.fromInt(_i % _convert(other)); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return new int32.fromInt(_i % intVal); |
+ } |
+ throw new ArgumentError(other); |
} |
int32 operator ~/(other) { |
if (other is int64) { |
- // Result will be int32 |
return (this.toInt64() ~/ other).toInt32(); |
} |
- return new int32.fromInt(_i ~/ _convert(other)); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return new int32.fromInt(_i ~/ intVal); |
+ } |
+ throw new ArgumentError(other); |
} |
int32 remainder(other) { |
if (other is int64) { |
- // Result will be int32 |
int64 t = this.toInt64(); |
return (t - (t ~/ other) * other).toInt32(); |
} |
@@ -219,21 +230,33 @@ class int32 implements intx { |
if (other is int64) { |
return (this.toInt64() & other).toInt32(); |
} |
- return new int32.fromInt(_i & _convert(other)); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return new int32.fromInt(_i & intVal); |
+ } |
+ throw new ArgumentError(other); |
} |
int32 operator |(other) { |
if (other is int64) { |
return (this.toInt64() | other).toInt32(); |
} |
- return new int32.fromInt(_i | _convert(other)); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return new int32.fromInt(_i | intVal); |
+ } |
+ throw new ArgumentError(other); |
} |
int32 operator ^(other) { |
if (other is int64) { |
return (this.toInt64() ^ other).toInt32(); |
} |
- return new int32.fromInt(_i ^ _convert(other)); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return new int32.fromInt(_i ^ intVal); |
+ } |
+ throw new ArgumentError(other); |
} |
int32 operator ~() => new int32.fromInt(~_i); |
@@ -279,48 +302,67 @@ class int32 implements intx { |
* given object. The argument may be an [int] or an [intx]. |
*/ |
bool operator ==(other) { |
- if (other == null) { |
- return false; |
- } |
if (other is int64) { |
return this.toInt64() == other; |
} |
- return _i == _convert(other); |
+ return _i == _toInt(other); |
} |
int compareTo(Comparable other) { |
if (other is int64) { |
return this.toInt64().compareTo(other); |
} |
- return _i.compareTo(_convert(other)); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return _i.compareTo(intVal); |
+ } |
+ throw new ArgumentError(other); |
} |
bool operator <(other) { |
if (other is int64) { |
return this.toInt64() < other; |
} |
- return _i < _convert(other); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return _i < intVal; |
+ } |
+ throw new ArgumentError(other); |
+ |
} |
bool operator <=(other) { |
if (other is int64) { |
return this.toInt64() < other; |
} |
- return _i <= _convert(other); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return _i <= intVal; |
+ } |
+ throw new ArgumentError(other); |
} |
bool operator >(other) { |
if (other is int64) { |
return this.toInt64() < other; |
} |
- return _i > _convert(other); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return _i > intVal; |
+ } |
+ throw new ArgumentError(other); |
+ |
} |
bool operator >=(other) { |
if (other is int64) { |
return this.toInt64() < other; |
} |
- return _i >= _convert(other); |
+ var intVal = _toInt(other); |
+ if (intVal != null) { |
+ return _i >= intVal; |
+ } |
+ throw new ArgumentError(other); |
} |
bool get isEven => (_i & 0x1) == 0; |