digitalmars.D.learn - How-to manipulate a C array with D2 vector operations?
- Lars Holowko (12/12) Feb 28 2011 Hi
- Denis Koroskin (6/18) Feb 28 2011 Here you go:
- Lars Holowko (3/16) Feb 28 2011 Thanks Denis and Trass3r,
- Trass3r (1/6) Feb 28 2011 auto darray = buffer[0 .. buf_len]
Hi I am trying to implement a D2 function that has this C signature (it gets called from a C module and I cannot change the caller): extern(C) read_into(char *buffer, size_t buf_len); I would like to use D's vector (or array-wise according to TDPL) operations on buffer but I cannot find a way, how I could initialize a (static?) D array and tell it to use buffer as its memory. Obviously I don't want to copy buffer in a regular D array to be able to do the manipulations and than having to copy everything back into buffer. Is there any way to get this accomplished? Thanks, Lars
Feb 28 2011
On Mon, 28 Feb 2011 19:51:28 +0300, Lars Holowko <lars.holowko gmail.com> wrote:Hi I am trying to implement a D2 function that has this C signature (it gets called from a C module and I cannot change the caller): extern(C) read_into(char *buffer, size_t buf_len); I would like to use D's vector (or array-wise according to TDPL) operations on buffer but I cannot find a way, how I could initialize a (static?) D array and tell it to use buffer as its memory. Obviously I don't want to copy buffer in a regular D array to be able to do the manipulations and than having to copy everything back into buffer. Is there any way to get this accomplished? Thanks, LarsHere you go: auto arr = buffer[0..buf_len]; Now you can operate on this array however you like. E.g. arr[] = 0; // initialize with zeros
Feb 28 2011
On 2/28/2011 10:15 AM, Denis Koroskin wrote:On Mon, 28 Feb 2011 19:51:28 +0300, Lars Holowko <lars.holowko gmail.com> wrote:Thanks Denis and Trass3r, that was embarrasingly easy ;-)gets called from a C module and I cannot change the caller): extern(C) read_into(char *buffer, size_t buf_len); I would like to use D's vector (or array-wise according to TDPL) operations on buffer but I cannot find a way, how I could initialize a (static?) D array and tell it to use buffer as its memory. Obviously IHere you go: auto arr = buffer[0..buf_len]; Now you can operate on this array however you like. E.g. arr[] = 0; // initialize with zeros
Feb 28 2011
I am trying to implement a D2 function that has this C signature (it gets called from a C module and I cannot change the caller): extern(C) read_into(char *buffer, size_t buf_len); I would like to use D's vector (or array-wise according to TDPL) operations on buffer but I cannot find a way, how I could initialize aauto darray = buffer[0 .. buf_len]
Feb 28 2011