~earboxer/bare-mess-php

7170f55c06f387cc4a03bf918c26aec46c7b44b0 — Zach DeCook 4 years ago 4aac74b
* Strings: Support strings longer than 127 characters
3 files changed, 17 insertions(+), 6 deletions(-)

M src/Bare.php
M src/Mutatable.php
M tests/PrimitivesTest.php
M src/Bare.php => src/Bare.php +2 -3
@@ 12,8 12,7 @@ class Bare
                $mess = substr($mess, 1);
                return $val;
            case 'string': // string
                // TODO: Use uint for string's length.
                $length = (new U8($mess))->get();
                $length = (new UInt($mess))->get();
                $val = substr($mess, 0, $length);
                $mess = substr($mess, $length);
                return $val;


@@ 31,7 30,7 @@ class Bare
                return chr($value);
            case 'string': // string
                // TODO: Use uint for string's length.
                return self::mess(strlen($value)) . $value;
                return UInt::fromValue(strlen($value))->mess() . $value;
            case 'integer': // int
                // TODO: encode as variable length integer.
                return chr($value);

M src/Mutatable.php => src/Mutatable.php +9 -2
@@ 4,12 4,19 @@ namespace BareMess;

trait Mutatable
{
    public static function fromValue($value)
    {
        $o = new self();
        $o->set($value);
        return $o;
    }

    public function get()
    {
        return $this->value;
        return is_object($this->value) ? $this->value->get() : $this->value;
    }
    public function set($value)
    {
        return $this->value = $value;
        return is_object($this->value) ? $this->value->set($value) : $this->value = $value;
    }
}

M tests/PrimitivesTest.php => tests/PrimitivesTest.php +6 -1
@@ 96,7 96,12 @@ class PrimitivesTest extends TestCase
        $mess = Bare::mess($string1, '');
        $mess .= Bare::mess($string2, '');
        $this->assertEquals(chr(5) . "hello" . chr(7) . " world!", $mess);
        // TODO: Test strings longer than 127 characters

        $mess = "\x8D\x01" . str_pad('', 141, 'L');
        $string3 = Bare::construct($mess, '');
        $this->assertEquals('', $mess, "Whole mess was not parsed.");
        $this->assertEquals(str_pad('', 141, 'L'), $string3);
        $this->assertEquals("\x8D\x01" . str_pad('', 141, 'L'), Bare::mess($string3));
    }

    public function testUInt()