Add toStringUnsigned() method (#44)
* Update int64.dart
* Update int64_test.dart
* Update int64_test.dart
* Update pubspec.yaml
* Update int64.dart
* Update int64.dart
* Update int64_test.dart
* Update int64.dart
* Update CHANGELOG.md
* Update int64_test.dart
* Update int64.dart
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9c978a5..796f6c6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.10.9
+
+* Add `Int64.toStringUnsigned()` method.
+
## 0.10.8
* Set SDK version constraint to `>=2.0.0-dev.65 <3.0.0`.
diff --git a/lib/src/int64.dart b/lib/src/int64.dart
index a84cbd9..ead89fe 100644
--- a/lib/src/int64.dart
+++ b/lib/src/int64.dart
@@ -668,6 +668,19 @@
return hexStr;
}
+ /**
+ * Returns the digits of [this] when interpreted as an unsigned 64-bit value.
+ */
+ @pragma('dart2js:noInline')
+ String toStringUnsigned() {
+ return _toRadixStringUnsigned(10, _l, _m, _h, '');
+ }
+
+ @pragma('dart2js:noInline')
+ String toRadixStringUnsigned(int radix) {
+ return _toRadixStringUnsigned(Int32._validateRadix(radix), _l, _m, _h, '');
+ }
+
String toRadixString(int radix) {
return _toRadixString(Int32._validateRadix(radix));
}
@@ -677,8 +690,6 @@
int d1 = _m;
int d2 = _h;
- if (d0 == 0 && d1 == 0 && d2 == 0) return '0';
-
String sign = '';
if ((d2 & _SIGN_BIT_MASK) != 0) {
sign = '-';
@@ -695,6 +706,12 @@
// d2, d1, d0 now are an unsigned 64 bit integer for MIN_VALUE and an
// unsigned 63 bit integer for other values.
}
+ return _toRadixStringUnsigned(radix, d0, d1, d2, sign);
+ }
+
+ static String _toRadixStringUnsigned(
+ int radix, int d0, int d1, int d2, String sign) {
+ if (d0 == 0 && d1 == 0 && d2 == 0) return '0';
// Rearrange components into five components where all but the most
// significant are 10 bits wide.
diff --git a/pubspec.yaml b/pubspec.yaml
index d8f523d..ff06246 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: fixnum
-version: 0.10.8
+version: 0.10.9
description: Library for 32- and 64-bit signed fixed-width integers.
author: Dart Team <misc@dartlang.org>
diff --git a/test/int64_test.dart b/test/int64_test.dart
index 2c6f3ca..0fc2085 100644
--- a/test/int64_test.dart
+++ b/test/int64_test.dart
@@ -888,5 +888,29 @@
expect(() => new Int64(42).toRadixString(0), throwsArgumentError);
expect(() => new Int64(42).toRadixString(37), throwsArgumentError);
});
+
+ test("toStringUnsigned", () {
+ List<Int64> values = [];
+ for (int high = 0; high < 16; high++) {
+ for (int low = -2; low <= 2; low++) {
+ values.add((Int64(high) << (64 - 4)) + Int64(low));
+ }
+ }
+
+ for (Int64 value in values) {
+ for (int radix = 2; radix <= 36; radix++) {
+ String s1 = value.toRadixStringUnsigned(radix);
+ Int64 v2 = Int64.parseRadix(s1, radix);
+ expect(v2, value);
+ String s2 = v2.toRadixStringUnsigned(radix);
+ expect(s2, s1);
+ }
+ String s3 = value.toStringUnsigned();
+ Int64 v4 = Int64.parseInt(s3);
+ expect(v4, value);
+ String s4 = v4.toStringUnsigned();
+ expect(s4, s3);
+ }
+ });
});
}