Skip to content

Commit

Permalink
fix unicode indexing in parse(Complex, string) (#51758)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevengj authored Oct 20, 2023
1 parent 6d1c0a0 commit f71228d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 5 additions & 5 deletions base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
i₊ = something(findnext(in(('+','-')), s, i₊+1), 0)
end

# find trailing im/i/j
iᵢ = something(findprev(in(('m','i','j')), s, e), 0)
if iᵢ > 0 && s[iᵢ] == 'm' # im
iᵢ -= 1
iᵢ = prevind(s, iᵢ)
if s[iᵢ] != 'i'
raise && throw(ArgumentError("expected trailing \"im\", found only \"m\""))
return nothing
Expand All @@ -337,7 +337,7 @@ function tryparse_internal(::Type{Complex{T}}, s::Union{String,SubString{String}

if i₊ == 0 # purely real or imaginary value
if iᵢ > i && !(iᵢ == i+1 && s[i] in ('+','-')) # purely imaginary (not "±inf")
x = tryparse_internal(T, s, i, iᵢ-1, raise)
x = tryparse_internal(T, s, i, prevind(s, iᵢ), raise)
x === nothing && return nothing
return Complex{T}(zero(x),x)
else # purely real
Expand All @@ -353,11 +353,11 @@ function tryparse_internal(::Type{Complex{T}}, s::Union{String,SubString{String}
end

# parse real part
re = tryparse_internal(T, s, i, i₊-1, raise)
re = tryparse_internal(T, s, i, prevind(s, i₊), raise)
re === nothing && return nothing

# parse imaginary part
im = tryparse_internal(T, s, i₊+1, iᵢ-1, raise)
im = tryparse_internal(T, s, i₊+1, prevind(s, iᵢ), raise)
im === nothing && return nothing

return Complex{T}(re, s[i₊]=='-' ? -im : im)
Expand Down
2 changes: 2 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ end
@test_throws ArgumentError parse(Complex{T}, bad)
end
@test_throws ArgumentError parse(Complex{Int}, "3 + 4.2im")
@test_throws ArgumentError parse(ComplexF64, "3 β+ 4im")
@test_throws ArgumentError parse(ComplexF64, "3 + 4αm")
end

@testset "parse and tryparse type inference" begin
Expand Down

0 comments on commit f71228d

Please sign in to comment.