Thread overview
read/peek and automatically advance index in buffer
Mar 16, 2023
jwatson-CO-edu
Mar 16, 2023
ag0aep6g
Mar 16, 2023
jwatson-CO-edu
March 16, 2023

I read a file into a ubyte buffer as shown:

\\ ...
buffer = cast(ubyte[]) read( fName ); // Read the entire file as bytestring
marker = 0; // Current index to read from, managed manually, :(
\\ ...

The data file contains numbers of varying size, and I want to read them sequentially.
I gleaned the following from forum posts:

// Start read at marker, cast as int
int rtnVal = peek!(int, Endian.bigEndian)(buffer[marker..$]);
marker += 4; // I just peeked 32 bits

The docs imply that I can peek and also advance my index appropriately, but I'm unsure how to specify the return type, endianness, and index pointer all at once. The following does not work:

int rtnVal = buffer.peek(int,Endian.bigEndian)(&marker);
// Error: found `,` when expecting `.` following int

What is the idiom / function call that will automatically advance my marker index variable?

March 16, 2023

On Thursday, 16 March 2023 at 18:39:00 UTC, jwatson-CO-edu wrote:

>
int rtnVal = buffer.peek(int,Endian.bigEndian)(&marker);
// Error: found `,` when expecting `.` following int

You just forgot the exclamation mark there.

March 16, 2023

On Thursday, 16 March 2023 at 18:58:18 UTC, ag0aep6g wrote:

>

On Thursday, 16 March 2023 at 18:39:00 UTC, jwatson-CO-edu wrote:

>
int rtnVal = buffer.peek(int,Endian.bigEndian)(&marker);
// Error: found `,` when expecting `.` following int

You just forgot the exclamation mark there.

"there" was not descriptive in this context, but I was able to infer what you meant by trial and error. The following gives me the behavior I needed:

int rtnVal = buffer.peek!(int, Endian.bigEndian)(&marker);