Skip to content
Closed
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
6 changes: 2 additions & 4 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,10 @@ end

function _annotatedmatch(m::RegexMatch{S}, str::AnnotatedString{S}) where {S<:AbstractString}
RegexMatch{AnnotatedString{S}}(
(@inbounds SubString{AnnotatedString{S}}(
str, m.match.offset, m.match.ncodeunits, Val(:noshift))),
_unsafe_new_substring(str, m.match.offset, m.match.ncodeunits),
Union{Nothing,SubString{AnnotatedString{S}}}[
if !isnothing(cap)
(@inbounds SubString{AnnotatedString{S}}(
str, cap.offset, cap.ncodeunits, Val(:noshift)))
_unsafe_new_substring(str, cap.offset, cap.ncodeunits)
end for cap in m.captures],
m.offset, m.offsets, m.regex)
end
Expand Down
13 changes: 11 additions & 2 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ struct SubString{T<:AbstractString} <: AbstractString
end
new(s, i, j)
end
global function _unsafe_new_substring(s::AbstractString, offset::Int, ncodeunits::Int)
new{typeof(s)}(s, offset, ncodeunits)
end
end

@propagate_inbounds SubString(s::T, i::Int, j::Int) where {T<:AbstractString} = SubString{T}(s, i, j)
Expand All @@ -56,8 +59,14 @@ end
SubString(s.string, s.offset+i, s.offset+j)
end

SubString(s::AbstractString) = SubString(s, 1, lastindex(s)::Int)
SubString{T}(s::T) where {T<:AbstractString} = SubString{T}(s, 1, lastindex(s)::Int)
function SubString(s::AbstractString)
# Note: This is always valid, and will make the construction a noop
_unsafe_new_substring(s, 0, Int(ncodeunits(s))::Int)
end

SubString{T}(s::T) where {T<:AbstractString} = SubString(s)

SubString(s::SubString) = s

@propagate_inbounds view(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, r)
@propagate_inbounds maybeview(s::AbstractString, r::AbstractUnitRange{<:Integer}) = view(s, r)
Expand Down
2 changes: 1 addition & 1 deletion base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ end
end
off = s isa String ? 0 : s.offset
par = s isa String ? s : s.string
@inbounds @inline SubString{String}(par, off, len, Val{:noshift}())
_unsafe_new_substring(par, off, len)
end
"""
lstrip([pred=isspace,] str::AbstractString)::SubString
Expand Down