Skip to content

Core\Bytes

Works with binary data, the bytes type. It reads, searches, compares and builds buffers, and converts values to and from a fixed binary format, such as a network message header.

Core\Bytes reads and builds bytes — a buffer of octets with no character in it. Every position, length and count is a byte offset: length is O(1), at answers one octet as a uint, and a negative index counts from the end — where Core\Str counts graphemes. A bytes value cannot be written directly in the code, and there is no . over buffers: a buffer is written through Core\Encoding (fromHex, encodeText), joined with join, built with fill and repeat, and laid out as a wire format with pack and unpack, whose code table names every field's width and byte order outright and refuses a value outside the field's range.

<?nvs
bytes $png = Core\Encoding::fromHex('89504e470d0a1a0a');
echo Core\Bytes::length($png), ' ', Core\Bytes::at($png, 0), ' ', Core\Bytes::at($png, -1), "\n";
echo Core\Bytes::startsWith($png, Core\Encoding::fromHex('89504e47')) ? 'png' : 'other', "\n";
echo Core\Encoding::toHex(Core\Bytes::slice($png, 1, 3)), ' ', Core\Bytes::indexOf($png, Core\Encoding::fromHex('0d0a')) ?? 'none', "\n";
bytes $header = Core\Bytes::pack('NnC', 305419896, 4660, 255);
echo Core\Encoding::toHex($header), "\n";
var $fields = Core\Bytes::unpack($header, 'NnC');
echo Core\Json::encode($fields), "\n";
array<bytes> $parts = [$header, Core\Bytes::fill(2, 0)];
bytes $frame = Core\Bytes::join($parts);
echo Core\Bytes::length($frame), ' ', Core\Encoding::toHex($frame), "\n";
echo Core\Bytes::compare(Core\Encoding::fromHex('01'), Core\Encoding::fromHex('02')), "\n";
Output
8 137 10
png
504e47 4
123456781234ff
[305419896,4660,255]
9 123456781234ff0000
-1

## Byte sources

Core\Bytes names two types for methods that read bytes.

  • Core\Bytes::Source is bytes, an open Core\IO\File, an uploaded file (Core\Request\Part) or the request body (Core\Request\BodyStream). A method can read its start, or read it in any order.
  • Core\Bytes::Chunks is bytes that arrive in pieces: an array, a generator or any other loop over bytes values. A method reads it once, from the first piece to the last.

A method that reads from the start to the end takes both: Core\Bytes::Source|Core\Bytes::Chunks. You pass the value you have, and the method decides how much of it to read. Your own functions can use both types too.

Members

13 members.

MemberSignature
lengthlength(bytes $b): uint
atat(bytes $b, int $index): uint
sliceslice(bytes $b, int $offset, ?int $length = null): bytes
indexOfindexOf(bytes $haystack, bytes $needle, { from?: int }): ?uint
comparecompare(bytes $a, bytes $b): int
containscontains(bytes $haystack, bytes $needle): bool
startsWithstartsWith(bytes $b, bytes $prefix): bool
endsWithendsWith(bytes $b, bytes $suffix): bool
fillfill(uint $length, uint $byte): bytes
repeatrepeat(bytes $b, uint $times): bytes
joinjoin(array<bytes> $parts, bytes $separator = bytes()): bytes
packpack(string $format, mixed ...$values): bytes
unpackunpack(bytes $b, string $format): array<mixed>

Types

Source

Bytes a member can read from the start or in any order: bytes, an open file, an uploaded file or the request body.

Chunks

Bytes that arrive in pieces: an array, a generator or any other loop over bytes values, read once from the first piece to the last.

type Chunks = array<tainted bytes>| Iterable<tainted bytes>| Iterator<tainted bytes>;