M src/Bare.php => src/Bare.php +2 -6
@@ 12,10 12,7 @@ class Bare
$mess = substr($mess, 1);
return $val;
case 'string': // string
- $length = (new UInt($mess))->get();
- $val = substr($mess, 0, $length);
- $mess = substr($mess, $length);
- return $val;
+ return (new Str($mess))->get();
case 'integer': // int
return (new Integer($mess))->get();
};
@@ 31,8 28,7 @@ class Bare
case 'boolean': // bool
return chr($value);
case 'string': // string
- // TODO: Use uint for string's length.
- return UInt::fromValue(strlen($value))->mess() . $value;
+ return Str::fromValue($value)->mess();
case 'integer': // int
return Integer::fromValue($value)->mess();
case 'double': // f64
A src/Str.php => src/Str.php +25 -0
@@ 0,0 1,25 @@
+<?php
+
+namespace BareMess;
+
+// We can't use "String" as the name of this class as it's reserved by PHP.
+// Note: Bare::mess and Bare::construct will encode a PHP string as a \BareMess\Str.
+class Str
+{
+ use Mutatable;
+ private string $value = '';
+ public function __construct(& $mess = null)
+ {
+ if ($mess === null){
+ return;
+ }
+ $length = (new UInt($mess))->get();
+ $this->value = substr($mess, 0, $length);
+ $mess = substr($mess, $length);
+ }
+
+ public function mess()
+ {
+ return UInt::fromValue(strlen($this->value))->mess() . $this->value;
+ }
+}