mirror of
https://github.com/danog/PHPStruct.git
synced 2024-12-02 09:17:53 +01:00
Applied fixes from StyleCI
This commit is contained in:
parent
59ce911b73
commit
68fbb64ab9
@ -12,14 +12,14 @@ var_dump(\danog\PHP\Struct::calcsize('>l'));
|
|||||||
|
|
||||||
// Dynamic usage with format definition on istantiation
|
// Dynamic usage with format definition on istantiation
|
||||||
$struct = new \danog\PHP\StructClass('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP');
|
$struct = new \danog\PHP\StructClass('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP');
|
||||||
var_dump($struct->unpack($struct->pack('n', 'v', -127, 255, true, -32767, 65535, -2147483647, 4294967295, 999999, 9999999, -9223372036854775807, 18446744073709550590, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
var_dump($struct->unpack($struct->pack('n', 'v', -127, 255, true, -32767, 65535, -2147483647, 4294967295, 999999, 9999999, -9223372036854775807, 18446744073709550590, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
||||||
|
|
||||||
// Dynamic usage with format definition on function call
|
// Dynamic usage with format definition on function call
|
||||||
$struct = new \danog\PHP\StructClass();
|
$struct = new \danog\PHP\StructClass();
|
||||||
var_dump($struct->unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', $struct->pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', 'n', 'v', -127, 255, true, -32767, 65535, -2147483647, 4294967295, 999999, 9999999, -9223372036854775807, 18446744073709550590, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
var_dump($struct->unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', $struct->pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', 'n', 'v', -127, 255, true, -32767, 65535, -2147483647, 4294967295, 999999, 9999999, -9223372036854775807, 18446744073709550590, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
||||||
|
|
||||||
// Static usage
|
// Static usage
|
||||||
var_dump(\danog\PHP\Struct::unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', \danog\PHP\Struct::pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', 'n', 'v', -127, 255, true, -32767, 65535, -2147483647, 4294967295, 999999, 9999999, -922337203685, 18446744073709550590, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
var_dump(\danog\PHP\Struct::unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', \danog\PHP\Struct::pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', 'n', 'v', -127, 255, true, -32767, 65535, -2147483647, 4294967295, 999999, 9999999, -922337203685, 18446744073709550590, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
||||||
|
|
||||||
// S'more examples
|
// S'more examples
|
||||||
var_dump(\danog\PHP\Struct::calcsize('f'));
|
var_dump(\danog\PHP\Struct::calcsize('f'));
|
||||||
|
@ -304,7 +304,7 @@ class StructTools
|
|||||||
case 'S':
|
case 'S':
|
||||||
case 'c':
|
case 'c':
|
||||||
case 'C':
|
case 'C':
|
||||||
$curresult = $this->num_pack($data[$command['datakey']], $command['modifiers']['SIZE'], ctype_upper($command['phpformat']));
|
$curresult = $this->num_pack($data[$command['datakey']], $command['modifiers']['SIZE'], ctype_upper($command['phpformat']));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -419,8 +419,8 @@ class StructTools
|
|||||||
if (!is_int($result[$arraycount]) && !is_float($result[$arraycount])) {
|
if (!is_int($result[$arraycount]) && !is_float($result[$arraycount])) {
|
||||||
$result[$arraycount] = (int) $result[$arraycount];
|
$result[$arraycount] = (int) $result[$arraycount];
|
||||||
}
|
}
|
||||||
if(is_float($result[$arraycount]) && $result[$arraycount] < PHP_INT_MAX) {
|
if (is_float($result[$arraycount]) && $result[$arraycount] < PHP_INT_MAX) {
|
||||||
$result[$arraycount] = (int)$result[$arraycount];
|
$result[$arraycount] = (int) $result[$arraycount];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'float':
|
case 'float':
|
||||||
@ -688,13 +688,13 @@ class StructTools
|
|||||||
do {
|
do {
|
||||||
$concat = ($number % 2).$concat;
|
$concat = ($number % 2).$concat;
|
||||||
$number = intval($number / 2);
|
$number = intval($number / 2);
|
||||||
} while($number > 0);
|
} while ($number > 0);
|
||||||
$concat = str_pad($concat, $length, '0', STR_PAD_LEFT);
|
$concat = str_pad($concat, $length, '0', STR_PAD_LEFT);
|
||||||
if ($negative) {
|
if ($negative) {
|
||||||
$concat = $this->binadd($this->stringnot($concat), '1');
|
$concat = $this->binadd($this->stringnot($concat), '1');
|
||||||
}
|
}
|
||||||
if(strlen($concat) == $length + 1 && $concat == str_pad("1", $length + 1, '0', STR_PAD_RIGHT)){
|
if (strlen($concat) == $length + 1 && $concat == str_pad('1', $length + 1, '0', STR_PAD_RIGHT)) {
|
||||||
$concat = str_pad("", $length, "0");
|
$concat = str_pad('', $length, '0');
|
||||||
}
|
}
|
||||||
if (strlen($concat) > $length) {
|
if (strlen($concat) > $length) {
|
||||||
trigger_error('Converted binary number is too long ('.strlen($concat).' > '.$length.').');
|
trigger_error('Converted binary number is too long ('.strlen($concat).' > '.$length.').');
|
||||||
@ -716,13 +716,13 @@ class StructTools
|
|||||||
public function bindec($binary, $unsigned = true)
|
public function bindec($binary, $unsigned = true)
|
||||||
{
|
{
|
||||||
$decimal = 0;
|
$decimal = 0;
|
||||||
if (!$unsigned && $binary[0] == "1") {
|
if (!$unsigned && $binary[0] == '1') {
|
||||||
$binary = $this->binadd($this->stringnot($binary), '1');
|
$binary = $this->binadd($this->stringnot($binary), '1');
|
||||||
$negative = true;
|
$negative = true;
|
||||||
} else {
|
} else {
|
||||||
$negative = false;
|
$negative = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (str_split(strrev($binary)) as $n => $bit) {
|
foreach (str_split(strrev($binary)) as $n => $bit) {
|
||||||
$decimal += (pow(2, $n) * $bit);
|
$decimal += (pow(2, $n) * $bit);
|
||||||
}
|
}
|
||||||
@ -812,7 +812,7 @@ class StructTools
|
|||||||
$bitnumber = $blocksize * 8;
|
$bitnumber = $blocksize * 8;
|
||||||
if ($unsigned) {
|
if ($unsigned) {
|
||||||
$min = 0;
|
$min = 0;
|
||||||
switch($bitnumber) {
|
switch ($bitnumber) {
|
||||||
case '8':
|
case '8':
|
||||||
$max = 255;
|
$max = 255;
|
||||||
break;
|
break;
|
||||||
@ -824,13 +824,13 @@ class StructTools
|
|||||||
break;
|
break;
|
||||||
case '64':
|
case '64':
|
||||||
$max = 18446744073709551615;
|
$max = 18446744073709551615;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$max = pow(2, $bitnumber) - 1;
|
$max = pow(2, $bitnumber) - 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch($bitnumber) {
|
switch ($bitnumber) {
|
||||||
case '8':
|
case '8':
|
||||||
$min = -127;
|
$min = -127;
|
||||||
$max = 127;
|
$max = 127;
|
||||||
@ -846,7 +846,7 @@ class StructTools
|
|||||||
case '64':
|
case '64':
|
||||||
$min = -9223372036854775807;
|
$min = -9223372036854775807;
|
||||||
$max = 9223372036854775807;
|
$max = 9223372036854775807;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$max = pow(2, $bitnumber - 1) - 1;
|
$max = pow(2, $bitnumber - 1) - 1;
|
||||||
$min = -pow(2, $bitnumber - 1);
|
$min = -pow(2, $bitnumber - 1);
|
||||||
|
Loading…
Reference in New Issue
Block a user