Merge pull request #4160 from DmitryOlshansky/issue-14615

Fix issue 14615 - std.regex.replaceFirstInto throws exception when no match is found
This commit is contained in:
Ilya Yaroshenko 2016-04-07 17:40:49 +02:00
commit efd74a5fea
2 changed files with 31 additions and 6 deletions

View file

@ -745,6 +745,16 @@ unittest
assert(!match("a"d, "aa"d));
}
// bugzilla 7551
unittest
{
auto r = regex("[]abc]*");
assert("]ab".matchFirst(r).hit == "]ab");
assertThrown(regex("[]"));
auto r2 = regex("[]abc--ab]*");
assert("]ac".matchFirst(r2).hit == "]");
}
unittest
{//bugzilla 7674
assert("1234".replace(regex("^"), "$$") == "$1234");
@ -968,12 +978,22 @@ unittest
assert(matchAll(v, ctPat2).front.hit == v);
}
// bugzilla 7551
// bugzilla 14615
unittest
{
auto r = regex("[]abc]*");
assert("]ab".matchFirst(r).hit == "]ab");
assertThrown(regex("[]"));
auto r2 = regex("[]abc--ab]*");
assert("]ac".matchFirst(r2).hit == "]");
import std.stdio : writeln;
import std.regex : replaceFirst, replaceFirstInto, regex;
import std.array : appender;
auto example = "Hello, world!";
auto pattern = regex("^Hello, (bug)"); // won't find this one
auto result = replaceFirst(example, pattern, "$1 Sponge Bob");
assert(result == "Hello, world!"); // Ok.
auto sink = appender!string;
replaceFirstInto(sink, example, pattern, "$1 Sponge Bob");
assert(sink.data == "Hello, world!");
replaceAllInto(sink, example, pattern, "$1 Sponge Bob");
assert(sink.data == "Hello, world!Hello, world!");
}

View file

@ -702,6 +702,11 @@ private @trusted void replaceCapturesInto(alias output, Sink, R, T)
(ref Sink sink, R input, T captures)
if(isOutputRange!(Sink, dchar) && isSomeString!R)
{
if(captures.empty)
{
sink.put(input);
return;
}
sink.put(captures.pre);
// a hack to get around bogus errors, should be simply output(captures, sink)
// "is a nested function and cannot be accessed from"