added boolean support

This commit is contained in:
flash 2020-12-28 23:28:27 +00:00
parent 0fa0ac0d1b
commit 947e717920
3 changed files with 27 additions and 12 deletions

View file

@ -16,14 +16,16 @@ class FWIF {
public const EXCLUDE_VERSION = 0x02; // Exclude version byte at the start of the stream
public const TYPE_NULL = 0; // NULL, no data
public const TYPE_INTEGER = 0x01; // LEB128, implicit length
public const TYPE_FLOAT = 0x02; // double precision IEEE 754, fixed length of 8 bytes
public const TYPE_STRING = 0x03; // UTF-8 string, terminated with TRAILER
public const TYPE_BUFFER = 0x04; // Buffer with binary data, prefixed with a LEB128 length
public const TYPE_ARRAY = 0x05; // List of values, terminated with TRAILER
public const TYPE_OBJECT = 0x06; // List of values with ASCII names, terminated with TRAILER
public const TYPE_DATETIME = 0x07; // A gregorian year, month and day as well as an hour, minute, seconds and millisecond component, variable ranging from 4 to 7 bytes
public const TYPE_TIMESPAN = 0x08; // A period of time, containing days, hours, minutes, seconds and milliseconds, variable ranging from 4 to 6 bytes
public const TYPE_TRUE = 0x01; // Represents a TRUE boolean value
public const TYPE_FALSE = 0x02; // Represents a FALSE boolean value
public const TYPE_INTEGER = 0x03; // LEB128, implicit length
public const TYPE_FLOAT = 0x04; // double precision IEEE 754, fixed length of 8 bytes
public const TYPE_STRING = 0x05; // UTF-8 string, terminated with TRAILER
public const TYPE_BUFFER = 0x06; // Buffer with binary data, prefixed with a LEB128 length
public const TYPE_ARRAY = 0x07; // List of values, terminated with TRAILER
public const TYPE_OBJECT = 0x08; // List of values with ASCII names, terminated with TRAILER
public const TYPE_DATETIME = 0x09; // A gregorian year, month and day as well as an hour, minute, seconds and millisecond component, variable ranging from 4 to 7 bytes
public const TYPE_TIMESPAN = 0x10; // A period of time, containing days, hours, minutes, seconds and milliseconds, variable ranging from 4 to 6 bytes
public const TRAILER = 0xFF; // Termination byte
@ -31,6 +33,8 @@ class FWIF {
private const CODECS = [
self::TYPE_NULL => 'Null',
self::TYPE_TRUE => 'True',
self::TYPE_FALSE => 'False',
self::TYPE_INTEGER => 'Integer',
self::TYPE_FLOAT => 'Float',
self::TYPE_STRING => 'String',
@ -68,6 +72,8 @@ class FWIF {
private static function detectType($data, int $flags): int {
if(is_null($data))
return self::TYPE_NULL;
if(is_bool($data))
return $data ? self::TYPE_TRUE : self::TYPE_FALSE;
if(is_int($data))
return self::TYPE_INTEGER;
if(is_float($data))
@ -132,15 +138,21 @@ class FWIF {
private static function encodeNull($data, int $flags): string { return ''; }
private static function decodeNull($data, int $flags) { return null; }
private static function encodeTrue($data, int $flags): string { return ''; }
private static function decodeTrue($data, int $flags): bool { return true; }
private static function encodeFalse($data, int $flags): string { return ''; }
private static function decodeFalse($data, int $flags): bool { return false; }
private static function encodeInteger(int $number, int $flags): string {
$packed = ''; $more = 1; $negative = $number < 0; $size = PHP_INT_SIZE * 8;
$packed = ''; $more = true; $negative = $number < 0; $size = PHP_INT_SIZE * 8;
while($more) {
$byte = $number & 0x7F;
$number >>= 7;
if($negative)
$number |= (~0 << ($size - 7));
if((!$number && !($byte & 0x40)) || ($number === -1 && ($byte & 0x40)))
$more = 0;
$more = false;
else
$byte |= 0x80;
$packed .= chr($byte);

View file

@ -12,6 +12,7 @@ header('Content-Type: ' . FWIF::CONTENT_TYPE);
if($request->match('GET', '/packages')) {
$tags = explode(';', (string)$request->getQueryParam('tags', FILTER_SANITIZE_STRING));
$packages = empty($tags) ? Patchouli::getPackages() : Patchouli::getPackagesWithTags($tags);
header('Content-Type: text/plain; charset=us-ascii');
echo FWIF::encode($packages);
return;
}

View file

@ -27,7 +27,9 @@ class DummyPackage implements IPackage {
'name' => $this->getName(),
'version' => $this->getVersion(),
'deps' => [],
'null' => null,
/*'null' => null,
'true' => true,
'false' => false,
'zero' => 0,
'u8' => 0x42,
'u16' => 0x4344,
@ -73,7 +75,7 @@ class DummyPackage implements IPackage {
'periodZero' => (new \DateTime())->diff(new \DateTime()),
'array' => ['e', 'a', 0x55],
'object' => new \stdClass,
'misaka' => '御坂 美琴',
'misaka' => '御坂 美琴',*/
];
foreach($this->getDependencies() as $dependency)
$data['deps'][] = $dependency->getName();