Want to read a whole file as utf-8

Tobias Pankrath via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 3 16:00:53 PST 2015


On Tuesday, 3 February 2015 at 23:07:03 UTC, Foo wrote:
> 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.
>
> How would I use decoding for that? Isn't there a way to read 
> the file as utf8 or event better, as unicode?

Arrays of char, wchar and dchar are supposed to be UTF strings 
and of course you can just read them using a c function from a 
file. You'd just need to make sure they are valid UTF before 
passing them on to other parts of phobos.

What do you mean with "as unicode"?


More information about the Digitalmars-d-learn mailing list