Skip to content

Commit

Permalink
Fix issue 17673 - wrong whichPattern in multi-regex with alteration
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryOlshansky committed Aug 24, 2017
1 parent b91f053 commit 5fbab17
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
16 changes: 14 additions & 2 deletions std/regex/internal/tests.d
Expand Up @@ -1087,10 +1087,10 @@ alias Sequence(int B, int E) = staticIota!(B, E);
@safe unittest
{
import std.algorithm.searching : canFind;
void willThrow(T)(T arg, string msg)
void willThrow(T, size_t line = __LINE__)(T arg, string msg)
{
auto e = collectException(regex(arg));
assert(e.msg.canFind(msg), e.msg);
assert(e.msg.canFind(msg), to!string(line) ~ ": " ~ e.msg);
}
willThrow([r".", r"[\(\{[\]\}\)]"], "no matching ']' found while parsing character class");
willThrow([r"[\", r"123"], "no matching ']' found while parsing character class");
Expand All @@ -1106,3 +1106,15 @@ alias Sequence(int B, int E) = staticIota!(B, E);
auto e = collectException!RegexException(regex(q"<[^]>"));
assert(e.msg.canFind("no operand for '^'"));
}

// bugzilla 17673
@safe unittest
{
string str = `<">`;
string[] regexps = ["abc", "\"|x"];
auto regexp = regex(regexps);
auto c = matchFirst(str, regexp);
assert(c);
assert(c.whichPattern == 2);
}

8 changes: 7 additions & 1 deletion std/regex/package.d
Expand Up @@ -374,9 +374,15 @@ if (isSomeString!(S))
app.put("|");
app.put("(?:");
app.put(patterns[i]);
// terminator for the pattern
// to detect if the pattern unexpectedly ends
app.put("\\");
app.put(cast(dchar)(privateUseStart+i)); // special end marker
app.put(cast(dchar)(privateUseStart+i));
app.put(")");
// another one to return correct whichPattern
// for all of potential alternatives in the patterns[i]
app.put("\\");
app.put(cast(dchar)(privateUseStart+i));
}
pat = app.data;
}
Expand Down

0 comments on commit 5fbab17

Please sign in to comment.