Skip to content

Commit 3104bec

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 8cf6158 commit 3104bec

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
@@ -795,6 +795,8 @@ write(io::IO, s::CodeUnits) = write(io, s.s)
795795
unsafe_convert(::Type{Ptr{T}}, s::CodeUnits{T}) where {T} = unsafe_convert(Ptr{T}, s.s)
796796
unsafe_convert(::Type{Ptr{Int8}}, s::CodeUnits{UInt8}) = unsafe_convert(Ptr{Int8}, s.s)
797797

798+
similar(::Type{<:CodeUnits{T}}, dims::Dims) where {T} = similar(Array{T}, dims)
799+
798800
"""
799801
codeunits(s::AbstractString)
800802

test/strings/basic.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ let s = "∀x∃y", u = codeunits(s)
10731073
@test_throws Base.CanonicalIndexError (u[1] = 0x00)
10741074
@test collect(u) == b"∀x∃y"
10751075
@test Base.elsize(u) == Base.elsize(typeof(u)) == 1
1076+
@test similar(typeof(u), 3) isa Vector{UInt8}
10761077
end
10771078

10781079
# issue #24388

0 commit comments

Comments
 (0)