Skip to content

Commit ba3d2d8

Browse files
ararslanKristofferC
authored andcommitted
Add a similar method for Type{<:CodeUnits} (#57826)
Currently, `similar(::CodeUnits)` works as expected by going through the generic `AbstractArray` method. However, the fallback method hit by `similar(::Type{<:CodeUnits}, dims)` does not work, as it assumes the existence of a constructor that accepts an `UndefInitializer`. This can be made to work by defining a corresponding `similar` method that returns an `Array`. One could make a case that this is a bugfix since it was arguably a bug that this method didn't work given that `CodeUnits` is an `AbstractArray` subtype and the other `similar` methods work. If anybody buys that argument, it could be nice to backport this; it came up in some internal code that uses Arrow.jl and JSON3.jl together. (cherry picked from commit 8e524c7)
1 parent 8180b74 commit ba3d2d8

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

base/strings/basic.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,8 @@ write(io::IO, s::CodeUnits) = write(io, s.s)
810810
cconvert(::Type{Ptr{T}}, s::CodeUnits{T}) where {T} = cconvert(Ptr{T}, s.s)
811811
cconvert(::Type{Ptr{Int8}}, s::CodeUnits{UInt8}) = cconvert(Ptr{Int8}, s.s)
812812

813+
similar(::Type{<:CodeUnits{T}}, dims::Dims) where {T} = similar(Array{T}, dims)
814+
813815
"""
814816
codeunits(s::AbstractString)
815817

test/strings/basic.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ let s = "∀x∃y", u = codeunits(s)
10781078
@test_throws Base.CanonicalIndexError (u[1] = 0x00)
10791079
@test collect(u) == b"∀x∃y"
10801080
@test Base.elsize(u) == Base.elsize(typeof(u)) == 1
1081+
@test similar(typeof(u), 3) isa Vector{UInt8}
10811082
end
10821083

10831084
@testset "issue #24388" begin

0 commit comments

Comments
 (0)