Add toRadixString(radix, {digits, prefix})
diff --git a/lib/src/int64.dart b/lib/src/int64.dart
index 256058e..9c2a88d 100644
--- a/lib/src/int64.dart
+++ b/lib/src/int64.dart
@@ -58,7 +58,12 @@
*/
static const Int64 TWO = const Int64._bits(2, 0, 0);
- static const _constants = const <Int64>[ZERO, ONE, TWO];
+ /**
+ * An [Int64] constant equal to -1.
+ */
+ static const Int64 NEGATIVE_ONE = const Int64._bits(_MASK, _MASK, _MASK2);
+
+ static const _constants = const <Int64>[NEGATIVE_ONE, ZERO, ONE, TWO];
/**
* Constructs an [Int64] with a given bitwise representation. No validation
@@ -66,7 +71,6 @@
*/
const Int64._bits(this._l, this._m, this._h);
-
/// Parses a string into the numerical value.
///
/// The input has the same syntax as [int.parse]. Leading an trailing
@@ -86,7 +90,7 @@
if (radix != null) Int32._validateRadix(radix);
return _tryParse(s, radix);
}
-
+
/// Parses a decimal [String] and returns an [Int64].
///
/// The string must contain only decimal digits, optionally preceeded by a
@@ -128,7 +132,7 @@
}
static int _returnNull(_) => null;
-
+
static Int64 _parseRadixOld(String s, int radix) {
int i = 0;
bool negative = false;
@@ -164,12 +168,11 @@
}
static Int64 _parseRadix(String s, bool negative, int start, int radix) {
- int i = start;
int d0 = 0, d1 = 0, d2 = 0; // low, middle, high components.
- for (; i < s.length; i++) {
+ for (int i = start; i < s.length; i++) {
int c = s.codeUnitAt(i);
int digit = Int32._decodeDigit(c);
- if (digit < 0 || digit >= radix) {
+ if (digit < 0 || digit >= radix) {
return null;
}
@@ -201,8 +204,8 @@
*/
factory Int64([int value = 0]) {
bool negative = false;
- if (value < _constants.length) {
- if (value >= 0) return _constants[value];
+ if (value < _constants.length - 1) {
+ if (value >= -1) return _constants[value + 1];
negative = true;
value = -value;
}
@@ -724,7 +727,7 @@
/**
* Returns the value of this [Int64] as a decimal [String].
*/
- String toString() => _toRadixString(10);
+ String toString() => _toRadixString(10, 0, '');
// TODO(rice) - Make this faster by avoiding arithmetic.
String toHexString() {
@@ -739,11 +742,12 @@
return hexStr;
}
- String toRadixString(int radix) {
- return _toRadixString(Int32._validateRadix(radix));
+ String toRadixString(int radix, {int digits, String prefix}) {
+ return _toRadixString(
+ Int32._validateRadix(radix), digits ?? 0, prefix ?? "");
}
- String _toRadixString(int radix) {
+ String _toRadixString(int radix, int minDigits, String prefix) {
int d0 = _l;
int d1 = _m;
int d2 = _h;
@@ -836,7 +840,9 @@
}
int residue = (d2 << 20) + (d1 << 10) + d0;
String leadingDigits = residue == 0 ? '' : residue.toRadixString(radix);
- return '$sign$leadingDigits$chunk1$chunk2$chunk3';
+ String digits = '$leadingDigits$chunk1$chunk2$chunk3';
+ digits = digits.padLeft(minDigits, '0');
+ return '$sign$prefix$digits';
}
// Table of 'fat' radix values. Each entry for index `i` is the largest power
diff --git a/test/int64_test.dart b/test/int64_test.dart
index 877e668..76693f6 100644
--- a/test/int64_test.dart
+++ b/test/int64_test.dart
@@ -45,9 +45,19 @@
expect(() => op(receiver, 'foo'), throwsArgumentErrorMentioning(r'"foo"'));
}
+ group("constants", () {
+ test("toString", () {
+ expect(Int64.NEGATIVE_ONE.toString(), "-1");
+ expect(Int64.ZERO.toString(), "0");
+ expect(Int64.ONE.toString(), "1");
+ expect(Int64.TWO.toString(), "2");
+ });
+ });
+
group("is-tests", () {
test("isEven", () {
expect((-Int64.ONE).isEven, false);
+ expect(Int64.NEGATIVE_ONE.isEven, false);
expect(Int64.ZERO.isEven, true);
expect(Int64.ONE.isEven, false);
expect(Int64.TWO.isEven, true);
@@ -836,7 +846,7 @@
expect(() => Int64.parse('0', radix: 37), throwsRangeError);
expect(() => Int64.parse('xyzzy', radix: -1), throwsRangeError);
expect(() => Int64.parse('xyzzy', radix: 10), throwsFormatException);
-});
+ });
test("parseRadixN", () {
check(String s, int r) {
@@ -912,9 +922,19 @@
expect(Int64.MAX_VALUE.toRadixString(14), "4340724c6c71dc7a7");
expect(Int64.MAX_VALUE.toRadixString(15), "160e2ad3246366807");
expect(Int64.MAX_VALUE.toRadixString(16), "7fffffffffffffff");
+ });
+
+ test("toRadixString-bad-radix", () {
expect(() => new Int64(42).toRadixString(-1), throwsArgumentError);
expect(() => new Int64(42).toRadixString(0), throwsArgumentError);
expect(() => new Int64(42).toRadixString(37), throwsArgumentError);
});
+
+ test("toRadixString-digits-prefix", () {
+ expect(
+ new Int64(256).toRadixString(16, digits: 4, prefix: "0x"), "0x0100");
+ expect(new Int64(-256).toRadixString(16, digits: 4, prefix: "0x"),
+ "-0x0100");
+ });
});
}