Skip to content

Commit 8fa86d0

Browse files
authored
refactor: remove redundant "& 0xFF" masks from ord() and BitArray (#203)
* Remove redundant "& 0xFF" masks from ord() calls These masks were carried over from C-style code, where "& 0xFF" is used to prevent sign-extension when promoting signed chars (-128..127) to ints. In that context, masking ensures the value is treated as an unsigned byte (0..255). In PHP, however, ord() already returns an integer in the range 0–255, making the extra "& 0xFF" operation unnecessary. * Remove redundant "& 0xFF" mask in BitArray::generateEcBytes() The mask was carried over from C-style code, where "& 0xFF" is used to prevent sign-extension when promoting signed chars (-128..127) to ints. In PHP, however, BitArray::toBytes() already produces values in the range 0–255, making the extra mask operation unnecessary.
1 parent bd2370d commit 8fa86d0

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/Encoder/Encoder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private static function isOnlyDoubleByteKanji(string $content) : bool
222222
}
223223

224224
for ($i = 0; $i < $length; $i += 2) {
225-
$byte = ord($bytes[$i]) & 0xff;
225+
$byte = ord($bytes[$i]);
226226

227227
if (($byte < 0x81 || $byte > 0x9f) && $byte < 0xe0 || $byte > 0xeb) {
228228
return false;
@@ -457,7 +457,7 @@ private static function generateEcBytes(SplFixedArray $dataBytes, int $numEcByte
457457
$toEncode = new SplFixedArray($numDataBytes + $numEcBytesInBlock);
458458

459459
for ($i = 0; $i < $numDataBytes; $i++) {
460-
$toEncode[$i] = $dataBytes[$i] & 0xff;
460+
$toEncode[$i] = $dataBytes[$i];
461461
}
462462

463463
$ecBytes = new SplFixedArray($numEcBytesInBlock);
@@ -649,8 +649,8 @@ private static function appendKanjiBytes(string $content, BitArray $bits) : void
649649
$length = strlen($bytes);
650650

651651
for ($i = 0; $i < $length; $i += 2) {
652-
$byte1 = ord($bytes[$i]) & 0xff;
653-
$byte2 = ord($bytes[$i + 1]) & 0xff;
652+
$byte1 = ord($bytes[$i]);
653+
$byte2 = ord($bytes[$i + 1]);
654654
$code = ($byte1 << 8) | $byte2;
655655

656656
if ($code >= 0x8140 && $code <= 0x9ffc) {

0 commit comments

Comments
 (0)