Fix parsing numbers with padded whitespaces (#607)

This commit is contained in:
Max Graey 2019-05-27 15:37:07 +03:00 committed by Daniel Wirtz
parent 0339d82781
commit 9c51f1332c
5 changed files with 2287 additions and 1988 deletions

View File

@ -547,6 +547,11 @@ export function parseFloat(str: String): f64 {
// determine sign
var sign: f64;
// trim white spaces
while (isWhiteSpaceOrLineTerminator(code)) {
code = <i32>load<u16>(ptr += 2);
--len;
}
if (code == CharCode.MINUS) {
if (!--len) return NaN;
code = <i32>load<u16>(ptr += 2);

View File

@ -40,7 +40,7 @@ export const enum CharCode {
z = 0x7A
}
export function isWhiteSpaceOrLineTerminator(c: u16): bool {
export function isWhiteSpaceOrLineTerminator(c: i32): bool {
switch (c) {
case 9: // <TAB>
case 10: // <LF>
@ -67,6 +67,11 @@ export function parse<T>(str: string, radix: i32 = 0): T {
// determine sign
var sign: T;
// trim white spaces
while (isWhiteSpaceOrLineTerminator(code)) {
code = <i32>load<u16>(ptr += 2);
--len;
}
if (code == CharCode.MINUS) {
// @ts-ignore: cast
if (!--len) return <T>NaN;

File diff suppressed because it is too large Load Diff

View File

@ -76,12 +76,17 @@ assert(parseInt("0xF0F") == 0xf0f);
assert(parseInt("011") == 11); // not octal
assert(parseInt("0x1g") == 1); // not valid
assert(parseInt(" \t\n1") == 1);
assert(parseInt(" \t\n0x02") == 2);
assert(parseFloat("0") == 0);
assert(parseFloat("1") == 1);
assert(parseFloat("0.1") == 0.1);
assert(parseFloat(".25") == 0.25);
assert(parseFloat(".1foobar") == 0.1);
assert(parseFloat(" \t\n.1") == 0.1);
{
let c = "a" + "b";
assert(c == "ab");

File diff suppressed because it is too large Load Diff