On Wednesday, 7 July 2021 at 01:44:20 UTC, Steven Schveighoffer wrote:
So I have this situation where I need to split a string, then where the splits are, insert a string to go between the elements making a new range, all without allocating (hopefully).


Without considering the more general case, isn't that just splitter-joiner?

```d
import std;

auto foo(string s, string sp, string j) @nogc {
        return s.splitter(sp).joiner(j);
}

void main() {
    foo("ab,cd,ef,gh", ",", "##").writeln; // => ab##cd##ef##gh
}
```

Looking around phobos I found inside the documentation of [roundRobin](https://dlang.org/phobos/std_range.html#.roundRobin) something that does *exactly* what I'm looking for.

Except... the provided `interleave` function iterates the original range twice, which means 2x the searching calls for splitter. Why does it do this? Because `roundRobin` will keep going as long as ANY range still has data left, so you need to make the "interleaving" range stop when the first one stops.


Interesting! Tried it myself, shame that this doesn't quite work:

```d
import std;

auto foo(R)(string s, string sp, R r) @nogc {
    return s.splitter(sp).zip(r)
            .map!(a => a.expand.only)
            .joiner.dropBackOne.joiner; // not bidirectional
}

void main() {
    foo("ab,cd,ef,gh", ",", ["##", "**"].cycle).writeln;
}
```

Reply via email to