-
-
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
fix unicode indexing in parse(Complex, string) #51758
Conversation
ebdd8ae
to
8239fd6
Compare
@@ -321,14 +321,14 @@ function tryparse_internal(::Type{Complex{T}}, s::Union{String,SubString{String} | |||
if i₊ == i # leading ± sign | |||
i₊ = something(findnext(in(('+','-')), s, i₊+1), 0) | |||
end | |||
if i₊ != 0 && s[i₊-1] in ('e','E') # exponent sign | |||
if i₊ != 0 && s[prevind(s, i₊)] in ('e','E') # exponent sign |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: Checks like this could be sped up further if we used codeunits(s)
and did byte indexing/comparisons rather than Char
comparisons. It would uglify the code and maybe the speedup would be small, however?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(In any case, speedups to this function should go into a separate PR, not a bugfix PR, since speedups need not be backported.)
Unrelated CI failures, it looks like: |
This fixes a string-indexing bug introduced in #24713 (Julia 0.7). Sometimes, this would cause `parse(Complex{T}, string)` to throw a `StringIndexError` rather than an `ArgumentError`, e.g. for `parse(ComplexF64, "3 β+ 4im")` or `parse(ComplexF64, "3 + 4αm")`. (As far as I can tell, it can never cause parsing to fail for valid strings.) The source of the error is that if `i` is the index of an ASCII character in a string `s`, then `s[i+1]` is valid (even if the next character is non-ASCII) but `s[i-1]` is invalid if the previous character is non-ASCII. (cherry picked from commit f71228d)
This fixes a string-indexing bug introduced in #24713 (Julia 0.7). Sometimes, this would cause
parse(Complex{T}, string)
to throw aStringIndexError
rather than anArgumentError
, e.g. forparse(ComplexF64, "3 β+ 4im")
orparse(ComplexF64, "3 + 4αm")
. (As far as I can tell, it can never cause parsing to fail for valid strings.)The source of the error is that if
i
is the index of an ASCII character in a strings
, thens[i+1]
is valid (even if the next character is non-ASCII) buts[i-1]
is invalid if the previous character is non-ASCII.