parseInt compatibility layer around parseI64, see #19

This commit is contained in:
dcodeIO
2018-01-30 01:26:38 +01:00
parent cae89e0b1f
commit ab5a938ea0
9 changed files with 282 additions and 41 deletions

4
std/assembly.d.ts vendored
View File

@ -189,10 +189,12 @@ declare function changetype<T>(value: any): T;
declare function isNaN<T = f32 | f64>(value: T): bool;
/** Tests if a 32-bit or 64-bit float is finite, that is not `NaN` or +/-`Infinity`. */
declare function isFinite<T = f32 | f64>(value: T): bool;
/** Traps if the specified value is not true-ish, otherwise returns the value. */
/** Traps if the specified value is not true-ish, otherwise returns the (non-nullable) value. */
declare function assert<T>(isTrueish: T, message?: string): T & object; // any better way to model `: T != null`?
/** Parses an integer string to a 64-bit float. */
declare function parseInt(str: string, radix?: i32): f64;
/** Parses an integer string to a 64-bit integer. */
declare function parseI64(str: string, radix?: i32): i64;
/** Parses a string to a 64-bit float. */
declare function parseFloat(str: string): f64;

View File

@ -350,7 +350,30 @@ const enum CharCode {
z = 0x7A
}
export function parseInt(str: String, radix: i32 = 0): i64 {
export function parseInt(str: String, radix: i32 = 0): f64 {
var len: i32 = str.length;
if (!len)
return NaN;
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEAD);
// pre-check sign
if (code == CharCode.MINUS) {
if (!--len)
return NaN;
} else if (code == CharCode.PLUS) {
if (!--len)
return NaN;
}
// pre-check radix
if (radix && (radix < 2 || radix > 36))
return NaN;
return <f64>parseI64(str, radix);
}
export function parseI64(str: String, radix: i32 = 0): i64 {
var len: i32 = str.length;
if (!len)
return 0; // (NaN)
@ -422,7 +445,7 @@ export function parseInt(str: String, radix: i32 = 0): i64 {
return sign * num;
}
export function parseFloat(str: string): f64 {
export function parseFloat(str: String): f64 {
var len: i32 = str.length;
if (!len)
return NaN;