From cee6e38a2cf914f937a492f2d8038bda56a4fd76 Mon Sep 17 00:00:00 2001 From: Zach DeCook Date: Mon, 29 Jun 2020 19:22:11 -0400 Subject: [PATCH] * Primitives: Construct and test String type --- src/Bare.php | 12 ++++++++++++ tests/PrimitivesTest.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Bare.php b/src/Bare.php index 039776f..7224c27 100644 --- a/src/Bare.php +++ b/src/Bare.php @@ -4,6 +4,18 @@ namespace BareMess; class Bare { + public static function construct(& $mess, $startingValue) + { + switch(gettype($startingValue)) { + case 'string': // string + // TODO: Use uint for string's length. + $length = (new U8($mess))->get(); + $val = substr($mess, 0, $length); + $mess = substr($mess, $length); + return $val; + }; + } + public static function mess($value): string { switch(gettype($value)) { diff --git a/tests/PrimitivesTest.php b/tests/PrimitivesTest.php index 149d966..e430efa 100644 --- a/tests/PrimitivesTest.php +++ b/tests/PrimitivesTest.php @@ -2,6 +2,7 @@ namespace BareMess\Tests; +use BareMess\Bare; use BareMess\Example\PublicKey; use BareMess\{I8,I16,I32,I64}; use BareMess\{U8,U16,U32,U64}; @@ -57,6 +58,20 @@ class PrimitivesTest extends TestCase $this->assertEquals("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", $i64->mess()); } + public function testString() + { + $mess = chr(5) . "hello" . chr(7) . " world!"; + $string1 = Bare::construct($mess, ''); + $string2 = Bare::construct($mess, ''); + $this->assertEquals("", $mess); + $this->assertEquals("hello", $string1); + $this->assertEquals(" world!", $string2); + $mess = Bare::mess($string1, ''); + $mess .= Bare::mess($string2, ''); + $this->assertEquals(chr(5) . "hello" . chr(7) . " world!", $mess); + // TODO: Test strings longer than 127 characters + } + public function testUs() { $u8 = new U8(); -- 2.45.2