-
Notifications
You must be signed in to change notification settings - Fork 456
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
[Belt] Stricter parse for Int and Float #5209
Conversation
The actual implementation of Belt.Int.fromString relies on the Javascript `parseInt` function [0]. That function parses strings in a permissive manner. For instance, if the function encounters an alphabetical character, it returns the value up to that character: parseInt("123a456") // output: 123 The same applies for Belt.Float.fromString that relies on `parseFloat` [1] and have a similar interpretation. This commit proposes a stricter implementation of Int and Float parsing using the Javascript `Number` constructor [2]. The Belt.Int.fromString uses the more specific `Math.trunc` function [3], that parses string similarly to `Number` and then returns the Int part of that number. Relying only on the `Number` constructor keeps the implementation simple. However, there exists one last quirk while using it with an empty string. It returns the `0` value: Number("") // ouptput: 0 Consequently, Belt.Int.fromString("") will return `0`. Tests for Belt.(Int/Float).fromString have been updated in order to reflect that. Fix rescript-lang#3732 [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number [3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc Signed-Off-By: Ronan-A. Cherrueau <[email protected]>
relevant #3732, I think it is okay to introduce a breaking change in v10.
I would suggest here we just use a handwritten one which has clear specification, reference implementation (need some error/overflow checking):
|
I understand. In that case, may I suggest to follow an implementation similar to Elm [0]. I tested its performance against the implementations in #3732 (comment), and the result is really good [1]. However, even with that implementation the parsing of |
Since v10 is coming soon, perhaps this can be revisited. |
Feel free to reopen if still relevant. |
The actual implementation of Belt.Int.fromString relies on the
Javascript
parseInt
function [0]. That function parses Strings in apermissive manner. For instance, if the function encounters an
alphabetical character, it returns the value up to that character:
The same applies for Belt.Float.fromString that relies on
parseFloat
[1] and have a similar interpretation.This commit proposes a stricter implementation of Int and Float parsing
using the Javascript
Number
constructor [2]. The Belt.Int.fromStringuses the more specific
Math.trunc
function [3], that parses stringsimilarly to Number and then returns the Int part of that number.
Relying only on the
Number
constructor keeps the implementation simple.However, there exists one last quirk while using it with an empty
string. It returns the
0
value:Consequently, Belt.Int.fromString("") will return
0
. Tests forBelt.(Int/Float).fromString have been updated in order to reflect that.
Fix #3732
[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number
[3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc