Skip to content
Merged
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
22 changes: 11 additions & 11 deletions std/regex/internal/backtracking.d
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,10 @@ final:
break;
case IR.Backref:
immutable n = re.ir[pc].data;
auto referenced = re.ir[pc].localRef
? s[matches[n].begin .. matches[n].end]
: s[backrefed[n].begin .. backrefed[n].end];
auto g = re.ir[pc].localRef ? matches[n] : backrefed[n];
if (!g)
goto L_backtrack;
auto referenced = s[g.begin .. g.end];
while (!atEnd && !referenced.empty && front == referenced.front)
{
next();
Expand Down Expand Up @@ -1430,14 +1431,13 @@ struct CtContext
$$`, ir[0].data, nextInstr);
break;
case IR.Backref:
string mStr = "auto referenced = ";
mStr ~= ir[0].localRef
? ctSub("s[matches[$$].begin .. matches[$$].end];",
ir[0].data, ir[0].data)
: ctSub("s[backrefed[$$].begin .. backrefed[$$].end];",
ir[0].data, ir[0].data);
string gStr = ir[0].localRef
? ctSub("matches[$$]", ir[0].data)
: ctSub("backrefed[$$]", ir[0].data);
code ~= ctSub( `
$$
if (!$$)
$$
auto referenced = s[$$.begin .. $$.end];
while (!atEnd && !referenced.empty && front == referenced.front)
{
next();
Expand All @@ -1446,7 +1446,7 @@ struct CtContext
if (referenced.empty)
$$
else
$$`, mStr, nextInstr, bailOut);
$$`, gStr, bailOut, gStr, gStr, nextInstr, bailOut);
break;
case IR.Nop:
case IR.End:
Expand Down
4 changes: 4 additions & 0 deletions std/regex/internal/tests.d
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ debug(std_regex_test) import std.stdio;
TestVectors( "a\\b", "a", "y", "$&", "a" ),
TestVectors( "(a)b\\1", "abaab","y", "$&", "aba" ),
TestVectors( "()b\\1", "aaab", "y", "$&", "b" ),
TestVectors( "(a)?\\1", "aa", "y", "$&", "aa" ),
TestVectors( "(a)?\\1", "xaa", "y", "$&", "aa" ),
TestVectors( "(a)?b\\1", "aba", "y", "$&", "aba" ),
TestVectors( "(a)?b\\1", "b", "n", "-", "-" ),
TestVectors( "abc", "abc", "y", "$&", "abc" ),
TestVectors( "abc", "xbc", "n", "-", "-" ),
TestVectors( "abc", "axc", "n", "-", "-" ),
Expand Down
14 changes: 11 additions & 3 deletions std/regex/internal/thompson.d
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,13 @@ template ThompsonOps(E, S, bool withInput:true)
uint n = re.ir[t.pc].data;
Group!DataIndex* source = re.ir[t.pc].localRef ? t.matches.ptr : backrefed.ptr;
assert(source);
if (source[n].begin == source[n].end)//zero-width Backref!
if (!source[n]) // unmatched group
{
recycle(t);
t = worklist.fetch();
return t != null;
}
if (source[n].begin == source[n].end) // zero-width match
{
t.pc += IRL!(IR.Backref);
return true;
Expand Down Expand Up @@ -681,15 +687,17 @@ template ThompsonOps(E,S, bool withInput:false)
return state.popState(e);
}

// special case of zero-width backref
// special case of zero-width or unmatched backref
static bool op(IR code:IR.Backref)(E e, S* state)
{
with(e) with(state)
{
uint n = re.ir[t.pc].data;
Group!DataIndex* source = re.ir[t.pc].localRef ? t.matches.ptr : backrefed.ptr;
assert(source);
if (source[n].begin == source[n].end)//zero-width Backref!
if (!source[n]) // unmatched group
return popState(e);
if (source[n].begin == source[n].end) // zero-width match
{
t.pc += IRL!(IR.Backref);
return true;
Expand Down
Loading