Skip to content

Commit 26f0f06

Browse files
authored
add a summary for AbstractString (#36139)
* add a summary for AbstractString
1 parent 8c65f8d commit 26f0f06

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

base/strings/basic.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ Symbol(x...) = Symbol(string(x...))
229229
convert(::Type{T}, s::T) where {T<:AbstractString} = s
230230
convert(::Type{T}, s::AbstractString) where {T<:AbstractString} = T(s)
231231

232+
## summary ##
233+
234+
function summary(io::IO, s::AbstractString)
235+
prefix = isempty(s) ? "empty" : string(ncodeunits(s), "-codeunit")
236+
print(io, prefix, " ", typeof(s))
237+
end
238+
232239
## string & character concatenation ##
233240

234241
"""
@@ -412,11 +419,11 @@ julia> thisind("α", 3)
412419
3
413420
414421
julia> thisind("α", 4)
415-
ERROR: BoundsError: attempt to access String at index [4]
422+
ERROR: BoundsError: attempt to access 2-codeunit String at index [4]
416423
[...]
417424
418425
julia> thisind("α", -1)
419-
ERROR: BoundsError: attempt to access String at index [-1]
426+
ERROR: BoundsError: attempt to access 2-codeunit String at index [-1]
420427
[...]
421428
```
422429
"""
@@ -466,7 +473,7 @@ julia> prevind("α", 1)
466473
0
467474
468475
julia> prevind("α", 0)
469-
ERROR: BoundsError: attempt to access String at index [0]
476+
ERROR: BoundsError: attempt to access 2-codeunit String at index [0]
470477
[...]
471478
472479
julia> prevind("α", 2, 2)
@@ -525,7 +532,7 @@ julia> nextind("α", 1)
525532
3
526533
527534
julia> nextind("α", 3)
528-
ERROR: BoundsError: attempt to access String at index [3]
535+
ERROR: BoundsError: attempt to access 2-codeunit String at index [3]
529536
[...]
530537
531538
julia> nextind("α", 0, 2)

doc/src/manual/strings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ Using an index less than `begin` (`1`) or greater than `end` raises an error:
205205

206206
```jldoctest helloworldstring
207207
julia> str[begin-1]
208-
ERROR: BoundsError: attempt to access String at index [0]
208+
ERROR: BoundsError: attempt to access 14-codeunit String at index [0]
209209
[...]
210210
211211
julia> str[end+1]
212-
ERROR: BoundsError: attempt to access String at index [15]
212+
ERROR: BoundsError: attempt to access 14-codeunit String at index [15]
213213
[...]
214214
```
215215

test/strings/basic.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,3 +1085,9 @@ end
10851085
e = StringIndexError(str, 2)
10861086
@test sprint(showerror, e) == "StringIndexError: invalid index [2], valid nearby index [1]=>'κ'"
10871087
end
1088+
1089+
@testset "summary" begin
1090+
@test sprint(summary, "foα") == "4-codeunit String"
1091+
@test sprint(summary, SubString("foα", 2)) == "3-codeunit SubString{String}"
1092+
@test sprint(summary, "") == "empty String"
1093+
end

0 commit comments

Comments
 (0)