Skip to content

Core\Bytes::compare

Core\Bytes::compare(bytes $a, bytes $b): intCore\Bytes::compare(
    bytes $a,
    bytes $b,
): int

Compares two bytes values and tells you which of them comes first in order.

The result is -1 when the first value comes first, 0 when the two are the same, and 1 when the second value comes first. The bytes are compared one at a time from the start, each as a number from 0 to 255. When one value is the start of the other, the shorter one comes first. Use this to sort binary keys, or to test whether a key falls between two boundaries.

Good to know: to test only whether two values are the same, write $a == $b. That is shorter to read, and it stops at the first byte that differs.

Parameters

  • $abytesThe first buffer.
  • $bbytesThe second buffer.

Return value

Returns int. -1 when $a sorts first, 0 when both are equal, and 1 when $b sorts first. No other value is returned.

Examples

Which value comes first

<?nvs
// Shows the three results of comparing two `bytes` values.
bytes $apple = 'apple' as bytes;
bytes $banana = 'banana' as bytes;
// "apple" comes before "banana", so this prints -1.
echo Core\Bytes::compare($apple, $banana), "\n";
// The same two values the other way round. This prints 1.
echo Core\Bytes::compare($banana, $apple), "\n";
// Two values with the same bytes. This prints 0.
echo Core\Bytes::compare($apple, 'apple' as bytes), "\n";
Output
-1
1
0

Short values and high bytes

<?nvs
// Shows the two rules that decide the order: a shorter value that is the start
// of a longer one comes first, and every byte counts as a number from 0 to 255.
// "car" is the start of "cargo", so "car" comes first. This prints -1.
echo Core\Bytes::compare('car' as bytes, 'cargo' as bytes), "\n";
// The byte 0xff is 255 and the byte 0x7f is 127, so 0xff comes second.
// This prints 1.
bytes $high = Core\Encoding::fromHex('ff');
bytes $low = Core\Encoding::fromHex('7f');
echo Core\Bytes::compare($high, $low), "\n";
// The first byte that differs decides the order. Here that is the second byte,
// 0x00 against 0x01, so this prints -1.
echo Core\Bytes::compare(Core\Encoding::fromHex('0100ff'), Core\Encoding::fromHex('0101')), "\n";
Output
-1
1
-1

Keys inside a range

<?nvs
// Shows how a program picks the records one server is responsible for. Each
// record has a key made of bytes, and a server holds the keys from its start
// up to, but not including, its end.
class Range
{
// Returns true when $key is at or after $start and before $end.
public static function holds(bytes $key, bytes $start, bytes $end): bool
{
return Core\Bytes::compare($key, $start) >= 0
&& Core\Bytes::compare($key, $end) < 0;
}
}
bytes $start = Core\Encoding::fromHex('40');
bytes $end = Core\Encoding::fromHex('80');
array<bytes> $keys = [
Core\Encoding::fromHex('3fffffff'),
Core\Encoding::fromHex('40000000'),
Core\Encoding::fromHex('6a1b2c3d'),
Core\Encoding::fromHex('80000000'),
];
// The first key is too small and the last one is too large, so this prints 2.
uint $held = 0;
foreach ($keys as bytes $key) {
if (Range::holds($key, $start, $end)) {
$held = $held + 1;
}
}
echo $held, "\n";
Output
2