Skip to content

Commit 1d294f8

Browse files
authored
Show type for IdOffsetRange (#143)
Display an `IdOffsetRange` using its type, except in the summary of an OffsetArray
1 parent 55af8ad commit 1d294f8

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/OffsetArrays.jl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,21 +299,19 @@ indexlength(r::AbstractRange) = length(r)
299299
indexlength(i::Integer) = i
300300
indexlength(i::Colon) = Colon()
301301

302+
# These functions keep the summary compact
303+
function Base.inds2string(inds::Tuple{Vararg{Union{IdOffsetRange, IdentityUnitRange{<:IdOffsetRange}}}})
304+
Base.inds2string(map(UnitRange, inds))
305+
end
306+
Base.showindices(io::IO, ind1::IdOffsetRange, inds::IdOffsetRange...) = Base.showindices(io, map(UnitRange, (ind1, inds...))...)
307+
302308
function Base.showarg(io::IO, a::OffsetArray, toplevel)
303309
print(io, "OffsetArray(")
304310
Base.showarg(io, parent(a), false)
305-
if ndims(a) > 0
306-
print(io, ", ")
307-
printindices(io, axes(a)...)
308-
end
311+
Base.showindices(io, axes(a)...)
309312
print(io, ')')
310313
toplevel && print(io, " with eltype ", eltype(a))
311314
end
312-
printindices(io::IO, ind1, inds...) =
313-
(print(io, _unslice(ind1), ", "); printindices(io, inds...))
314-
printindices(io::IO, ind1) = print(io, _unslice(ind1))
315-
_unslice(x) = x
316-
_unslice(x::IdentityUnitRange) = x.indices
317315

318316
function Base.replace_in_print_matrix(A::OffsetArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString)
319317
J = map(parentindex, axes(A), (i,j))

src/axes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), r::IdO
163163
Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), x::Integer, r::IdOffsetRange{T}) where T =
164164
IdOffsetRange{T}(x .+ r.parent, r.offset)
165165

166-
Base.show(io::IO, r::IdOffsetRange) = print(io, first(r), ':', last(r))
166+
Base.show(io::IO, r::IdOffsetRange) = print(io, "OffsetArrays.IdOffsetRange(",first(r), ':', last(r),")")
167167

168168
# Optimizations
169169
@inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset)

test/runtests.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,32 @@ end
502502
a = OffsetArray(reshape([1]))
503503
@test summary(a) == "0-dimensional OffsetArray(::$(typeof(parent(a)))) with eltype $(Int)"
504504

505+
a = OffsetArray([1 2; 3 4], -1:0, 5:6)
506+
io = IOBuffer()
507+
show(io, axes(a, 1))
508+
@test String(take!(io)) == "OffsetArrays.IdOffsetRange(-1:0)"
509+
show(io, axes(a, 2))
510+
@test String(take!(io)) == "OffsetArrays.IdOffsetRange(5:6)"
511+
512+
@test Base.inds2string(axes(a)) == Base.inds2string(map(UnitRange, axes(a)))
513+
505514
show(io, OffsetArray(3:5, 0:2))
506515
@test String(take!(io)) == "3:5 with indices 0:2"
507516

508517
d = Diagonal([1,2,3])
509518
Base.print_array(io, d)
510519
s1 = String(take!(io))
511-
Base.print_array(io, OffsetArray(d, -1:1, 3:5))
520+
od = OffsetArray(d, -1:1, 3:5)
521+
Base.print_array(io, od)
512522
s2 = String(take!(io))
513523
@test s1 == s2
524+
525+
@test Base.replace_in_print_matrix(od, -1, 3, " ") == Base.replace_in_print_matrix(d, 1, 1, " ")
526+
@test Base.replace_in_print_matrix(od, -1, 4, " ") == Base.replace_in_print_matrix(d, 1, 2, " ")
527+
528+
v = rand(3)
529+
ov = OffsetArray(v, (-2,))
530+
@test Base.replace_in_print_matrix(ov, -1, 1, " ") == Base.replace_in_print_matrix(v, 1, 1, " ")
514531
end
515532

516533
@testset "readdlm/writedlm" begin

0 commit comments

Comments
 (0)