Core\Bytes::endsWith
Core\Bytes::endsWith(bytes $b, bytes $suffix): boolCore\Bytes::endsWith( bytes $b, bytes $suffix, ): bool
Tells you whether a bytes value ends with some other bytes.
The result is true when the last bytes of the value are exactly the bytes you gave, and false when they are not. The comparison reads the bytes one after the other. There is no option to ignore the difference between upper case and lower case, because a bytes value carries no language and no character set.
This is the method a program writes when it reads from a file or a network connection and needs to know whether what has arrived so far ends on a complete record. Use Core\Bytes::startsWith for the other end of the value, and Core\Bytes::indexOf when the bytes may be anywhere in it.
Good to know: an empty value ends every value, so bytes with nothing in them always return true.
Parameters
$bbytesThe buffer.$suffixbytesThe bytes$bmust end with.
Return value
Returns bool. true when $b ends with $suffix. An empty suffix matches every buffer.
Examples
Checking how a value ends
<?nvs// Shows how to check the last bytes of a value. Here a program asks whether// the bytes it read end with a newline.
bytes $chunk = "id=1042\nid=1043\n" as bytes;bytes $newline = Core\Encoding::fromHex('0a');
// The last byte is a newline, so this prints "yes".echo Core\Bytes::endsWith($chunk, $newline) ? 'yes' : 'no', "\n";
// This value stops in the middle of a line, so this prints "no".echo Core\Bytes::endsWith("id=1042\nid=10" as bytes, $newline) ? 'yes' : 'no', "\n";yesnoAn empty suffix and one too long
<?nvs// Shows the two edges of the test. An empty value ends every value, and bytes// longer than the value can never end it.
bytes $name = 'report.pdf' as bytes;
// This prints "true".echo Core\Bytes::endsWith($name, '' as bytes) ? 'true' : 'false', "\n";
// The value is 10 bytes long, so 11 bytes cannot end it. This prints "false".echo Core\Bytes::endsWith($name, 'xreport.pdf' as bytes) ? 'true' : 'false', "\n";
// The whole value ends itself, so this prints "true".echo Core\Bytes::endsWith($name, $name) ? 'true' : 'false', "\n";truefalsetrueReading whole records from a stream
<?nvs// Shows how a program reading from a network connection keeps the part of a// record that has not arrived yet. Each record ends with a newline.
final class Stream{ // Returns the bytes that have to be kept for the next read. Nothing is // kept when the chunk ends on a complete record. public static function leftover(bytes $chunk): bytes { if (Core\Bytes::endsWith($chunk, Core\Encoding::fromHex('0a'))) { return Core\Encoding::fromHex(''); } // The record is not finished, so the whole chunk waits for the bytes // that come next. return $chunk; }}
// This chunk ends on a complete record, so nothing is kept. This prints 0.echo Core\Bytes::length(Stream::leftover("id=1042\nid=1043\n" as bytes)), "\n";
// This one stops in the middle of a line, so all 13 bytes are kept.echo Core\Bytes::length(Stream::leftover("id=1042\nid=10" as bytes)), "\n";013