Skip to content
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

give more information in StringIndexError #36054

Merged
merged 2 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ julia> isvalid(str, 2)
false

julia> str[2]
ERROR: StringIndexError("αβγdef", 2)
ERROR: StringIndexError: invalid index [2], valid previous index [1] ('α'), valid next index [3] ('β')
Stacktrace:
[...]
```
Expand Down
9 changes: 9 additions & 0 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ struct StringIndexError <: Exception
end
@noinline string_index_err(s::AbstractString, i::Integer) =
throw(StringIndexError(s, Int(i)))
function Base.showerror(io::IO, exc::StringIndexError)
s = exc.string
i = thisind(s, exc.index)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringIndexError is exported and may be (mis)used by user code — maybe defensively check for exc.index < firstindex(s) && return invoke(showerror, Tuple{IO, Any}, io, exc) before doing something that may throw?

print(io, "StringIndexError: ", "invalid index [$(exc.index)], valid previous index [$i] ('$(s[i])')")
inext = nextind(s, i)
if !(inext > ncodeunits(s))
print(io, ", valid next index [$(inext)] ('$(s[inext])')")
end
end

const ByteArray = Union{Vector{UInt8},Vector{Int8}}

Expand Down
9 changes: 5 additions & 4 deletions doc/src/manual/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,12 @@ julia> s[1]
'∀': Unicode U+2200 (category Sm: Symbol, math)

julia> s[2]
ERROR: StringIndexError("∀ x ∃ y", 2)
ERROR: StringIndexError: invalid index [2], valid previous index [1] ('∀'), valid next index [4] (' ')
Stacktrace:
[...]

julia> s[3]
ERROR: StringIndexError("∀ x ∃ y", 3)
ERROR: StringIndexError: invalid index [3], valid previous index [1] ('∀'), valid next index [4] (' ')
Stacktrace:
[...]

Expand All @@ -305,7 +306,7 @@ julia> s[end-1]
' ': ASCII/Unicode U+0020 (category Zs: Separator, space)

julia> s[end-2]
ERROR: StringIndexError("∀ x ∃ y", 9)
ERROR: StringIndexError: invalid index [9], valid previous index [7] ('∃'), valid next index [10] (' ')
Stacktrace:
[...]

Expand All @@ -325,7 +326,7 @@ julia> s[1:1]
"∀"

julia> s[1:2]
ERROR: StringIndexError("∀ x ∃ y", 2)
ERROR: StringIndexError: invalid index [2], valid previous index [1] ('∀'), valid next index [4] (' ')
Stacktrace:
[...]

Expand Down
9 changes: 9 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1074,3 +1074,12 @@ let x = SubString("ab", 1, 1)
@test y === x
chop("ab") === chop.(["ab"])[1]
end

@testset "show StringIndexError" begin
str = "abcdefghκijklmno"
e = StringIndexError(str, 10)
@test sprint(showerror, e) == "StringIndexError: invalid index [10], valid previous index [9] ('κ'), valid next index [11] ('i')"
str = "κ"
e = StringIndexError(str, 2)
@test sprint(showerror, e) == "StringIndexError: invalid index [2], valid previous index [1] ('κ')"
end