Overhauling the notion of output range
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Mon Jul 12 19:58:07 PDT 2010
On 07/12/2010 04:39 PM, Steven Schveighoffer wrote:
> On Mon, 12 Jul 2010 17:25:43 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> On 07/12/2010 02:41 PM, Steven Schveighoffer wrote:
>>> I'm unsure how it will work either. I admit now that I didn't think
>>> through how this will be used.
>>
>> It's very simple. As far as a user of an output range is concerned,
>> they should write stuff like:
>>
>> put(r, '[');
>> char[] someBuf;
>> put(r, someBuf);
>> put(r, ", ");
>> put(r, ']');
>>
>> in confidence that things are reasonably efficient.
>
> How does that work for a range whose front() can be assigned a dchar?
> Wait, it doesn't, because it won't compile.
>
> But wouldn't that be the same for a delegate that takes a dchar?
>
> I'm very confused at what you are trying to do. I expected that a char[]
> would be a valid output range.
Actually a char[] is not a valid output range. Overwriting
variable-length codes with other variable-length codes might mess up the
string.
Here's what I have. Works?
void put(R, E)(ref R r, E e)
{
static if (!isArray!R && is(typeof(r.put(e))))
{
r.put(e);
}
else static if (!isArray!R && is(typeof(r.put((&e)[0..1]))))
{
r.put((&e)[0..1]);
}
else static if (is(typeof(r.front = e, r.popFront())))
{
r.front = e;
r.popFront();
}
else static if (isInputRange!E && is(typeof(put(r, e.front))))
{
for (; !e.empty; e.popFront()) put(r, e.front);
}
else static if (is(typeof(r(e))))
{
r(e);
}
else static if (is(typeof(r((&e)[0..1]))))
{
r((&e)[0..1]);
}
else
{
static assert(false, "Cannot put a "~E.stringof~" into a
"~R.stringof);
}
}
Andrei
More information about the Digitalmars-d
mailing list