Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] support multiple s=>r Pairs in strings replace method #35414

Closed
wants to merge 17 commits into from
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
51 changes: 51 additions & 0 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,57 @@ replace(s::AbstractString, pat_f::Pair; count=typemax(Int)) =

# TODO: allow transform as the first argument to replace?

@inline rangify(c::T) where T<:AbstractRange = c
@inline rangify(c::T) where T<:Integer = c:c
@inline rangify(c::Nothing) = c

function multi_replace(str::String,count::Integer,subs::NTuple{N,Pair}) where N
sh=floor(Int,1.2sizeof(str))
@inbounds(count > 0 && for si in 1:N
s,r = subs[si]
R=rangify(findfirst(s,str))
isnothing(R) && continue
buf = IOBuffer(sizehint = sh)
i,j = first(R),last(R)
H,n = multi_replace(str[begin:(i - 1)], count, subs[(si + 1):end])
count -= n
totrepl = n
print(buf,H)
if count > 0
print(buf,replace(str[R],s=>r))
count -= 1
totrepl += 1
else
print(buf,str[R])
end
T,n=multi_replace(str[(j + 1):end], count, subs)
totrepl += n
print(buf,T)
return String(take!(buf)), totrepl
end)
str, 0
end

function replace(str::String, subs::Pair...; count::Integer=typemax(Int))
multi_replace(str, count, subs)[1]
end

const _ReplaceCharToTS=Pair{Char,B} where {B<:Union{AbstractChar,AbstractString,Number}}
function replace(str::String,mapping::_ReplaceCharToTS...;count::Integer = typemax(Int))
d=Dict(mapping...)
sizestr = sizeof(str)
buf = IOBuffer(sizehint=sizestr+sizestr>>1)
for c in str
if count > 0 && haskey(d,c)
count -= 1
print(buf,@inbounds d[c])
else
print(buf, c)
end
end
String(take!(buf))
end

# hex <-> bytes conversion

"""
Expand Down
10 changes: 10 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ end
@test replace("a", in("a") => typeof) == "Char"
@test replace("a", ['a'] => typeof) == "Char"

# PR 35414
@test replace("foobarbaz","oo"=>"zz","ar"=>"zz","z"=>"m") == "fzzbzzbam"
substmp=["z"=>"m","oo"=>"zz","ar"=>"zz"]
for perm in [[1,2,3],[2,1,3],[3,2,1],[2,3,1],[1,3,2],[3,1,2]]
@test replace("foobarbaz",substmp[perm]...) == "fzzbzzbam"
@test replace("foobarbaz",substmp[perm]...,count=2) == "fzzbzzbaz"
@test replace("foobarbaz",substmp[perm]...,count=1) == "fzzbarbaz"
end
@test replace("foobarbaz","z"=>"m",r"a.*a"=>uppercase) == "foobARBAm"
@test replace("foobarbaz",'o'=>'z','a'=>'q','z'=>'m') == "fzzbqrbqm"
end

@testset "chomp/chop" begin
Expand Down