Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
malmaud committed Jul 6, 2015
1 parent a9fcb72 commit a4a75dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ end
search(s::AbstractString, r::Regex, idx::Integer) =
throw(ArgumentError("regex search is only available for bytestrings; use bytestring(s) to convert"))
search(s::AbstractString, r::Regex) = search(s,r,start(s))
_search(s::AbstractString, r::Regex, idx::Integer) = match(r, s, idx)

first(m::RegexMatch) = m.offset
last(m::RegexMatch) = m.offset + length(m.match) - 1

function _replace(io, repl::AbstractString, str, m::RegexMatch)
write(io, "repl")
write(io, repl)
end

immutable RegexMatchIterator
regex::Regex
Expand Down
12 changes: 8 additions & 4 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1327,8 +1327,11 @@ function _rsplit{T<:AbstractString,U<:Array}(str::T, splitter, limit::Integer, k
end
#rsplit(str::AbstractString) = rsplit(str, _default_delims, 0, false)

_replacement(repl, str, j, k) = repl
_replacement(repl::Function, str, j, k) = repl(SubString(str, j, k))
_replace(io, repl, str, r) = write(io, repl)
_replace(io, repl::Function, str, r) =
write(io, repl(SubString(str, first(r), last(r))))
_search(str, pattern, offset) = search(str, pattern, offset)


function replace(str::ByteString, pattern, repl, limit::Integer)
n = 1
Expand All @@ -1340,7 +1343,7 @@ function replace(str::ByteString, pattern, repl, limit::Integer)
while j != 0
if i == a || i <= k
write_sub(out, str.data, i, j-i)
write(out, _replacement(repl, str, j, k))
_replace(out, repl, str, r)
end
if k<j
i = j
Expand All @@ -1351,7 +1354,8 @@ function replace(str::ByteString, pattern, repl, limit::Integer)
if j > e
break
end
r = search(str,pattern,k)
r = _search(str,pattern,k)
r == nothing && break
j, k = first(r), last(r)
n == limit && break
n += 1
Expand Down

0 comments on commit a4a75dc

Please sign in to comment.