[Issue 14615] New: std.regex.replaceFirstInto throws exception when no match is found
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Fri May 22 06:18:59 PDT 2015
https://issues.dlang.org/show_bug.cgi?id=14615
Issue ID: 14615
Summary: std.regex.replaceFirstInto throws exception when no
match is found
Product: D
Version: D2
Hardware: x86_64
OS: Linux
Status: NEW
Severity: enhancement
Priority: P1
Component: Phobos
Assignee: nobody at puremagic.com
Reporter: wendlec at tcd.ie
std.regex.replaceFirst returns the original input, if no match has been found.
However, replaceFirstInto throws an exception, which means you have to know
beforehand whether or not a replacement is possible (example code below).
The same might be true of replaceAllInto, although I haven't tested it.
See also this post
http://forum.dlang.org/thread/lvchjmhzdiokrunadvhi@forum.dlang.org
void main()
{
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");
/++
Throws:
phobos/std/regex/package.d(993): invalid submatch number 1
----------------
./replaceFirstInto(pure @safe bool std.exception.enforce!(Exception,
bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong)+0x65)
[0x495dc5]
./replaceFirstInto(pure @safe void std.regex.replaceFmt!(const(char)[],
std.regex.Captures!(immutable(char)[], ulong).Captures,
std.array.Appender!(immutable(char)[]).Appender).replaceFmt(const(char)[],
std.regex.Captures!(immutable(char)[], ulong).Captures,
std.array.Appender!(immutable(char)[]).Appender, bool)+0x204) [0x4b074c]
./replaceFirstInto(pure @safe void
std.regex.replaceFirstInto!(std.array.Appender!(immutable(char)[]).Appender,
immutable(char)[], char,
std.regex.internal.ir.Regex!(char).Regex).replaceFirstInto(ref
std.array.Appender!(immutable(char)[]).Appender, immutable(char)[],
std.regex.internal.ir.Regex!(char).Regex,
const(char)[]).__lambda5!(std.regex.Captures!(immutable(char)[],
ulong).Captures,
std.array.Appender!(immutable(char)[]).Appender).__lambda5(std.regex.Captures!(immutable(char)[],
ulong).Captures, std.array.Appender!(immutable(char)[]).Appender)+0x39)
[0x4b2051]
./replaceFirstInto(pure @trusted void
std.regex.__T19replaceCapturesIntoS2463std5regex112__T16replaceFirstIntoTS3std5array17__T8AppenderTAyaZ8AppenderTAyaTaTS3std5regex8internal2ir12__T5RegexTaZ5RegexZ16replaceFirstIntoFNeKS3std5array17__T8AppenderTAyaZ8AppenderAyaS3std5regex8internal2ir12__T5RegexTaZ5RegexAxaZ9__lambda5TS3std5array17__T8AppenderTAyaZ8AppenderTAyaTS3std5regex19__T8CapturesTAyaTmZ8CapturesZ.replaceCapturesInto(ref
std.array.Appender!(immutable(char)[]).Appender, immutable(char)[],
std.regex.Captures!(immutable(char)[], ulong).Captures)+0x4f) [0x4b1fef]
./replaceFirstInto(@trusted void
std.regex.replaceFirstInto!(std.array.Appender!(immutable(char)[]).Appender,
immutable(char)[], char,
std.regex.internal.ir.Regex!(char).Regex).replaceFirstInto(ref
std.array.Appender!(immutable(char)[]).Appender, immutable(char)[],
std.regex.internal.ir.Regex!(char).Regex, const(char)[])+0x9c) [0x4b1f94]
./replaceFirstInto(_Dmain+0x14f) [0x48ad17]
./replaceFirstInto(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f)
[0x4b8357]
./replaceFirstInto(void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).tryExec(scope void delegate())+0x2a) [0x4b82aa]
./replaceFirstInto(void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).runAll()+0x30) [0x4b8310]
./replaceFirstInto(void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).tryExec(scope void delegate())+0x2a) [0x4b82aa]
./replaceFirstInto(_d_run_main+0x1dc) [0x4b8224]
./replaceFirstInto(main+0x17) [0x4b2fa7]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f3acab8eec5]
+/
}
--
More information about the Digitalmars-d-bugs
mailing list