Merge branch '2.0'

This commit is contained in:
terrafrost 2021-12-26 00:41:00 -06:00
commit d58c82afc2

View File

@ -308,7 +308,7 @@ abstract class Strings
* @param string $x * @param string $x
* @return string * @return string
*/ */
public static function bin2bits($x) public static function bin2bits($x, $trim = true)
{ {
/* /*
// the pure-PHP approach is slower than the GMP approach BUT // the pure-PHP approach is slower than the GMP approach BUT
@ -337,7 +337,7 @@ abstract class Strings
} }
} }
return ltrim($bits, '0'); return $trim ? ltrim($bits, '0') : $bits;
} }
/** /**
@ -350,15 +350,22 @@ abstract class Strings
public static function switchEndianness($x) public static function switchEndianness($x)
{ {
$r = ''; $r = '';
// from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits
for ($i = strlen($x) - 1; $i >= 0; $i--) { for ($i = strlen($x) - 1; $i >= 0; $i--) {
$b = ord($x[$i]); $b = ord($x[$i]);
if (PHP_INT_SIZE === 8) {
// 3 operations
// from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64BitsDiv
$r.= chr((($b * 0x0202020202) & 0x010884422010) % 1023);
} else {
// 7 operations
// from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits
$p1 = ($b * 0x0802) & 0x22110; $p1 = ($b * 0x0802) & 0x22110;
$p2 = ($b * 0x8020) & 0x88440; $p2 = ($b * 0x8020) & 0x88440;
$r.= chr( $r.= chr(
(($p1 | $p2) * 0x10101) >> 16 (($p1 | $p2) * 0x10101) >> 16
); );
} }
}
return $r; return $r;
} }