mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-21 18:51:43 +00:00
parseInt compatibility layer around parseI64, see #19
This commit is contained in:
4
std/assembly.d.ts
vendored
4
std/assembly.d.ts
vendored
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user