Want to read a whole file as utf-8

Tobias Pankrath via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 3 11:50:26 PST 2015


On Tuesday, 3 February 2015 at 19:44:49 UTC, FG wrote:
> On 2015-02-03 at 19:53, Foo wrote:
>> How can I do that without any GC allocation? Nothing in 
>> std.file seems to be marked with @nogc
>>
>> I'm asking since it seems very complicated to do that with 
>> C++, maybe D is a better choice, then we would probably move 
>> our whole project from C++ to D.
>
> Looks like std.stdio isn't marked with @nogc all the way either.
>
> So for now the temporary solution would be to use std.c.stdio.
> Get the file size, malloc a buffer large enough for it[1],
> use std.c.stdio.read to fill it, assign it to a char[] slice
> and std.utf.decode to consume the text...
>
> Oh wait, decode isn't @nogc either. FFS, what now?
>
>
> [1] I assume the file is small, otherwise there would be an 
> extra step
> involved where after nearing the end of the buffer you move the 
> rest
> of the data to the front, read new data after it, and continue 
> decoding.

Use std.utf.validate instead of decode. It will only allocate one 
exception if necessary.


More information about the Digitalmars-d-learn mailing list