mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 15:40:36 +03:00
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:
commit
efd74a5fea
2 changed files with 31 additions and 6 deletions
|
@ -745,6 +745,16 @@ unittest
|
||||||
assert(!match("a"d, "aa"d));
|
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
|
unittest
|
||||||
{//bugzilla 7674
|
{//bugzilla 7674
|
||||||
assert("1234".replace(regex("^"), "$$") == "$1234");
|
assert("1234".replace(regex("^"), "$$") == "$1234");
|
||||||
|
@ -968,12 +978,22 @@ unittest
|
||||||
assert(matchAll(v, ctPat2).front.hit == v);
|
assert(matchAll(v, ctPat2).front.hit == v);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bugzilla 7551
|
// bugzilla 14615
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
auto r = regex("[]abc]*");
|
import std.stdio : writeln;
|
||||||
assert("]ab".matchFirst(r).hit == "]ab");
|
import std.regex : replaceFirst, replaceFirstInto, regex;
|
||||||
assertThrown(regex("[]"));
|
import std.array : appender;
|
||||||
auto r2 = regex("[]abc--ab]*");
|
|
||||||
assert("]ac".matchFirst(r2).hit == "]");
|
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!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -702,6 +702,11 @@ private @trusted void replaceCapturesInto(alias output, Sink, R, T)
|
||||||
(ref Sink sink, R input, T captures)
|
(ref Sink sink, R input, T captures)
|
||||||
if(isOutputRange!(Sink, dchar) && isSomeString!R)
|
if(isOutputRange!(Sink, dchar) && isSomeString!R)
|
||||||
{
|
{
|
||||||
|
if(captures.empty)
|
||||||
|
{
|
||||||
|
sink.put(input);
|
||||||
|
return;
|
||||||
|
}
|
||||||
sink.put(captures.pre);
|
sink.put(captures.pre);
|
||||||
// a hack to get around bogus errors, should be simply output(captures, sink)
|
// a hack to get around bogus errors, should be simply output(captures, sink)
|
||||||
// "is a nested function and cannot be accessed from"
|
// "is a nested function and cannot be accessed from"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue