Skip to content

Commit

Permalink
give more information in StringIndexError (#36054)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC authored Jun 5, 2020
1 parent fa102c1 commit 8c65f8d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
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 nearby indices [1]=>'α', [3]=>'β'
Stacktrace:
[...]
```
Expand Down
13 changes: 13 additions & 0 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ 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
print(io, "StringIndexError: ", "invalid index [$(exc.index)]")
if firstindex(s) <= exc.index <= ncodeunits(s)
iprev = thisind(s, exc.index)
inext = nextind(s, iprev)
if inext <= ncodeunits(s)
print(io, ", valid nearby indices [$iprev]=>'$(s[iprev])', [$inext]=>'$(s[inext])'")
else
print(io, ", valid nearby index [$iprev]=>'$(s[iprev])'")
end
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 @@ -279,11 +279,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 nearby indices [1]=>'∀', [4]=>' '
Stacktrace:
[...]
julia> s[3]
ERROR: StringIndexError("∀ x ∃ y", 3)
ERROR: StringIndexError: invalid index [3], valid nearby indices [1]=>'∀', [4]=>' '
Stacktrace:
[...]
Expand All @@ -303,7 +304,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 nearby indices [7]=>'∃', [10]=>' '
Stacktrace:
[...]
Expand All @@ -323,7 +324,7 @@ julia> s[1:1]
"∀"
julia> s[1:2]
ERROR: StringIndexError("∀ x ∃ y", 2)
ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'∀', [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 @@ -1076,3 +1076,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 nearby indices [9]=>'κ', [11]=>'i'"
str = "κ"
e = StringIndexError(str, 2)
@test sprint(showerror, e) == "StringIndexError: invalid index [2], valid nearby index [1]=>'κ'"
end

2 comments on commit 8c65f8d

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.