diff --git a/std/regex/internal/tests.d b/std/regex/internal/tests.d index 88bc0a6ea..98609a37f 100644 --- a/std/regex/internal/tests.d +++ b/std/regex/internal/tests.d @@ -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!"); } + diff --git a/std/regex/package.d b/std/regex/package.d index 22c31854f..01fd14ba2 100644 --- a/std/regex/package.d +++ b/std/regex/package.d @@ -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"