www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - read/peek and automatically advance index in buffer

reply jwatson-CO-edu <real.name colorado.edu> writes:
I read a file into a `ubyte` buffer as shown:

```d
\\ ...
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:

```d
// 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](https://dlang.org/library/std/bitmanip/peek.html) 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:

```d
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?
Mar 16 2023
parent reply ag0aep6g <anonymous example.com> writes:
On Thursday, 16 March 2023 at 18:39:00 UTC, jwatson-CO-edu wrote:
 ```d
 int rtnVal = buffer.peek(int,Endian.bigEndian)(&marker);
 // Error: found `,` when expecting `.` following int
 ```
You just forgot the exclamation mark there.
Mar 16 2023
parent jwatson-CO-edu <real.name colorado.edu> writes:
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:
 ```d
 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: ```d int rtnVal = buffer.peek!(int, Endian.bigEndian)(&marker); ```
Mar 16 2023