Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Make rxx_replace behave correctly with zero-length matches #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 25 additions & 32 deletions libpub/rxx.C
Original file line number Diff line number Diff line change
Expand Up @@ -195,45 +195,38 @@ str
rxx_replace_2 (str input, rxx pat, str repl_str)
{
repl_t repl;
str ret;
if (!repl.parse (repl_str)) {
warn << "XX cannot parse replacement string: " << repl_str << "\n";
} else {
const char *p = input.cstr();
const char *const e = p + input.len ();
strbuf b;
bool go = true;
bool err = false;

// check p < e to see that we're not dealing with an empty
// string (especially since x? matches "").
while (go && !err && p < e) {

if (!pat._exec (p, e - p, 0)) {
warn << "XX regex execution failed\n";
err = true;
}

else if (!pat.success ()) { go = false; }

else {
str pre = str (p, pat.start (0));
strbuf_output (b, pre);
repl.output (b, p, pat);
p += max (pat.end (0), 1);
}

return nullptr;
}
strbuf b;
int offset = 0;

while (pat._exec(input.cstr(), input.len(), 0, offset) && pat.success()) {

if (pat.start(0) == pat.end(0)) {
if (offset < input.len()) {
repl.output (b, input.cstr(), pat);
b.copy(input.cstr() + offset, pat.start(0) - offset + 1);
offset++;
continue;
} else {
repl.output (b, input.cstr(), pat);
break;
}
}

if (p < e && !err) {
str post = str (p, e - p);
strbuf_output (b, post);
}
b.copy(input.cstr() + offset, pat.start(0) - offset);
repl.output (b, input.cstr(), pat);
offset = pat.end(0);

if (!err) { ret = b; }
}

if (offset < input.len()) {
b.copy(input.cstr() + offset, input.len() - offset);
}
return ret;

return b;
}

//-----------------------------------------------------------------------
Expand Down