~earboxer/bare-mess-php

07fd06e22dc315ef9589ed700ebc91cd10de3034 — Zach DeCook 1 year, 7 months ago 70affac
UInt: Make encoding more efficient

(and compliant with upcoming standards)
2 files changed, 12 insertions(+), 2 deletions(-)

M src/UInt.php
M tests/PrimitivesTest.php
M src/UInt.php => src/UInt.php +2 -2
@@ 26,9 26,9 @@ class UInt
    public function mess()
    {
        $mess = '';
        // The number of bytes should be greater than or equal to the number required,
        // The number of bytes MUST be equal to the number required,
        // value < pow(2, 7 * bytes)
        $bytes = floor(log($this->value + 1, 2) / 7) + 1;
        $bytes = (int)floor(log($this->value, 2) / 7) + 1;

        for ($i = 0; $i < $bytes; $i++) {
            // BARE encodes integers as little-endian (least significant first),

M tests/PrimitivesTest.php => tests/PrimitivesTest.php +10 -0
@@ 179,6 179,16 @@ class PrimitivesTest extends TestCase
        $uint2 = new UInt($mess);
        $this->assertEquals("", $mess);
        $this->assertEquals(pow(2, 7*8), $uint2->get());

        // The encoder MUST encode uint using the minimum necessary number of octets
        $uint->set(127);
        $this->assertEquals("\x7f", $uint->mess(), "UInt 127 encoded inefficiently.");
        $uint->set(128);
        $this->assertEquals("\x80\x01", $uint->mess());
        $uint->set(129);
        $this->assertEquals("\x81\x01", $uint->mess());
        $uint->set(255);
        $this->assertEquals("\xFF\x01", $uint->mess());
    }

    public function testUs()