[RFC] replaceInto

Dmitry Olshansky dmitry.olsh at gmail.com
Sun Mar 4 23:41:52 PST 2012


On 05.03.2012 9:43, Jonathan M Davis wrote:
> On Sunday, March 04, 2012 22:21:46 Timon Gehr wrote:
>> On 03/04/2012 09:48 PM, Dmitry Olshansky wrote:
>>> This is proposal was dictated by a simple sudden need when designing
>>> with ranges in mind. It's to introduce into Phobos the idiomatic
>>> function replaceInto taking an output range to store the result in.
>>>
>>> The signature revolves around:
>>> void replace(..., Sink)(..., Sink sink)
>>> if(...&&  isOutputRange!Sink)
>>>
>>> Motivating example (std.array replace):
>>>
>>> auto writer = stdout.lockingTextWriter; //or pick an Appender
>>> foreach(line; stdin.byLine)
>>> writer.put(replace(line, "Nick", "Nicholas"));
>>>
>>>
>>> Skiping hardcoded strings, and lockingTextWriter (it has problems) at
>>> the very deep level we see - what exactly? A horrible inefficiency, of
>>> course! There is no need to allocate space for replaced string and even
>>> doing it on buffer in-place risks allocation.
>>>
>>> With proposed replaceInto:
>>>
>>> auto writer = stdout.lockingTextWriter; //or pick an Appender
>>> foreach(line; stdin.byLine)
>>> replaceInto(line, "Nick", "Nicholas", writer);
>>>
>>> Look ma, no allocations!
>>>
>>> Same goes for e.g. std.regex replace, coincidence, coincidence :)
>>>
>>> P.S. A DIP 9 flashback?
>>
>> Yes, we need this.
>
> I've been considering that we should probably add overloads which take an
> output range to most of the functions in std.string - and possibly a number in
> std.array - so that we can get that extra boost of efficiency, but I haven't
> gotten around to doing anything with that yet.
>

Yes, by the looks of it there is plenty of places to apply this. One 
thing that I'm unclear on is should it be overloads?
The problem is that e.g. the fact that replace either returns new array 
or nothing depending on the arguments is kind of unexpected for the 
reader. Not to mention that overload space was pretty crumbled, that's 
why I ended up with replaceInto name.


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list