Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upImprove String.toInt and String.toFloat #818
Conversation
evancz
added some commits
Jan 20, 2017
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
process-bot
Jan 20, 2017
Thanks for the pull request! Make sure it satisfies this checklist. My human colleagues will appreciate it!
Here is what to expect next, and if anyone wants to comment, keep these things in mind.
process-bot
commented
Jan 20, 2017
|
Thanks for the pull request! Make sure it satisfies this checklist. My human colleagues will appreciate it! Here is what to expect next, and if anyone wants to comment, keep these things in mind. |
evancz
referenced this pull request
Jan 20, 2017
Closed
Improve String.toFloat and String.toInt #817
src/Native/String.js
| { | ||
| if (len === 1) | ||
| // must be hex | ||
| if (s[1] !== 'x') |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
brian-carroll
Jan 20, 2017
If this is a number of minutes that I've extracted from a time-formatted string, like "09", it will fail. Is that desired behaviour?
brian-carroll
Jan 20, 2017
If this is a number of minutes that I've extracted from a time-formatted string, like "09", it will fail. Is that desired behaviour?
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
mbylstra
Jan 20, 2017
Any chance you could also accept binary literals? eg 0b1010101 or 0B1010101. These are really helpful if you have to deal with parsing or generating binary file formats. Eg: If I just want the last 5 bits of a byte:
Bitwise.and myByte (String.toInt "0b11111")
With hex it's
Bitwise.and myByte 0x1F
which is far less readable. Parsing binary formats is really hard - this makes it a little easier!
mbylstra
commented
Jan 20, 2017
|
Any chance you could also accept binary literals? eg
With hex it's
which is far less readable. Parsing binary formats is really hard - this makes it a little easier! |
| , goodFloat "6.022e+23" 6.022e23 | ||
| , badFloat "6.022e" | ||
| , badFloat "6.022n" | ||
| , badFloat "6.022.31" |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
eeue56
Jan 21, 2017
Contributor
fuzz int "All ints are correctly converted to a string and back" (\int ->
Expect.equal (Ok int) (String.toInt (toString int))
eeue56
Jan 21, 2017
•
Contributor
fuzz int "All ints are correctly converted to a string and back" (\int ->
Expect.equal (Ok int) (String.toInt (toString int))
src/Native/String.js
| } | ||
| return _elm_lang$core$Result$Ok(parseFloat(s)); | ||
| var n = +s; | ||
| return n === n ? _elm_lang$core$Result$Ok(n) : floatErr(s); |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
eeue56
Jan 21, 2017
Contributor
isNaN? is it meant to be faster? If so, a comment might be worthwhile here since it's not obvious
eeue56
Jan 21, 2017
Contributor
isNaN? is it meant to be faster? If so, a comment might be worthwhile here since it's not obvious
src/Native/String.js
| } | ||
| var dotCount = 0; | ||
| for (var i = start; i < len; ++i) | ||
| if (s.length === 0 || /[\sxbo]/.test(s)) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
eeue56
Jan 21, 2017
Contributor
eeue56
Jan 21, 2017
Contributor
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
crazymykl
Jan 23, 2017
Also, many languages (e. g. JavaScript, Ruby) allow capital letters for indicating base, 0XF and similar.
crazymykl
Jan 23, 2017
Also, many languages (e. g. JavaScript, Ruby) allow capital letters for indicating base, 0XF and similar.
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
justinmimbs
Jan 21, 2017
The toInt function could be simplified with regexes. For example, the version below performs the same checks (but allows strings with leading zeros), and is a bit faster in some brief timing tests.
I think @brian-carroll brings up a good point. I would want "09" to parse without error.
function toInt(s)
{
// decimal
if (/^[+-]?\d+$/.test(s))
{
return _elm_lang$core$Result$Ok(parseInt(s, 10));
}
// hex
if (/^0[Xx][\dA-Fa-f]+$/.test(s))
{
return _elm_lang$core$Result$Ok(parseInt(s, 16));
}
return intErr(s);
}(Edited hex regex to allow "0X" prefix, as pointed out by @crazymykl.)
justinmimbs
commented
Jan 21, 2017
•
|
The I think @brian-carroll brings up a good point. I would want function toInt(s)
{
// decimal
if (/^[+-]?\d+$/.test(s))
{
return _elm_lang$core$Result$Ok(parseInt(s, 10));
}
// hex
if (/^0[Xx][\dA-Fa-f]+$/.test(s))
{
return _elm_lang$core$Result$Ok(parseInt(s, 16));
}
return intErr(s);
}(Edited hex regex to allow "0X" prefix, as pointed out by @crazymykl.) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
mbylstra
Jan 21, 2017
Something to keep in mind with parsing numbers like 09 is that a leading zero is often used as syntax for Octal representations. I'm not saying 09 should be parsed as an Octal number, it's just something to consider.
mbylstra
commented
Jan 21, 2017
•
|
Something to keep in mind with parsing numbers like |
evancz
added some commits
Jan 23, 2017
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Jan 23, 2017
Member
The alternate toInt implementation is not faster by my tests. The results reverse by swapping the order of the tests, so I suspect this has more to do with "VM warm up" than the actual details.
About parsing 0b1111, the code you write does not type check. toInt produces a Result. It'd be better to just allow binary digit literals, but that's a separate discussion that I do not want to have right now.
About leading zeros, I only know of JS making that octal, and it's getting phased out. I would prefer not to allow "too many" zeros at the front, but to be backwards compatible and release this as a core patch, I need to keep allowing it for now.
I've responded to other things with some code changes.
|
The alternate About parsing About leading zeros, I only know of JS making that octal, and it's getting phased out. I would prefer not to allow "too many" zeros at the front, but to be backwards compatible and release this as a core patch, I need to keep allowing it for now. I've responded to other things with some code changes. |
evancz
merged commit f2c29f5
into
master
Jan 23, 2017
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Thanks for the reviews everyone! |
evancz commentedJan 20, 2017
With some parser work I’m doing, I wanted to expand the set of numbers that Elm can easily turn into
IntandFloatvalues.Changes
String.toFloataccepts scientific notation like1e+40String.toIntaccepts hex like0xBEEF