Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -782,7 +782,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
10 changes: 10 additions & 0 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ 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,)

_checkcontiguous(::Type{Bool}, A::CodeUnits{UInt8, String}) = true
_checkcontiguous(::Type{Bool}, A::CodeUnits{UInt8, <:SubString}) = _checkcontiguous(Bool, A.string)

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