replaceAllInto

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.

As with replaceAll there are 2 overloads - one with a format string, the other one with a user defined functor.

  1. void replaceAllInto(Sink sink, R input, RegEx re, const(C)[] format)
  2. void replaceAllInto(Sink sink, R input, RegEx re)
    @trusted
    void
    replaceAllInto
    (
    alias fun
    Sink
    R
    RegEx
    )
    (
    Sink sink
    ,,
    RegEx re
    )
    if (
    isOutputRange!(Sink, dchar) &&
    &&
    isRegexFor!(RegEx, R)
    )

Examples

// insert comma as thousands delimiter in fifty randomly produced big numbers
import std.array, std.conv, std.random, std.range;
static re = regex(`(?<=\d)(?=(\d\d\d)+\b)`, "g");
auto sink = appender!(char [])();
enum ulong min = 10UL ^^ 10, max = 10UL ^^ 19;
foreach (i; 0 .. 50)
{
    sink.clear();
    replaceAllInto(sink, text(uniform(min, max)), re, ",");
    foreach (pos; iota(sink.data.length - 4, 0, -4))
        assert(sink.data[pos] == ',');
}

Meta