-
-
Notifications
You must be signed in to change notification settings - Fork 185
Implement parseInt and parseFloat #920
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| function __TS__ParseFloat(this: void, numberString: string): number { | ||
| // Check if string is infinity | ||
| const infinityMatch = string.match(numberString, "^%s*(-?Infinity)"); | ||
| if (infinityMatch) { | ||
| // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with | ||
| return infinityMatch[0] === "-" ? -Infinity : Infinity; | ||
| } | ||
|
|
||
| const number = tonumber(string.match(numberString, "^%s*(-?%d+%.?%d*)")); | ||
| return number ?? NaN; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| const __TS__parseInt_base_pattern = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTvVwWxXyYzZ"; | ||
|
|
||
| function __TS__ParseInt(this: void, numberString: string, base?: number): number { | ||
| // Check which base to use if none specified | ||
| if (base === undefined) { | ||
| base = 10; | ||
| const hexMatch = string.match(numberString, "^%s*-?0[xX]"); | ||
| if (hexMatch) { | ||
| base = 16; | ||
| numberString = string.match(hexMatch, "-") | ||
| ? "-" + numberString.substr(hexMatch.length) | ||
| : numberString.substr(hexMatch.length); | ||
| } | ||
| } | ||
|
|
||
| // Check if base is in bounds | ||
| if (base < 2 || base > 36) { | ||
| return NaN; | ||
| } | ||
|
|
||
| // Calculate string match pattern to use | ||
| const allowedDigits = | ||
| base <= 10 | ||
| ? __TS__parseInt_base_pattern.substring(0, base) | ||
| : __TS__parseInt_base_pattern.substr(0, 10 + 2 * (base - 10)); | ||
| const pattern = `^%s*(-?[${allowedDigits}]*)`; | ||
|
|
||
| // Try to parse with Lua tonumber | ||
| const number = tonumber(string.match(numberString, pattern), base); | ||
|
|
||
| if (number === undefined) { | ||
| return NaN; | ||
| } | ||
|
|
||
| // Lua uses a different floor convention for negative numbers than JS | ||
| if (number >= 0) { | ||
| return math.floor(number); | ||
| } else { | ||
| return math.ceil(number); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.