Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ iterate(r::Iterators.Reverse{<:EachStringIndex}, i=lastindex(r.itr.s)) = i < fir
Wrap a string (without copying) in an immutable vector-like object that accesses the code units
of the string's representation.
"""
struct CodeUnits{T,S<:AbstractString} <: DenseVector{T}
struct CodeUnits{T,S<:AbstractString} <: AbstractVector{T}
s::S
CodeUnits(s::S) where {S<:AbstractString} = new{codeunit(s),S}(s)
end
Expand Down
7 changes: 7 additions & 0 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ convert(::Type{Union{String, SubString{String}}}, s::String) = s
convert(::Type{Union{String, SubString{String}}}, s::SubString{String}) = s
convert(::Type{Union{String, SubString{String}}}, s::AbstractString) = convert(String, s)::String

# This allows CodeUnits of known, memory-backed String types to act like a dense array
# See issue #53996
function _memory_offset(x::CodeUnits{<:Any, <:Union{String, SubString{String}}}, I::Vararg{Any,N}) where {N}
(_to_linear_index(x, I...) - first(LinearIndices(x)))*elsize(x)
end
strides(x::CodeUnits{<:Any, <:Union{String, SubString{String}}}) = (1,)

function String(s::SubString{String})
parent = s.string
copy = GC.@preserve parent unsafe_string(pointer(parent, s.offset+1), s.ncodeunits)
Expand Down
Loading