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.
<?nvsbytes $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";8 137 10png504e47 4123456781234ff[305419896,4660,255]9 123456781234ff0000-1## Byte sources
Core\Bytes names two types for methods that read bytes.
Core\Bytes::Sourceisbytes, an openCore\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::Chunksis bytes that arrive in pieces: an array, a generator or any other loop overbytesvalues. 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.
| Member | Signature |
|---|---|
length | length(bytes $b): uint |
at | at(bytes $b, int $index): uint |
slice | slice(bytes $b, int $offset, ?int $length = null): bytes |
indexOf | indexOf(bytes $haystack, bytes $needle, { from?: int }): ?uint |
compare | compare(bytes $a, bytes $b): int |
contains | contains(bytes $haystack, bytes $needle): bool |
startsWith | startsWith(bytes $b, bytes $prefix): bool |
endsWith | endsWith(bytes $b, bytes $suffix): bool |
fill | fill(uint $length, uint $byte): bytes |
repeat | repeat(bytes $b, uint $times): bytes |
join | join(array<bytes> $parts, bytes $separator = bytes()): bytes |
pack | pack(string $format, mixed ...$values): bytes |
unpack | unpack(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.
type Source = tainted bytes| Core\IO\File| Core\Request\Part| Core\Request\BodyStream;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.
