Skip to content

Commit 8c65f8d

Browse files
authored
give more information in StringIndexError (#36054)
1 parent fa102c1 commit 8c65f8d

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

base/strings/basic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ julia> isvalid(str, 2)
133133
false
134134
135135
julia> str[2]
136-
ERROR: StringIndexError("αβγdef", 2)
136+
ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'α', [3]=>'β'
137137
Stacktrace:
138138
[...]
139139
```

base/strings/string.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ struct StringIndexError <: Exception
1111
end
1212
@noinline string_index_err(s::AbstractString, i::Integer) =
1313
throw(StringIndexError(s, Int(i)))
14+
function Base.showerror(io::IO, exc::StringIndexError)
15+
s = exc.string
16+
print(io, "StringIndexError: ", "invalid index [$(exc.index)]")
17+
if firstindex(s) <= exc.index <= ncodeunits(s)
18+
iprev = thisind(s, exc.index)
19+
inext = nextind(s, iprev)
20+
if inext <= ncodeunits(s)
21+
print(io, ", valid nearby indices [$iprev]=>'$(s[iprev])', [$inext]=>'$(s[inext])'")
22+
else
23+
print(io, ", valid nearby index [$iprev]=>'$(s[iprev])'")
24+
end
25+
end
26+
end
1427

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

doc/src/manual/strings.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,12 @@ julia> s[1]
279279
'∀': Unicode U+2200 (category Sm: Symbol, math)
280280
281281
julia> s[2]
282-
ERROR: StringIndexError("∀ x ∃ y", 2)
282+
ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'∀', [4]=>' '
283+
Stacktrace:
283284
[...]
284285
285286
julia> s[3]
286-
ERROR: StringIndexError("∀ x ∃ y", 3)
287+
ERROR: StringIndexError: invalid index [3], valid nearby indices [1]=>'∀', [4]=>' '
287288
Stacktrace:
288289
[...]
289290
@@ -303,7 +304,7 @@ julia> s[end-1]
303304
' ': ASCII/Unicode U+0020 (category Zs: Separator, space)
304305
305306
julia> s[end-2]
306-
ERROR: StringIndexError("∀ x ∃ y", 9)
307+
ERROR: StringIndexError: invalid index [9], valid nearby indices [7]=>'∃', [10]=>' '
307308
Stacktrace:
308309
[...]
309310
@@ -323,7 +324,7 @@ julia> s[1:1]
323324
"∀"
324325
325326
julia> s[1:2]
326-
ERROR: StringIndexError("∀ x ∃ y", 2)
327+
ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'∀', [4]=>' '
327328
Stacktrace:
328329
[...]
329330

test/strings/basic.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,3 +1076,12 @@ let x = SubString("ab", 1, 1)
10761076
@test y === x
10771077
chop("ab") === chop.(["ab"])[1]
10781078
end
1079+
1080+
@testset "show StringIndexError" begin
1081+
str = "abcdefghκijklmno"
1082+
e = StringIndexError(str, 10)
1083+
@test sprint(showerror, e) == "StringIndexError: invalid index [10], valid nearby indices [9]=>'κ', [11]=>'i'"
1084+
str = "κ"
1085+
e = StringIndexError(str, 2)
1086+
@test sprint(showerror, e) == "StringIndexError: invalid index [2], valid nearby index [1]=>'κ'"
1087+
end

0 commit comments

Comments
 (0)