peek

Takes a range of ubytes and converts the first T.sizeof bytes to T. The value returned is converted from the given endianness to the native endianness. The range is not consumed.

  1. T peek(R range)
  2. T peek(R range, size_t index)
  3. T peek(R range, size_t* index)
    T
    peek
    (
    T
    Endian endianness = Endian.bigEndian
    R
    )
    (,
    size_t* index
    )
    if (
    canSwapEndianness!T &&
    &&
    &&
    is(ElementType!R : const ubyte)
    )

Parameters

T

The integral type to convert the first T.sizeof bytes to.

endianness

The endianness that the bytes are assumed to be in.

range R

The range to read from.

index size_t*

The index to start reading from (instead of starting at the front). If index is a pointer, then it is updated to the index after the bytes read. The overloads with index are only available if hasSlicing!R is true.

Examples

1 ubyte[] buffer = [1, 5, 22, 9, 44, 255, 8];
2 assert(buffer.peek!uint() == 17110537);
3 assert(buffer.peek!ushort() == 261);
4 assert(buffer.peek!ubyte() == 1);
5 
6 assert(buffer.peek!uint(2) == 369700095);
7 assert(buffer.peek!ushort(2) == 5641);
8 assert(buffer.peek!ubyte(2) == 22);
9 
10 size_t index = 0;
11 assert(buffer.peek!ushort(&index) == 261);
12 assert(index == 2);
13 
14 assert(buffer.peek!uint(&index) == 369700095);
15 assert(index == 6);
16 
17 assert(buffer.peek!ubyte(&index) == 8);
18 assert(index == 7);
import std.algorithm.iteration : filter;
ubyte[] buffer = [1, 5, 22, 9, 44, 255, 7];
auto range = filter!"true"(buffer);
assert(range.peek!uint() == 17110537);
assert(range.peek!ushort() == 261);
assert(range.peek!ubyte() == 1);

Meta

Suggestion Box / Bug Report