Skip to content

Commit

Permalink
Don't make a copy when passing a SubString to match
Browse files Browse the repository at this point in the history
Only try to accept SubString{<:ByteString}
  • Loading branch information
malmaud committed Jul 26, 2015
1 parent 295bf63 commit 6aa853d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 5 additions & 3 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ end

call(r::Regex, s) = ismatch(r, s)

function match(re::Regex, str::UTF8String, idx::Integer, add_opts::UInt32=UInt32(0))
function match(re::Regex, str::Union{SubString{UTF8String}, UTF8String}, idx::Integer, add_opts::UInt32=UInt32(0))
compile(re)
opts = re.match_options | add_opts
if !PCRE.exec(re.regex, str, idx-1, opts, re.match_data)
Expand All @@ -152,8 +152,10 @@ function match(re::Regex, str::UTF8String, idx::Integer, add_opts::UInt32=UInt32
RegexMatch(mat, cap, ovec[1]+1, off, re)
end

match(re::Regex, str::Union{ByteString,SubString}, idx::Integer, add_opts::UInt32=UInt32(0)) =
match(re, utf8(str), idx, add_opts)
_utf8(str) = utf8(str)
_utf8(str::SubString{ASCIIString}) = convert(SubString{UTF8String}, str)
match{T<:ByteString}(re::Regex, str::Union{T,SubString{T}}, idx::Integer, add_opts::UInt32=UInt32(0)) =
match(re, _utf8(str), idx, add_opts)

match(r::Regex, s::AbstractString) = match(r, s, start(s))
match(r::Regex, s::AbstractString, i::Integer) =
Expand Down
2 changes: 2 additions & 0 deletions base/unicode/utf8.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ write(io::IO, s::UTF8String) = write(io, s.data)
utf8(x) = convert(UTF8String, x)
convert(::Type{UTF8String}, s::UTF8String) = s
convert(::Type{UTF8String}, s::ASCIIString) = UTF8String(s.data)
convert(::Type{SubString{UTF8String}}, s::SubString{ASCIIString}) =
SubString(utf8(s.string), s.offset+1, s.endof+s.offset)
convert(::Type{UTF8String}, a::Array{UInt8,1}) = isvalid(UTF8String, a) ? UTF8String(a) : throw(UnicodeError(UTF_ERR_INVALID_8))
function convert(::Type{UTF8String}, a::Array{UInt8,1}, invalids_as::AbstractString)
l = length(a)
Expand Down

0 comments on commit 6aa853d

Please sign in to comment.