Skip to content

Commit

Permalink
Some bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
malmaud committed Jul 7, 2015
1 parent c67c828 commit 803f031
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ function _write_capture(io, re, group)
PCRE.substring_copy_bynumber(re.match_data, group,
pointer(io.data, io.ptr), len+1)
io.ptr += len
io.size = max(io.size, io.ptr - 1)
end

function _replace(io, repl_s::SubstitutionString, str, r, re)
Expand All @@ -245,7 +246,7 @@ function _replace(io, repl_s::SubstitutionString, str, r, re)
next_i = nextind(repl, i)
next_i > e && replace_err(repl)
if repl[next_i] == SUB_CHAR
write(io, SUB_CHAR, SUB_CHAR)
write(io, SUB_CHAR, repl[next_i])
i = nextind(repl, next_i)
elseif isnumber(repl[next_i])
group = parse(Int, repl[next_i])
Expand All @@ -271,13 +272,14 @@ function _replace(io, repl_s::SubstitutionString, str, r, re)
i = nextind(repl, i)
i > e && replace_err(repl)
end
#groupname = repl[groupstart:prevind(repl, i)]
# TODO: avoid this allocation
groupname = SubString(repl, groupstart, prevind(repl, i))
if isnumber(groupname)
_write_capture(io, re, parse(Int, groupname))
else
_write_capture(io, re,
PCRE.substring_number_from_name(re.regex, groupname))
group = PCRE.substring_number_from_name(re.regex, groupname)
group < 0 && replace_err("Group $groupname not found in regex $re")
_write_capture(io, re, group)
end
i = nextind(repl, i)
else
Expand Down
1 change: 1 addition & 0 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ end

# Backcapture reference in substitution string
@test replace("abcde", r"(..)(?P<byname>d)", s"\g<byname>xy\1") == "adxybce"
@test_throws ErrorException replace("a", r"(?P<x>)", s"\g<y>")

0 comments on commit 803f031

Please sign in to comment.