Comparing version 0.0.78 to 0.0.79
@@ -22,3 +22,3 @@ import { i128 } from './i128'; | ||
import { atou128, u128toa10 } from '../utils'; | ||
import { atou128, u128toDecimalString } from '../utils'; | ||
import { Encoder, Decoder, Packer } from "../../serializer"; | ||
@@ -854,9 +854,9 @@ | ||
/** | ||
* 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(); | ||
@@ -962,4 +962,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"; | ||
import { Encoder, Decoder, Packer } from "../../serializer"; | ||
@@ -587,9 +587,9 @@ | ||
/** | ||
* 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(); | ||
@@ -670,4 +670,4 @@ else if (dummy instanceof i8) return <T>this.toI64(); | ||
} | ||
return u256toa10(this); | ||
return u256toDecimalString(this); | ||
} | ||
} |
@@ -6,6 +6,3 @@ import { CharCode } from "util/string"; | ||
// @ts-ignore: decorator | ||
@lazy const HEX_CHARS = '0123456789abcdef'; | ||
// @ts-ignore: decorator | ||
@lazy const MaxBaseForExponent128: u64[] = [ | ||
@lazy const MaxBaseForExponent128 = memory.data<u64>([ | ||
u64.MAX_VALUE, // 0 | ||
@@ -50,7 +47,7 @@ u64.MAX_VALUE, // 1 | ||
0x000000000000000A, // 38 | ||
]; | ||
]); | ||
// Use LUT wrapped by function for lazy compilation | ||
// @ts-ignore: decorator | ||
@lazy const RadixCharsTable: u8[] = [ | ||
@lazy const RadixCharsTable = memory.data<u8>([ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 36, 36, 36, 36, 36, 36, | ||
@@ -61,44 +58,52 @@ 36, 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 | ||
]; | ||
]); | ||
@inline | ||
export function isPowerOverflow128(base: u128, exponent: i32): bool { | ||
if (!(exponent > 1 && base > u128.One)) return false; | ||
if (base.hi != 0 || exponent >= 128) return true; | ||
// @ts-ignore: decorator | ||
@inline export function isPowerOverflow128(base: u128, exponent: i32): bool { | ||
// never overflow | ||
if (exponent <= 1 || base <= u128.One) { | ||
return false; | ||
} | ||
// always overflow | ||
if (base.hi != 0 || exponent >= 128) { | ||
return true; | ||
} | ||
var low = base.lo; | ||
if (low <= 9) { | ||
if (low <= 10) { | ||
switch (<i32>low) { | ||
case 2: return exponent > 127; | ||
case 3: return exponent > 80; | ||
case 4: return exponent > 63; | ||
case 5: return exponent > 55; | ||
case 6: return exponent > 49; | ||
case 7: return exponent > 45; | ||
case 8: return exponent > 42; | ||
case 9: return exponent > 40; | ||
case 2: return exponent > 127; | ||
case 3: return exponent > 80; | ||
case 4: return exponent > 63; | ||
case 5: return exponent > 55; | ||
case 6: return exponent > 49; | ||
case 7: return exponent > 45; | ||
case 8: return exponent > 42; | ||
case 9: return exponent > 40; | ||
case 10: return exponent > 38; | ||
} | ||
} | ||
return low > MaxBaseForExponent128[exponent]; | ||
if (exponent >= 38) return true; | ||
return low > load<u64>(MaxBaseForExponent128 + (exponent << 3)); | ||
} | ||
// 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; | ||
@@ -108,7 +113,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); | ||
} | ||
} | ||
@@ -118,5 +125,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; | ||
@@ -128,7 +136,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); | ||
} | ||
} | ||
@@ -151,12 +161,15 @@ return result; | ||
// @ts-ignore | ||
var index = <i32>(isNeg | (first == CharCode.PLUS)); | ||
var index = i32(isNeg | (first == CharCode.PLUS)); | ||
if (str.charCodeAt(index) == CharCode._0) { | ||
let second = str.charCodeAt(++index); | ||
if (second == CharCode.x || second == CharCode.X) { | ||
if ((second | 32) == CharCode.x) { | ||
radix = 16; ++index; | ||
} else if (second == CharCode.o || second == CharCode.O) { | ||
} else if ((second | 32) == CharCode.o) { | ||
radix = 8; ++index; | ||
} else if (second == CharCode.b || second == CharCode.B) { | ||
} else if ((second | 32) == CharCode.b) { | ||
radix = 2; ++index; | ||
} else if (second == CharCode._0) { | ||
// skip leading zeros | ||
while (index < len && str.charCodeAt(index) == CharCode._0) ++index; | ||
} | ||
@@ -173,3 +186,3 @@ } | ||
let num = unchecked(table[n]); | ||
let num = load<u8>(table + n); | ||
if (num >= <u8>radix) break; | ||
@@ -211,3 +224,3 @@ | ||
let num = unchecked(table[n]); | ||
let num = load<u8>(table + n); | ||
if (num >= 16) break; | ||
@@ -228,3 +241,3 @@ | ||
let num = unchecked(table[n]); | ||
let num = load<u8>(table + n); | ||
if (num >= <u8>radix) break; | ||
@@ -231,0 +244,0 @@ |
{ | ||
"name": "as-chain", | ||
"version": "0.0.78", | ||
"version": "0.0.79", | ||
"description": "chain module for assemblyscript", | ||
@@ -5,0 +5,0 @@ "main": "js/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1576544
6719