From 34ce6c26da4fecc996c7dbdae4fa32f4e3bde2dd Mon Sep 17 00:00:00 2001 From: inhere Date: Wed, 20 Mar 2024 16:02:18 +0800 Subject: [PATCH] :necktie: up: move some text class to extlib package --- README.md | 1 - src/Concern/SimpleEventAwareTrait.php | 6 +- src/IO/MemoryIO.php | 1 - src/IO/StreamIO.php | 1 - src/Text/TextScanner.php | 169 ++++++++++++++++++++++++++ test/Text/TextScannerTest.php | 44 +++++++ 6 files changed, 216 insertions(+), 6 deletions(-) create mode 100644 src/Text/TextScanner.php create mode 100644 test/Text/TextScannerTest.php diff --git a/README.md b/README.md index ea4f500..580ba92 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ Some useful extends library for php. - ## Install ```bash diff --git a/src/Concern/SimpleEventAwareTrait.php b/src/Concern/SimpleEventAwareTrait.php index 605ea50..79ed1e0 100644 --- a/src/Concern/SimpleEventAwareTrait.php +++ b/src/Concern/SimpleEventAwareTrait.php @@ -24,7 +24,7 @@ trait SimpleEventAwareTrait * * @var array */ - protected static $supportedEvents = []; + protected static array $supportedEvents = []; /** * registered Events @@ -34,7 +34,7 @@ trait SimpleEventAwareTrait * 'event' => bool, // is once event * ] */ - private static $events = []; + private static array $events = []; /** * events and handlers @@ -44,7 +44,7 @@ trait SimpleEventAwareTrait * 'event' => callable, // event handler * ] */ - private static $eventHandlers = []; + private static array $eventHandlers = []; /** * register a event handler diff --git a/src/IO/MemoryIO.php b/src/IO/MemoryIO.php index bee73c1..33de79f 100644 --- a/src/IO/MemoryIO.php +++ b/src/IO/MemoryIO.php @@ -5,7 +5,6 @@ /** * Class MemoryIO - * @package Toolkit\Extlib\IO */ class MemoryIO { diff --git a/src/IO/StreamIO.php b/src/IO/StreamIO.php index e8d2319..fe16143 100644 --- a/src/IO/StreamIO.php +++ b/src/IO/StreamIO.php @@ -5,7 +5,6 @@ /** * Class StreamIO - * @package Toolkit\Extlib\IO */ class StreamIO { diff --git a/src/Text/TextScanner.php b/src/Text/TextScanner.php new file mode 100644 index 0000000..893a567 --- /dev/null +++ b/src/Text/TextScanner.php @@ -0,0 +1,169 @@ +source = $source; + } + + /** + * scan text token + * + * Usage: + * + * ```php + * $s = Scanner::new($source); + * while ($s->scan()) { + * $txt = $s->getText(); + * // do something + * } + * ``` + * + * @return bool + */ + public function scan(): bool + { + if ($this->done) { + return false; + } + + if ($this->start) { + $txt = strtok($this->splitToken); + } else { + $this->start = true; + Assert::notEmpty($this->source, 'The source can not be empty'); + $txt = strtok($this->source, $this->splitToken); + } + + // end + if ($txt === false) { + $this->tokText = ''; + // reset + strtok('', ''); + $this->done = true; + return false; + } + + $this->index++; + $this->tokText = $txt; + return true; + } + + /** + * @return array = [bool, string] + */ + public function nextText(): array + { + $ok = $this->scan(); + return [$ok, $this->tokText]; + } + + /** + * find next token text from given token + * + * @return array = [bool, string] + */ + public function nextToken(string $tok): array + { + $txt = strtok($tok); + if ($txt !== false) { + return [true, $txt]; + } + return [false, '']; + } + + /** + * @return string get current token text + */ + public function getText(): string + { + return $this->tokText; + } + + public function getIndex(): int + { + return $this->index; + } + + public function getSource(): string + { + return $this->source; + } + + public function setSource(string $source): void + { + $this->source = $source; + } + + public function setSplitToken(string $splitToken): void + { + $this->splitToken = $splitToken; + } + + public function current(): string + { + return $this->tokText; + } + + public function next(): void + { + $this->scan(); + } + + public function key(): int + { + return $this->index; + } + + public function valid(): bool + { + return !$this->done; + } + + public function rewind(): void + { + $this->source = ''; + $this->tokText = ''; + + $this->index = 0; + $this->start = $this->done = false; + } + +} \ No newline at end of file diff --git a/test/Text/TextScannerTest.php b/test/Text/TextScannerTest.php new file mode 100644 index 0000000..631f432 --- /dev/null +++ b/test/Text/TextScannerTest.php @@ -0,0 +1,44 @@ +setSplitToken(' '); // split by space + + $ls = []; + while ($s->scan()) { + $ls[] = $s->getText(); + } + $this->assertSame(['hello', 'world', 'abc', '123'], $ls); + } + + public function testScan_line(): void + { + $src = <<scan()) { + $ls[] = $s->getText(); + } + vdump($ls); + $this->assertNotEmpty($ls); + } +}