-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
string to integer conversion with newline #697
Comments
This is probably a good thing from a pragmatic perspective. What about other kinds of trailing whitespace? Should that be an error? Then there's also various combinations of |
It appears that
In fact they might be much too carefree:
I'm not sure what the desired behavior is, but I'm pretty sure it isn't that! Regardless of the desired behavior there should be some consistent behavior for converting strings to a given type. I'd vote for stripping all whitespace as whitespace has no meaning for most type conversion and is consistent with some other languages. |
Completely agreed. Int is way too strict (written by use in Julia code) and float is way too liberal (I think we're just using a standard C parsing function there). |
As you can see from the automatic message above, I fixed the behaviour for the int case (the easy one). I did not push it in julia master because I wanted to make sure that it makes sense to substitute About the floating point case, I see 2 ways of achieving the desired behaviour: reimplementing everything in Julia, or just do a preliminary sanity check and delegating the real work to const _jl_float_regex = r"^\s*([+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?|[Nn][Aa][Nn]|[Ii][Nn][Ff])\s*$"
_jl_float_isvalid_regex(s::String) = matches(_jl_float_regex, s) Now, apart from the fact that regexes cannot be defined within Two more things I found out while experimenting:
|
We can use the end pointer strtod returns to see if there's junk after the converted number. We have |
I implemented the first part here and it seems to work fine. I'm assuming that About denormals: from what you write it seems we could check if |
Here's my implementation:
|
Yes, testing for non-zero/non-inf results gets us denormals as well. |
Yes, that's better, So, if it's ok to throw exceptions in |
I tried replacing some of the |
Ok your version with strcat also works for me...I wonder what happened. |
Now the version with the modified if !float64_isvalid(s, tmp)
throw(ArgumentError(strcat("float64(String): invalid number format (in ",show_to_string(s),")")))
end then there goes my RAM again. Can you reproduce this? |
OK, that one gives me a stack overflow. Not 100% sure why, but it has something to do with the circular dependencies involved in string interpolation. Man I hate string interpolation; its only purpose is to serve the (apparently) deep human need for horrible broken syntax. |
In Julia passing a string that contains an integer and ends in a newline causes int() to error(). In other languages if the last character is a newline int() doesn't complain. Example:
I think that "1\n" should be valid and "1\n2" should be invalid. If there is a newline and it isn't the last character in the string, then the operation is ill defined. This is how Python's int works by the way.
The thinking for this behavior comes from the following use case:
It is pretty common, at least for me, to write parsers for simple file formats where there is a single number per line. Currently you would need to call chomp() before int().
The text was updated successfully, but these errors were encountered: