Am I doing this right? (File byChunk)

bearophile bearophileHUGS at lycos.com
Mon Sep 13 04:09:41 PDT 2010


Andrej Mitrovic:

> foreach (ubyte[] buf; file.byChunk(4096))
> {
> 	sendEditor(SCI_ADDTEXT, buf.length, cast(LPARAM)buf.ptr);
> }
> ...
> SCI_ADDTEXT(int length, const char *s)

Keep in mind that the length of a D array is a size_t, this means a 32 or 64 bit long unsigned word.


> The ADDTEXT function creates a copy of the pointed-to contents to it's
> internal scintilla buffers, so I don't have to keep any pointers
> around after the call.

In byChunk() the content of buffer is reused across calls, so you are not wasting allocations. I don't know if it's possible to use a fixed-size char[4096] array to remove the first memory allocation too. I think byChunk() needs a second optional argument, to give it a preallocated buffer (like a slice of a fixed-size array).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list