Am I doing this right? (File byChunk)

BCS none at anon.com
Sun Sep 12 18:47:45 PDT 2010


Hello Andrej,

> Here's a little snippet of code that interfaces with Scintilla (it
> works btw.):
> 
> File file = File("test.txt", "r");
> foreach (ubyte[] buf; file.byChunk(4096))
> {
> sendEditor(SCI_ADDTEXT, buf.length, (cast(char[])buf).idup);
> }
> The cast looks ugly, but I *have* to send a copy.

What do those have to do with each other?

> Am I doing it right,

It doesn't seem right to me either, because you are allocating memory in 
chunks and passing it to C without keeping pointers around. Also what does 
that function do with them? If it keeps them around, you might be faster 
to load the whole file in one go and make a single call (fewer allocations, 
fewer calls, no-extra memory usage, etc.)

> or can I make this a bit simpler? Otherwise I can use file.byLine, but
> I think this would be much slower (unless D reads the entire contents
> at once into memory, but I think it does not).
> 
> Actually, I shouldn't even complain. I can't believe how easy it is to
> open a file, use buffers and send it all to a C interface. This is a
> 4-liner, the C equivalent spans 20+ lines. Ha!
> 
-- 
... <IXOYE><





More information about the Digitalmars-d-learn mailing list