From 29e474b7c84d25126c8288fcc1a01b67bdcb535f Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 9 Feb 2025 12:13:56 +0100 Subject: [PATCH 1/2] Implement slicing operator --- src/main/php/util/Bytes.class.php | 13 +++++++++++++ .../php/util/unittest/BytesTest.class.php | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php index e5f7d8b3b..40052ac27 100755 --- a/src/main/php/util/Bytes.class.php +++ b/src/main/php/util/Bytes.class.php @@ -43,6 +43,19 @@ public function getIterator(): Traversable { } } + /** + * Slicing + * + * @param string $slice + * @return self + */ + public function __invoke($slice) { + list($start, $stop)= explode(':', $slice, 2); + $offset= (int)$start; + $end= (int)$stop ?: $this->size; + return new self(substr($this->buffer, $offset, ($end < 0 ? $this->size + $end : $end) - $offset)); + } + /** * = list[] overloading * diff --git a/src/test/php/util/unittest/BytesTest.class.php b/src/test/php/util/unittest/BytesTest.class.php index 27b8c9e68..bd322bac5 100755 --- a/src/test/php/util/unittest/BytesTest.class.php +++ b/src/test/php/util/unittest/BytesTest.class.php @@ -6,6 +6,19 @@ class BytesTest { + /** @return iterable */ + private function slices() { + yield ['0:4', 'This']; // start:stop + yield ['5:7', 'is']; // start:stop - with non-zero start + yield ['5:-5', 'is a']; // start:stop - with negative offset + yield ['-4:-2', 'test']; // start:stop - with negative offsets + yield [':4', 'This']; // :stop + yield [':-5', 'This is a']; // :stop - with negative offset + yield ['5:', 'is a test']; // start: + yield ['-4:', 'test']; // start: - at negative offset + yield [':', 'This is a test']; // : - copy + } + /** @return iterable */ private function comparing() { yield [new Bytes('Test'), 0]; @@ -338,6 +351,12 @@ public function iteration() { Assert::equals($i, sizeof($c)- 1); } + #[Test, Values(from: 'slices')] + public function slice($slice, $expected) { + $b= new Bytes('This is a test'); + Assert::equals(new Bytes($expected), $b($slice)); + } + #[Test, Values(from: 'comparing')] public function compare($value, $expected) { Assert::equals($expected, (new Bytes('Test'))->compareTo($value)); From f66af8e0bcd73beed2c625a8cb992708f2e8a956 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 9 Feb 2025 12:56:54 +0100 Subject: [PATCH 2/2] Accept arrays for slicing --- src/main/php/util/Bytes.class.php | 4 ++-- src/test/php/util/unittest/BytesTest.class.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php index 40052ac27..7c4d984e0 100755 --- a/src/main/php/util/Bytes.class.php +++ b/src/main/php/util/Bytes.class.php @@ -46,11 +46,11 @@ public function getIterator(): Traversable { /** * Slicing * - * @param string $slice + * @param string|array $slice * @return self */ public function __invoke($slice) { - list($start, $stop)= explode(':', $slice, 2); + list($start, $stop)= is_array($slice) ? $slice : explode(':', $slice, 2); $offset= (int)$start; $end= (int)$stop ?: $this->size; return new self(substr($this->buffer, $offset, ($end < 0 ? $this->size + $end : $end) - $offset)); diff --git a/src/test/php/util/unittest/BytesTest.class.php b/src/test/php/util/unittest/BytesTest.class.php index bd322bac5..feb8663df 100755 --- a/src/test/php/util/unittest/BytesTest.class.php +++ b/src/test/php/util/unittest/BytesTest.class.php @@ -12,11 +12,17 @@ private function slices() { yield ['5:7', 'is']; // start:stop - with non-zero start yield ['5:-5', 'is a']; // start:stop - with negative offset yield ['-4:-2', 'test']; // start:stop - with negative offsets + yield [':4', 'This']; // :stop yield [':-5', 'This is a']; // :stop - with negative offset yield ['5:', 'is a test']; // start: yield ['-4:', 'test']; // start: - at negative offset yield [':', 'This is a test']; // : - copy + + yield [[0, 4], 'This']; // start:stop + yield [[5, 7], 'is']; // start:stop - with non-zero start + yield [[5, -5], 'is a']; // start:stop - with negative offset + yield [[-4, -2], 'test']; // start:stop - with negative offsets } /** @return iterable */