replaceFirstInto

A variation on replaceFirst 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.

Like in replaceFirst family of functions there is an overload for the substitution guided by the format string and the one with the user defined callback.

  1. void replaceFirstInto(Sink sink, R input, RegEx re, const(C)[] format)
    @trusted
    void
    replaceFirstInto
    (
    Sink
    R
    C
    RegEx
    )
    (
    ref Sink sink
    ,,
    RegEx re
    ,
    const(C)[] format
    )
    if (
    isOutputRange!(Sink, dchar) &&
    &&
    is(C : dchar)
    &&
    isRegexFor!(RegEx, R)
    )
  2. void replaceFirstInto(Sink sink, R input, RegEx re)

Examples

import std.array;
string m1 = "first message\n";
string m2 = "second message\n";
auto result = appender!string();
replaceFirstInto(result, m1, regex(`([a-z]+) message`), "$1");
//equivalent of the above with user-defined callback
replaceFirstInto!(cap=>cap[1])(result, m2, regex(`([a-z]+) message`));
assert(result.data == "first\nsecond\n");

Meta