[phobos] std.base64 replacement
Sean Kelly
sean at invisibleduck.org
Tue Oct 12 07:47:28 PDT 2010
On Oct 11, 2010, at 10:31 PM, Daniel Murphy wrote:
> While this is range based, I had something different in mind.
> (Not these function names, just the basic idea of the signatures)
>
> struct Base64Encoder(Range) if (isInputRange!Range && is(ElementType!Range == ubyte))
> {
> // range methods
> @property char front() { ... }
> void popFront() { ... }
> @property size_t length() { ... }
> ...
> };
> Base64Encoder!(Range) Base64Encode(Range)(Range r) if (isInputRange!Range && is(ElementType!Range == ubyte))
> {
> return Base64Encoder!Range(r);
> }
>
> This way encoding would convert an input range of ubyte to an input range of char, and decoding would convert Range!char to Range!ubyte.
>
> This way you would be able to use it with std.algorithm, std.range etc.
> When called with an array the range would be able to provide length and be bidirectional.
> This way there would be no allocations inside the range at all.
>
> You could create and fill a buffer using
> auto buffer = array(encode(data));
> or fill an existing buffer using
> copy(encode(data), buffer);
>
> At this point I don't know if everything would be better off using char or dchar ranges.
>
> @Andrei
> Is it an improvement for the range to yield char[] instead of char?
>
> @Sean
> Would copy(decode(buffer), buffer) cover your use case sufficiently?
It would, and I really like the idea of a b64 adaptor instead of just a plain old conversion function.
More information about the phobos
mailing list