Skip to content

ParseInt Implementation #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d4c218c
ParseInt Implementation
MaxGraey Jan 10, 2018
52ca2d1
Move constans out of function
MaxGraey Jan 10, 2018
2dc07c6
Drop charCodeAt for char constant and use immediate values
MaxGraey Jan 10, 2018
d6b31e5
Fix tests
MaxGraey Jan 10, 2018
115c9d5
Explicit u16 type for constants
MaxGraey Jan 10, 2018
50062f8
Some simplifications
MaxGraey Jan 10, 2018
e73009f
Rename index -> pos
MaxGraey Jan 10, 2018
0f61d48
Some type hints
MaxGraey Jan 10, 2018
e918e20
Use break from loop instead early return with NaN
MaxGraey Jan 10, 2018
3966f36
break instead NaN other range checking
MaxGraey Jan 10, 2018
ed1c0a5
Manage '+' & '-' only signs without digits
MaxGraey Jan 11, 2018
2ef3491
Father optimizations
MaxGraey Jan 11, 2018
a7e5786
Fix some issues with partially valid numbers
MaxGraey Jan 11, 2018
2af303b
Additional fixes
MaxGraey Jan 11, 2018
3b83121
Minor fix
MaxGraey Jan 11, 2018
40103c6
Simplification
MaxGraey Jan 11, 2018
8c30dc0
Minor fixes
MaxGraey Jan 11, 2018
c5df495
Basic test peparations
MaxGraey Jan 11, 2018
a7a3416
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 12, 2018
95fb2c3
/implement-parse-int: Auto stash before merge of "std/implement-parse…
MaxGraey Jan 12, 2018
b2aaa41
Remove @global from parseInt
MaxGraey Jan 12, 2018
eaff433
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 12, 2018
d8cb0c3
Fix tests
MaxGraey Jan 12, 2018
3a841a8
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 17, 2018
e4e0880
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 19, 2018
02d0ba7
Replace var -> let
MaxGraey Jan 19, 2018
56f434a
Revert "Replace var -> let"
MaxGraey Jan 19, 2018
ece3ff7
Fix tests
MaxGraey Jan 19, 2018
d1f7d96
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 23, 2018
61da223
Update
MaxGraey Jan 23, 2018
eefade5
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 27, 2018
fb455f2
Update test fixture
MaxGraey Jan 27, 2018
142c201
Add tests for parseInt
MaxGraey Jan 27, 2018
f4b7706
Minor fixes
MaxGraey Jan 27, 2018
b42d894
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 28, 2018
37b6288
Update tests
MaxGraey Jan 28, 2018
309e232
Update u32 -> i32 in parseInt
MaxGraey Jan 28, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bin/asc
100644 → 100755
Empty file.
912 changes: 912 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

78 changes: 76 additions & 2 deletions std/assembly/string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// singleton empty string
const EMPTY: String = changetype<String>("");

const cp: u16 = 43; // "+"
const cn: u16 = 45; // "-"
const cx: u16 = 120; // "x"
const cX: u16 = 88; // "X"
const c0: u16 = 48; // "0"
const c9: u16 = 57; // "9"
const ca: u16 = 97; // "a"
const cz: u16 = 122; // "z"
const cA: u16 = 65; // "A"
const cZ: u16 = 90; // "Z"

// number of bytes preceeding string data
const HEAD: usize = 4;

Expand Down Expand Up @@ -323,8 +334,71 @@ function isWhiteSpaceOrLineTerminator(c: u16): bool {
}

// @binding(CALL, [ STRING, PASS_THRU ], PASS_THRU)
export function parseInt(str: string, radix: i32 = 10): f64 {
throw new Error("not implemented");
export function parseInt(str: String, radix: i32 = 0): f64 {
var len = <i32>str.length;
if (len == 0 || (radix != 0 && radix < 2) || radix > 36)
return NaN;

var s0 = str.charCodeAt(0);
var pos: i32 = 0;
var neg = (s0 == cn);

if (s0 == cp || neg) {
if (len == 1)
return NaN;

pos = 1;
}

if (radix == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be sufficiently covered by a radix: i32 = 10 parameter above?

Copy link
Member Author

@MaxGraey MaxGraey Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are fully justified changes. You can try in js:

console.log(parseInt('10')) // => 10
console.log(parseInt('10', 10)) // => 10

It's clear. But what about specified radix with hex prefix?

console.log(parseInt('0x10', 10)) // => 0
console.log(parseInt('0x10')) // => 16
console.log(parseInt('0x10', 0)) // => 16

So radix with zero or unspecified value mean actually auto radix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, makes sense.

if (str.charCodeAt(pos) == c0) {
var s1 = str.charCodeAt(pos + 1);
if (len > 1 && (s1 == cx || s1 == cX)) {
if (len < pos + 3)
return NaN;

radix = 16;
pos += 2;

} else {
// radix = 8;
// pos += 1;
radix = 10;
}
} else {
radix = 10;
}
}

var valid = false;
var result: f64 = 0;

for (; pos < len; ++pos) {
var digit: i32, c = str.charCodeAt(pos);

if (c0 <= c && c <= c9) digit = c - c0;
else if (ca <= c && c <= cz) digit = c - ca + 10;
else if (cA <= c && c <= cZ) digit = c - cA + 10;
else {
if (valid) break;
return NaN;
}

if (digit >= radix) {
if (valid) break;
return NaN;
}

valid = true;

result *= radix;
result += digit;
}

if (!valid && len > 2)
return NaN;

return neg ? -result : result;
}

// @binding(CALL, [ STRING ], PASS_THRU)
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/array.wast
Original file line number Diff line number Diff line change
Expand Up @@ -4065,6 +4065,16 @@
PROPERTY: std:set/Set#size
CLASS_PROTOTYPE: Set
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
GLOBAL: std:string/HEAD
FUNCTION_PROTOTYPE: std:string/allocate
CLASS_PROTOTYPE: std:string/String
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/carray.wast
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@
PROPERTY: std:set/Set#size
CLASS_PROTOTYPE: Set
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
GLOBAL: std:string/HEAD
FUNCTION_PROTOTYPE: std:string/allocate
CLASS_PROTOTYPE: std:string/String
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/heap.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,16 @@
PROPERTY: std:set/Set#size
CLASS_PROTOTYPE: Set
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
GLOBAL: std:string/HEAD
FUNCTION_PROTOTYPE: std:string/allocate
CLASS_PROTOTYPE: std:string/String
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/new.wast
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,16 @@
PROPERTY: std:set/Set#size
CLASS_PROTOTYPE: Set
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
GLOBAL: std:string/HEAD
FUNCTION_PROTOTYPE: std:string/allocate
CLASS_PROTOTYPE: std:string/String
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/set.wast
Original file line number Diff line number Diff line change
Expand Up @@ -3145,6 +3145,16 @@
PROPERTY: std:set/Set#size
CLASS_PROTOTYPE: Set
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
GLOBAL: std:string/HEAD
FUNCTION_PROTOTYPE: std:string/allocate
CLASS_PROTOTYPE: std:string/String
Expand Down
Loading