From 1577f6e6cccc612c5f03ed6681a4697d276f7edb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= <duesterhus@woltlab.com> Date: Wed, 2 Mar 2022 17:22:59 +0100 Subject: [PATCH] Improve performance of the hex encoder Single bytes can more efficiently be accessed by directly indexing the string. This also implicitly fixes a bug without any visible effect: `encodeUpper` retrieved two characters from the bytestring, but only ever used the first. --- src/Hex.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Hex.php b/src/Hex.php index 4c27328..03f75f7 100644 --- a/src/Hex.php +++ b/src/Hex.php @@ -45,7 +45,7 @@ abstract class Hex implements EncoderInterface $len = Binary::safeStrlen($binString); for ($i = 0; $i < $len; ++$i) { /** @var array<int, int> $chunk */ - $chunk = \unpack('C', Binary::safeSubstr($binString, $i, 1)); + $chunk = \unpack('C', $binString[$i]); $c = $chunk[1] & 0xf; $b = $chunk[1] >> 4; @@ -73,7 +73,7 @@ abstract class Hex implements EncoderInterface for ($i = 0; $i < $len; ++$i) { /** @var array<int, int> $chunk */ - $chunk = \unpack('C', Binary::safeSubstr($binString, $i, 2)); + $chunk = \unpack('C', $binString[$i]); $c = $chunk[1] & 0xf; $b = $chunk[1] >> 4; -- GitLab