D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 14615 - std.regex.replaceFirstInto throws exception when no match is found
Summary: std.regex.replaceFirstInto throws exception when no match is found
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 major
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2015-05-22 13:18 UTC by Chris
Modified: 2016-04-07 17:02 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Chris 2015-05-22 13:18:59 UTC
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]
  +/
}
Comment 1 Chris 2015-05-22 13:23:07 UTC
dmd 2.067.1
Comment 3 github-bugzilla 2016-04-07 15:40:54 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/add07e7ea6815bdfa6f89b12e78f76d0ecaff6fd
Fix issue 14615 - std.regex.replaceFirstInto throws exception when no match is found

https://github.com/D-Programming-Language/phobos/commit/efd74a5fea9dd7c8c14bf53d192ca2236f9013ef
Merge pull request #4160 from DmitryOlshansky/issue-14615

Fix issue 14615 - std.regex.replaceFirstInto throws exception when no match is found