From fc82c30e0ae66b8e189e9b49375e5ebcf92f0b04 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Thu, 28 May 2020 13:36:08 +0200 Subject: [PATCH 1/2] give more information in StringIndexError --- base/strings/basic.jl | 2 +- base/strings/string.jl | 9 +++++++++ doc/src/manual/strings.md | 9 +++++---- test/strings/basic.jl | 9 +++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/base/strings/basic.jl b/base/strings/basic.jl index e36a7fe92504d..a83d6dcbc74b2 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -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: [...] ``` diff --git a/base/strings/string.jl b/base/strings/string.jl index ee9abd745fd59..7b57c21c94e3f 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -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) + 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}} diff --git a/doc/src/manual/strings.md b/doc/src/manual/strings.md index 09f4506fe7398..79ffb6576c5fb 100644 --- a/doc/src/manual/strings.md +++ b/doc/src/manual/strings.md @@ -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: [...] @@ -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: [...] @@ -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: [...] diff --git a/test/strings/basic.jl b/test/strings/basic.jl index aa6974e53a157..3927922f1312c 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -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 From e40964625a511dde7bcc8f9cadfce46f70029b06 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Wed, 3 Jun 2020 23:11:14 +0200 Subject: [PATCH 2/2] update for review --- base/strings/basic.jl | 2 +- base/strings/string.jl | 14 +++++++++----- doc/src/manual/strings.md | 8 ++++---- test/strings/basic.jl | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/base/strings/basic.jl b/base/strings/basic.jl index a83d6dcbc74b2..0212d0f39eadb 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -133,7 +133,7 @@ julia> isvalid(str, 2) false julia> str[2] -ERROR: StringIndexError: invalid index [2], valid previous index [1] ('α'), valid next index [3] ('β') +ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'α', [3]=>'β' Stacktrace: [...] ``` diff --git a/base/strings/string.jl b/base/strings/string.jl index 7b57c21c94e3f..df394f5750aaf 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -13,11 +13,15 @@ end throw(StringIndexError(s, Int(i))) function Base.showerror(io::IO, exc::StringIndexError) s = exc.string - i = thisind(s, exc.index) - 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])')") + 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 diff --git a/doc/src/manual/strings.md b/doc/src/manual/strings.md index 79ffb6576c5fb..f02acb10210a9 100644 --- a/doc/src/manual/strings.md +++ b/doc/src/manual/strings.md @@ -281,12 +281,12 @@ julia> s[1] '∀': Unicode U+2200 (category Sm: Symbol, math) julia> s[2] -ERROR: StringIndexError: invalid index [2], valid previous index [1] ('∀'), valid next index [4] (' ') +ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'∀', [4]=>' ' Stacktrace: [...] julia> s[3] -ERROR: StringIndexError: invalid index [3], valid previous index [1] ('∀'), valid next index [4] (' ') +ERROR: StringIndexError: invalid index [3], valid nearby indices [1]=>'∀', [4]=>' ' Stacktrace: [...] @@ -306,7 +306,7 @@ julia> s[end-1] ' ': ASCII/Unicode U+0020 (category Zs: Separator, space) julia> s[end-2] -ERROR: StringIndexError: invalid index [9], valid previous index [7] ('∃'), valid next index [10] (' ') +ERROR: StringIndexError: invalid index [9], valid nearby indices [7]=>'∃', [10]=>' ' Stacktrace: [...] @@ -326,7 +326,7 @@ julia> s[1:1] "∀" julia> s[1:2] -ERROR: StringIndexError: invalid index [2], valid previous index [1] ('∀'), valid next index [4] (' ') +ERROR: StringIndexError: invalid index [2], valid nearby indices [1]=>'∀', [4]=>' ' Stacktrace: [...] diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 3927922f1312c..7bac3012f7408 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -1078,8 +1078,8 @@ 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')" + @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 previous index [1] ('κ')" + @test sprint(showerror, e) == "StringIndexError: invalid index [2], valid nearby index [1]=>'κ'" end