Skip to content

Commit

Permalink
Merge pull request #12224 from malmaud/str_replace
Browse files Browse the repository at this point in the history
Re-add faster `replace`
  • Loading branch information
tkelman committed Jul 20, 2015
2 parents 78e51b4 + bbca721 commit 673bb86
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ function _rsplit{T<:AbstractString,U<:Array}(str::T, splitter, limit::Integer, k
end
#rsplit(str::AbstractString) = rsplit(str, _default_delims, 0, false)

function replace(str::ByteString, pattern, repl::Function, limit::Integer)
_replacement(repl, str, j, k) = repl
_replacement(repl::Function, str, j, k) = repl(SubString(str, j, k))

function replace(str::ByteString, pattern, repl, limit::Integer)
n = 1
e = endof(str)
i = a = start(str)
Expand All @@ -182,8 +185,8 @@ function replace(str::ByteString, pattern, repl::Function, limit::Integer)
out = IOBuffer()
while j != 0
if i == a || i <= k
write(out, SubString(str,i,prevind(str,j)))
write(out, string(repl(SubString(str,j,k))))
write_sub(out, str.data, i, j-i)
write(out, _replacement(repl, str, j, k))
end
if k<j
i = j
Expand All @@ -202,8 +205,7 @@ function replace(str::ByteString, pattern, repl::Function, limit::Integer)
write(out, SubString(str,i))
takebuf_string(out)
end
replace(s::AbstractString, pat, f::Function, n::Integer) = replace(bytestring(s), pat, f, n)
replace(s::AbstractString, pat, r, n::Integer) = replace(s, pat, x->r, n)
replace(s::AbstractString, pat, f, n::Integer) = replace(bytestring(s), pat, f, n)
replace(s::AbstractString, pat, r) = replace(s, pat, r, 0)

# hex <-> bytes conversion
Expand Down

0 comments on commit 673bb86

Please sign in to comment.