Skip to content

Core\Bytes::join

Joins several bytes values into one.

You give the list of buffers, and a separator when you want one. The result is a new value that contains every buffer, in the order of the list. The separator is written between neighbours only, so there is none before the first buffer and none after the last one. Without a separator the buffers follow each other directly.

A list with one buffer returns that buffer's content, and an empty list returns a value with nothing in it. A buffer with nothing in it still counts as one part, so the separators around it are written. Nothing is converted here: every element is already a bytes value.

The list may be an array<tainted bytes>. The result is then tainted bytes.

Good to know: this is how a program puts the chunks of a body back together, or builds one message out of the pieces it collected.

Parameters

  • $partsarray<bytes>The buffers to join, in order.
  • $separatorbytesdefault bytes()The bytes written between two parts. The default is an empty buffer.

Return value

Returns bytes. The new buffer. It is empty when $parts is empty.

Errors

  • RuntimeErrorThe result is larger than the biggest buffer this process can have.

Changelog

VersionChange
0.0.6Core\Bytes::at takes tainted bytes and returns a plain uint. Core\Bytes::join takes an array<tainted bytes>, and the result is then tainted bytes.

Examples

Joining several buffers

<?nvs
// Shows how to join several `bytes` values into one. You give the list, and a
// separator when you want one between the parts.
array<bytes> $parts = ['GET /orders' as bytes, 'HTTP/1.1' as bytes];
// Without a separator the parts follow each other directly. This prints
// "GET /ordersHTTP/1.1".
echo Core\Bytes::join($parts) as string, "\n";
// With a separator it is written between the parts. This prints
// "GET /orders HTTP/1.1".
echo Core\Bytes::join($parts, ' ' as bytes) as string, "\n";
// The length of the result is the length of the parts plus the separator.
// This prints 20.
echo Core\Bytes::length(Core\Bytes::join($parts, ' ' as bytes)), "\n";
Output
GET /ordersHTTP/1.1
GET /orders HTTP/1.1
20

Where the separator goes

<?nvs
// Shows where the separator is written, and what the edges of the list give.
// The separator goes between neighbours. There is none before the first part
// and none after the last. This prints "a-b-c".
echo Core\Bytes::join(['a' as bytes, 'b' as bytes, 'c' as bytes], '-' as bytes) as string, "\n";
// A list of one part gives that part back, with no separator at all. This
// prints "a".
echo Core\Bytes::join(['a' as bytes], '-' as bytes) as string, "\n";
// A part with nothing in it still counts as a part, so the separators around
// it are written. This prints "a--c".
echo Core\Bytes::join(['a' as bytes, '' as bytes, 'c' as bytes], '-' as bytes) as string, "\n";
// An empty list gives a value with nothing in it. This prints 0.
array<bytes> $none = [];
echo Core\Bytes::length(Core\Bytes::join($none, '-' as bytes)), "\n";
Output
a-b-c
a
a--c
0

Putting a body back together

<?nvs
// Shows how a program puts a body back together. A reader gives the body in
// chunks, and joining them without a separator makes one value again.
array<bytes> $chunks = [
"{\"id\":1042," as bytes,
"\"status\":" as bytes,
"\"paid\"}" as bytes,
];
bytes $body = Core\Bytes::join($chunks);
// The three chunks are one value now. This prints the whole body.
echo $body as string, "\n";
// Its length is the lengths of the chunks added up. This prints 27.
echo Core\Bytes::length($body), "\n";
Output
{"id":1042,"status":"paid"}
27