Skip to content

Commit 9cf4f40

Browse files
committed
Get rid of unnecessary pack/unpack
1 parent 356f720 commit 9cf4f40

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/Util.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ class Util
1616
*/
1717
public static function cidr($ipAddress, $prefixLen)
1818
{
19-
$ipBytes = array_merge(unpack('C*', inet_pton($ipAddress)));
20-
$networkBytes = array_fill(0, \count($ipBytes), 0);
19+
$ipBytes = inet_pton($ipAddress);
20+
$networkBytes = str_repeat("\0", \strlen($ipBytes));
2121

2222
$curPrefix = $prefixLen;
23-
for ($i = 0; $i < \count($ipBytes) && $curPrefix > 0; $i++) {
23+
for ($i = 0; $i < \strlen($ipBytes) && $curPrefix > 0; $i++) {
2424
$b = $ipBytes[$i];
2525
if ($curPrefix < 8) {
2626
$shiftN = 8 - $curPrefix;
27-
$b = (0xFF & ($b >> $shiftN) << $shiftN);
27+
$b = \chr(0xFF & (\ord($b) >> $shiftN) << $shiftN);
2828
}
2929
$networkBytes[$i] = $b;
3030
$curPrefix -= 8;
3131
}
3232

33-
$network = inet_ntop(pack('C*', ...$networkBytes));
33+
$network = inet_ntop($networkBytes);
3434

3535
return "$network/$prefixLen";
3636
}

0 commit comments

Comments
 (0)