Comparing version 0.2.19 to 0.2.20
@@ -8,2 +8,3 @@ import { u128 } from '../../assembly/integer/u128'; | ||
expect('102482237023513730008').toStrictEqual(a.toString()); | ||
expect('58e3a0b9945ebdbd8').toStrictEqual(a.toString(16)); | ||
}); | ||
@@ -14,2 +15,3 @@ | ||
expect('340282366920938463463374607431768211455').toStrictEqual(a.toString()); | ||
expect('ffffffffffffffffffffffffffffffff').toStrictEqual(a.toString(16)); | ||
}); | ||
@@ -20,2 +22,3 @@ | ||
expect('0').toStrictEqual(a.toString()); | ||
expect('0').toStrictEqual(a.toString(16)); | ||
}); | ||
@@ -25,2 +28,3 @@ | ||
var a = u128.from(90); | ||
expect('90').toStrictEqual(a.toString()); | ||
expect('5a').toStrictEqual(a.toString(16)); | ||
@@ -30,6 +34,13 @@ }); | ||
it("Should convert to decimal string 5", () => { | ||
var a = u128.Max; | ||
expect('ffffffffffffffffffffffffffffffff').toStrictEqual(a.toString(16)); | ||
var a = new u128(u64.MAX_VALUE); | ||
expect('18446744073709551615').toStrictEqual(a.toString()); | ||
expect('ffffffffffffffff').toStrictEqual(a.toString(16)); | ||
}); | ||
it("Should convert to decimal string 6", () => { | ||
var a = new u128(u64.MAX_VALUE - 1, u64.MAX_VALUE - 1); | ||
expect('340282366920938463444927863358058659838').toStrictEqual(a.toString()); | ||
expect('fffffffffffffffefffffffffffffffe').toStrictEqual(a.toString(16)); | ||
}); | ||
it("Should convert from decimal string 1", () => { | ||
@@ -36,0 +47,0 @@ expect(u128.from('')).toStrictEqual(u128.Zero); |
@@ -22,3 +22,3 @@ import { i128 } from './i128'; | ||
import { atou128, u128toa10 } from '../utils'; | ||
import { atou128, u128toDecimalString } from '../utils'; | ||
@@ -836,9 +836,9 @@ @lazy const HEX_CHARS = '0123456789abcdef'; | ||
/** | ||
* Convert to generic type T. Useful inside other generics methods | ||
* @param T is <bool | i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64 | f32 | f64 | u128 | u256 | u8[] | Uint8Array | String> | ||
* @returns type of T | ||
* Convert to generic type `T`. Useful inside other generics methods | ||
* @param T is <bool | i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64 | f32 | f64 | i128 | u128 | u256 | u8[] | Uint8Array | `StaticArray<u8>` | string> | ||
* @returns type of `T` | ||
*/ | ||
@inline | ||
as<T>(): T { | ||
var dummy: T; | ||
var dummy!: T; | ||
if (dummy instanceof bool) return <T>this.toBool(); | ||
@@ -944,4 +944,4 @@ else if (dummy instanceof i8) return <T>this.toI64(); | ||
} | ||
return u128toa10(this); | ||
return u128toDecimalString(this); | ||
} | ||
} |
import { i128 } from './i128'; | ||
import { u128 } from './u128'; | ||
import { u256toa10 } from "../utils"; | ||
import { u256toDecimalString } from "../utils"; | ||
@@ -564,9 +564,9 @@ @lazy const HEX_CHARS = '0123456789abcdef'; | ||
/** | ||
* Convert to generic type T. Useful inside other generics methods | ||
* @param T is <bool | i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64 | f32 | f64 | u128 | u256 | u8[] | Uint8Array | String> | ||
* @returns type of T | ||
* Convert to generic type `T`. Useful inside other generics methods | ||
* @param T is <bool | i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64 | f32 | f64 | i128 | u128 | u256 | u8[] | Uint8Array | `StaticArray<u8>` | string> | ||
* @returns type of `T` | ||
*/ | ||
@inline | ||
as<T>(): T { | ||
var dummy: T; | ||
var dummy!: T; | ||
if (dummy instanceof bool) return <T>this.toBool(); | ||
@@ -647,4 +647,4 @@ else if (dummy instanceof i8) return <T>this.toI64(); | ||
} | ||
return u256toa10(this); | ||
return u256toDecimalString(this); | ||
} | ||
} |
@@ -6,5 +6,2 @@ import { CharCode } from "util/string"; | ||
// @ts-ignore: decorator | ||
@lazy const HEX_CHARS = '0123456789abcdef'; | ||
// @ts-ignore: decorator | ||
@lazy const MaxBaseForExponent128: u64[] = [ | ||
@@ -84,20 +81,21 @@ u64.MAX_VALUE, // 0 | ||
// helper function for utoa | ||
function processU64(digits: Int8Array, value: u64): void { | ||
function processU64(digits: Uint8Array, value: u64): void { | ||
var length = digits.length - 1; | ||
for (let i = 63; i != -1; i--) { | ||
for (let j = 0; j <= length; j++) { | ||
digits[j] = digits[j] + (i8(digits[j] >= 5) * 3); | ||
for (let i = 63; i != -1; --i) { | ||
for (let j = 0; j <= length; ++j) { | ||
unchecked(digits[j] += (u8(digits[j] >= 5) * 3)); | ||
} | ||
for (let j = length; j != -1; j--) { | ||
digits[j] = digits[j] << 1; | ||
if (j < length) digits[j + 1] = (digits[j+1] | i8(digits[j] > 15)); | ||
digits[j] = digits[j] & 15; | ||
for (let j = length; j != -1; --j) { | ||
let d = unchecked(digits[j]) << 1; | ||
if (j < length) unchecked(digits[j + 1] |= u8(d > 15)); | ||
unchecked(digits[j] = d & 15); | ||
} | ||
digits[0] = digits[0] + i8((value & (1 << i)) != 0); | ||
unchecked(digits[0] += u8((value & (1 << i)) != 0)); | ||
} | ||
} | ||
export function u128toa10(value: u128): string { | ||
export function u128toDecimalString(value: u128): string { | ||
var length = 40; | ||
var digits = new Int8Array(length); | ||
var digits = new Uint8Array(length); | ||
var result = "", start = false; | ||
@@ -107,7 +105,9 @@ processU64(digits, value.hi); | ||
var result = ""; | ||
var start = false; | ||
for (let i = length - 1; i != -1; i--) { | ||
if (!start && digits[i] > 0) start = true; | ||
if (start) result = result.concat(HEX_CHARS.charAt(digits[i])); | ||
for (let i = length - 1; i != -1; --i) { | ||
let d = unchecked(digits[i]); | ||
if (!start && d != 0) start = true; | ||
if (start) { | ||
assert(<u32>d <= 9); | ||
result += String.fromCharCode(0x30 + d); | ||
} | ||
} | ||
@@ -117,5 +117,6 @@ return result; | ||
export function u256toa10(value: u256): string { | ||
export function u256toDecimalString(value: u256): string { | ||
var length = 78; | ||
var digits = new Int8Array(length); | ||
var digits = new Uint8Array(length); | ||
var result = "", start = false; | ||
@@ -127,7 +128,9 @@ processU64(digits, value.hi2); | ||
var result = ""; | ||
var start = false; | ||
for (let i = length - 1; i != -1; i--) { | ||
if (!start && digits[i] > 0) start = true; | ||
if (start) result = result.concat(HEX_CHARS.charAt(digits[i])); | ||
for (let i = length - 1; i != -1; --i) { | ||
let d = unchecked(digits[i]); | ||
if (!start && d != 0) start = true; | ||
if (start) { | ||
assert(<u32>d <= 9); | ||
result += String.fromCharCode(0x30 + d); | ||
} | ||
} | ||
@@ -134,0 +137,0 @@ return result; |
{ | ||
"name": "as-bignum", | ||
"version": "0.2.19", | ||
"version": "0.2.20", | ||
"description": "128 and 256 bits integer and fixed point arithmetics for AssemblyScript. Also support checking overflow/underflow", | ||
@@ -5,0 +5,0 @@ "main": "js/index.js", |
Sorry, the diff of this file is not supported yet
949654
4518