How is std.regex.replaceAllInto more efficient?
Shriramana Sharma via Digitalmars-d
digitalmars-d at puremagic.com
Thu Oct 15 18:50:30 PDT 2015
Hello. The doc on std.regex.replace{First,All}Into reads:
A variation on replaceAll that instead of allocating a new string on
each call outputs the result piece-wise to the sink. In particular this
enables efficient construction of a final output incrementally.
Example:
//swap all 3 letter words and bring it back
string text = "How are you doing?";
auto sink = appender!(char[])();
replaceAllInto!(cap => retro(cap[0]))(sink, text, regex(`\b\w{3}\b`));
auto swapped = sink.data.dup; // make a copy explicitly
assert(swapped == "woH era uoy doing?");
sink.clear();
replaceAllInto!(cap => retro(cap[0]))(sink, swapped,
regex(`\b\w{3}\b`));
assert(sink.data == text);
Now IIUC the code, there are only two calls to replaceAllInto, and after the
first call the string is dupped, in which case there is an allocation
nevertheless, so how does using *into make for efficiency?
Or is it that "each call" means each match of the regex? So there aren't
just two calls, and that without the sink and *Into, each match of the regex
will cause a new string allocation and this is what is being avoided?
If so, why can't the default implementation of replace{First,All} itself use
an internal sink instead of needing the user to manually specify it?
Thanks!
Shriramana Sharma.
More information about the Digitalmars-d
mailing list