Skip to content

Commit 2c2cd6e

Browse files
committed
Implement encoding and decoding
1 parent 917758b commit 2c2cd6e

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"license": "GPL-3.0-or-later",
1515
"require": {
16-
"php": "^7||^8",
16+
"php": "^7||^8"
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "^7||^8||^9"

src/Netstring.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
11
<?php
22
namespace YOCLIB\Netstring;
33

4+
use Exception;
5+
46
class Netstring{
57

6-
private $string;
8+
/**
9+
* @var int $maxLength
10+
*/
11+
private static $maxLength = 2000000;
12+
13+
/**
14+
* @return int
15+
*/
16+
public static function getMaxLength(){
17+
return self::$maxLength;
18+
}
19+
20+
/**
21+
* @param $maxLength
22+
* @return void
23+
*/
24+
public static function setMaxLength($maxLength){
25+
self::$maxLength = $maxLength;
26+
}
727

828
/**
929
* @return string
30+
* @throws Exception
1031
*/
11-
public function getString(){
12-
return $this->string;
32+
public static function decode($string){
33+
$length = explode(':',$string,2)[0] ?? null;
34+
if($length==null){
35+
throw new Exception('Netstring does not have a semicolon.');
36+
}
37+
$semicolon = $string[strlen($length)] ?? null;
38+
if($semicolon!=':'){
39+
throw new Exception('Expecting semicolon. Was \''.$semicolon.'\'.');
40+
}
41+
$data = substr($string,strlen($length)+1,intval($length));
42+
$comma = $string[strlen($length)+1+intval($length)] ?? null;
43+
if($comma!=','){
44+
throw new Exception('Expecting comma. Was \''.$comma.'\'.');
45+
}
46+
return $data;
1347
}
1448

1549
/**
16-
* @param string string
50+
* @return string
51+
* @throws Exception
1752
*/
18-
public function setString($string): void{
19-
$this->string = $string;
53+
public static function encode($string){
54+
if(strlen($string)>self::$maxLength){
55+
throw new Exception('Length of '.strlen($string).' exceeding '.self::$maxLength.' during encoding.');
56+
}
57+
return strlen($string).':'.$string.',';
2058
}
2159

2260
}

tests/NetstringTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
class NetstringTest extends TestCase{
99

10-
//TODO Create tests
10+
public function testDecodingNetstring(){
11+
self::assertEquals('',Netstring::decode('0:,'));
12+
self::assertEquals('a',Netstring::decode('1:a,'));
13+
self::assertEquals('ab',Netstring::decode('2:ab,'));
14+
self::assertEquals('abc',Netstring::decode('3:abc,'));
15+
self::assertEquals('abcd',Netstring::decode('4:abcd,'));
16+
}
17+
18+
public function testEncodingNetstring(){
19+
self::assertEquals('0:,',Netstring::encode(''));
20+
self::assertEquals('1:a,',Netstring::encode('a'));
21+
self::assertEquals('2:ab,',Netstring::encode('ab'));
22+
self::assertEquals('3:abc,',Netstring::encode('abc'));
23+
self::assertEquals('4:abcd,',Netstring::encode('abcd'));
24+
}
1125

1226
}

0 commit comments

Comments
 (0)