Check radix in parseInt and use constant offsets

This commit is contained in:
dcodeIO 2018-01-29 00:33:32 +01:00
parent 002fba3067
commit d3f22637ed
3 changed files with 98 additions and 61 deletions

View File

@ -348,23 +348,23 @@ const enum CharCode {
}
export function parseInt(str: String, radix: i32 = 0): i64 {
var len = str.length;
var ptr = changetype<usize>(str) + HEAD;
var len: i32 = str.length;
if (!len)
return 0; // (NaN)
var code = <i32>load<u16>(ptr);
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEAD);
// determine sign
var sign: i64;
if (code == CharCode.MINUS) {
if (!--len)
return 0; // (NaN)
code = <i32>load<u16>(ptr += 2);
code = <i32>load<u16>(ptr += 2, HEAD);
sign = -1;
} else if (code == CharCode.PLUS) {
if (!--len)
return 0; // (NaN)
code = <i32>load<u16>(ptr += 2);
code = <i32>load<u16>(ptr += 2, HEAD);
sign = 1;
} else
sign = 1;
@ -372,7 +372,7 @@ export function parseInt(str: String, radix: i32 = 0): i64 {
// determine radix
if (!radix) {
if (code == CharCode._0 && len > 2) {
switch (<i32>load<u16>(ptr + 2)) {
switch (<i32>load<u16>(ptr + 2, HEAD)) {
case CharCode.B:
case CharCode.b:
@ -396,12 +396,13 @@ export function parseInt(str: String, radix: i32 = 0): i64 {
radix = 10;
}
} else radix = 10;
}
} else if (radix < 2 || radix > 36)
return 0; // (NaN)
// calculate value
var num: i64 = 0;
while (len--) {
code = <i32>load<u16>(ptr);
code = <i32>load<u16>(ptr, HEAD);
if (code >= CharCode._0 && code <= CharCode._9)
code -= CharCode._0;
else if (code >= CharCode.A && code <= CharCode.Z)

View File

@ -375,15 +375,9 @@
(local $3 i32)
(local $4 i64)
(local $5 i64)
(set_local $3
(i32.add
(get_local $0)
(i32.const 4)
)
)
(if
(i32.eqz
(tee_local $0
(tee_local $3
(i32.load
(get_local $0)
)
@ -397,8 +391,8 @@
(if (result i64)
(i32.eq
(tee_local $2
(i32.load16_u
(get_local $3)
(i32.load16_u offset=4
(get_local $0)
)
)
(i32.const 45)
@ -406,9 +400,9 @@
(block (result i64)
(if
(i32.eqz
(tee_local $0
(tee_local $3
(i32.sub
(get_local $0)
(get_local $3)
(i32.const 1)
)
)
@ -418,10 +412,10 @@
)
)
(set_local $2
(i32.load16_u
(tee_local $3
(i32.load16_u offset=4
(tee_local $0
(i32.add
(get_local $3)
(get_local $0)
(i32.const 2)
)
)
@ -437,9 +431,9 @@
(block (result i64)
(if
(i32.eqz
(tee_local $0
(tee_local $3
(i32.sub
(get_local $0)
(get_local $3)
(i32.const 1)
)
)
@ -449,10 +443,10 @@
)
)
(set_local $2
(i32.load16_u
(tee_local $3
(i32.load16_u offset=4
(tee_local $0
(i32.add
(get_local $3)
(get_local $0)
(i32.const 2)
)
)
@ -465,15 +459,35 @@
)
)
(if
(i32.eqz
(get_local $1)
(get_local $1)
(if
(i32.and
(select
(i32.lt_s
(get_local $1)
(i32.const 2)
)
(i32.gt_s
(get_local $1)
(i32.const 36)
)
(i32.lt_s
(get_local $1)
(i32.const 2)
)
)
(i32.const 1)
)
(return
(i64.const 0)
)
)
(set_local $1
(if (result i32)
(i32.and
(select
(i32.gt_s
(get_local $0)
(get_local $3)
(i32.const 2)
)
(i32.eq
@ -496,9 +510,9 @@
(i32.or
(i32.eq
(tee_local $2
(i32.load16_u
(i32.load16_u offset=4
(i32.add
(get_local $3)
(get_local $0)
(i32.const 2)
)
)
@ -539,15 +553,15 @@
(br $case6|0)
)
)
(set_local $3
(set_local $0
(i32.add
(get_local $3)
(get_local $0)
(i32.const 4)
)
)
(set_local $0
(set_local $3
(i32.sub
(get_local $0)
(get_local $3)
(i32.const 2)
)
)
@ -555,15 +569,15 @@
(i32.const 2)
)
)
(set_local $3
(set_local $0
(i32.add
(get_local $3)
(get_local $0)
(i32.const 4)
)
)
(set_local $0
(set_local $3
(i32.sub
(get_local $0)
(get_local $3)
(i32.const 2)
)
)
@ -571,15 +585,15 @@
(i32.const 8)
)
)
(set_local $3
(set_local $0
(i32.add
(get_local $3)
(get_local $0)
(i32.const 4)
)
)
(set_local $0
(set_local $3
(i32.sub
(get_local $0)
(get_local $3)
(i32.const 2)
)
)
@ -596,10 +610,10 @@
(loop $continue|1
(if
(block (result i32)
(set_local $0
(set_local $3
(i32.sub
(tee_local $2
(get_local $0)
(get_local $3)
)
(i32.const 1)
)
@ -612,8 +626,8 @@
(select
(i32.le_s
(tee_local $2
(i32.load16_u
(get_local $3)
(i32.load16_u offset=4
(get_local $0)
)
)
(i32.const 57)
@ -717,9 +731,9 @@
)
)
)
(set_local $3
(set_local $0
(i32.add
(get_local $3)
(get_local $0)
(i32.const 2)
)
)

View File

@ -504,12 +504,6 @@
(get_local $0)
)
)
(set_local $3
(i32.add
(get_local $0)
(i32.const 4)
)
)
(if
(i32.eqz
(get_local $2)
@ -518,8 +512,11 @@
(i64.const 0)
)
)
(set_local $3
(get_local $0)
)
(set_local $4
(i32.load16_u
(i32.load16_u offset=4
(get_local $3)
)
)
@ -544,7 +541,7 @@
)
)
(set_local $4
(i32.load16_u
(i32.load16_u offset=4
(tee_local $3
(i32.add
(get_local $3)
@ -580,7 +577,7 @@
)
)
(set_local $4
(i32.load16_u
(i32.load16_u offset=4
(tee_local $3
(i32.add
(get_local $3)
@ -632,7 +629,7 @@
(block $case1|0
(block $case0|0
(set_local $6
(i32.load16_u
(i32.load16_u offset=4
(i32.add
(get_local $3)
(i32.const 2)
@ -739,6 +736,31 @@
(i32.const 10)
)
)
(if
(i32.and
(if (result i32)
(i32.ne
(i32.lt_s
(get_local $1)
(i32.const 2)
)
(i32.const 0)
)
(i32.lt_s
(get_local $1)
(i32.const 2)
)
(i32.gt_s
(get_local $1)
(i32.const 36)
)
)
(i32.const 1)
)
(return
(i64.const 0)
)
)
)
(set_local $7
(i64.const 0)
@ -761,7 +783,7 @@
(block
(block
(set_local $4
(i32.load16_u
(i32.load16_u offset=4
(get_local $3)
)
)