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)
    T
    peek
    (
    T
    Endian endianness = Endian.bigEndian
    R
    )
    (,
    size_t index
    )
    if (
    canSwapEndianness!T &&
    &&
    &&
    is(ElementType!R : const ubyte)
    )
  3. T peek(R range, size_t* index)

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

ubyte[] buffer = [1, 5, 22, 9, 44, 255, 8];
assert(buffer.peek!uint() == 17110537);
assert(buffer.peek!ushort() == 261);
assert(buffer.peek!ubyte() == 1);

assert(buffer.peek!uint(2) == 369700095);
assert(buffer.peek!ushort(2) == 5641);
assert(buffer.peek!ubyte(2) == 22);

size_t index = 0;
assert(buffer.peek!ushort(&index) == 261);
assert(index == 2);

assert(buffer.peek!uint(&index) == 369700095);
assert(index == 6);

assert(buffer.peek!ubyte(&index) == 8);
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