Skip to content

Commit 02331ad

Browse files
committed
✨ feat: add Ext - new add TextScanner class
1 parent 4c8bc02 commit 02331ad

File tree

5 files changed

+286
-51
lines changed

5 files changed

+286
-51
lines changed

src/Arr.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
/**
1515
* Class Arr
16-
* alias of the ArrayHelper
16+
* - alias of the ArrayHelper
1717
*
1818
* @package Toolkit\Stdlib
1919
*/

src/Ext/TextScanner.php

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Ext;
4+
5+
use Iterator;
6+
use Toolkit\Stdlib\Helper\Assert;
7+
use function strtok;
8+
9+
/**
10+
* @author inhere
11+
*/
12+
class TextScanner implements Iterator
13+
{
14+
/** @var string source content */
15+
private string $source;
16+
17+
/**
18+
* @var string split token
19+
*/
20+
private string $splitToken = "\n";
21+
22+
private int $index = 0;
23+
private bool $start = false;
24+
private bool $done = false;
25+
26+
/**
27+
* @var string current token text
28+
*/
29+
private string $tokText = '';
30+
31+
/**
32+
* @param string $source
33+
*
34+
* @return static
35+
*/
36+
public static function new(string $source = ''): static
37+
{
38+
return new static($source);
39+
}
40+
41+
public function __construct(string $source = '')
42+
{
43+
$this->source = $source;
44+
}
45+
46+
/**
47+
* scan text token
48+
*
49+
* Usage:
50+
*
51+
* ```php
52+
* $s = Scanner::new($source);
53+
* while ($s->scan()) {
54+
* $txt = $s->getText();
55+
* // do something
56+
* }
57+
* ```
58+
*
59+
* @return bool
60+
*/
61+
public function scan(): bool
62+
{
63+
if ($this->done) {
64+
return false;
65+
}
66+
67+
if ($this->start) {
68+
$txt = strtok($this->splitToken);
69+
} else {
70+
$this->start = true;
71+
Assert::notEmpty($this->source, 'The source can not be empty');
72+
$txt = strtok($this->source, $this->splitToken);
73+
}
74+
75+
// end
76+
if ($txt === false) {
77+
$this->tokText = '';
78+
// reset
79+
strtok('', '');
80+
$this->done = true;
81+
return false;
82+
}
83+
84+
$this->index++;
85+
$this->tokText = $txt;
86+
return true;
87+
}
88+
89+
/**
90+
* @return array = [bool, string]
91+
*/
92+
public function nextText(): array
93+
{
94+
$ok = $this->scan();
95+
return [$ok, $this->tokText];
96+
}
97+
98+
/**
99+
* find next token text from given token
100+
*
101+
* @return array = [bool, string]
102+
*/
103+
public function nextToken(string $tok): array
104+
{
105+
$txt = strtok($tok);
106+
if ($txt !== false) {
107+
return [true, $txt];
108+
}
109+
return [false, ''];
110+
}
111+
112+
/**
113+
* @return string get current token text
114+
*/
115+
public function getText(): string
116+
{
117+
return $this->tokText;
118+
}
119+
120+
public function getIndex(): int
121+
{
122+
return $this->index;
123+
}
124+
125+
public function getSource(): string
126+
{
127+
return $this->source;
128+
}
129+
130+
public function setSource(string $source): void
131+
{
132+
$this->source = $source;
133+
}
134+
135+
public function setSplitToken(string $splitToken): void
136+
{
137+
$this->splitToken = $splitToken;
138+
}
139+
140+
public function current(): string
141+
{
142+
return $this->tokText;
143+
}
144+
145+
public function next(): void
146+
{
147+
$this->scan();
148+
}
149+
150+
public function key(): int
151+
{
152+
return $this->index;
153+
}
154+
155+
public function valid(): bool
156+
{
157+
return !$this->done;
158+
}
159+
160+
public function rewind(): void
161+
{
162+
$this->source = '';
163+
$this->tokText = '';
164+
165+
$this->index = 0;
166+
$this->start = $this->done = false;
167+
}
168+
169+
}

src/Util/Stream/DataStream.php

+3-14
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,18 @@ public static function intComparer(bool $desc = false): Closure
111111
// ---------------------- middle operations ----------------------
112112

113113
/**
114-
* @param callable(array): bool $func
114+
* @param callable(array): bool $func Will collect items that return true
115115
* @param mixed $boolExpr
116116
*
117117
* @return $this
118118
*/
119119
public function filterIf(callable $func, mixed $boolExpr): self
120120
{
121-
if ($boolExpr) {
122-
return $this->filter($func);
123-
}
124-
125-
return $this;
121+
return $boolExpr ? $this->filter($func) : $this;
126122
}
127123

128124
/**
129-
* @param callable(mixed):bool $filterFn
125+
* @param callable(mixed):bool $filterFn Will collect items that return true
130126
*
131127
* @return $this
132128
*/
@@ -431,7 +427,6 @@ public function noneMatch(callable $matcher): bool
431427
}
432428

433429
/**
434-
* @template T
435430
* @return Optional<T>
436431
*/
437432
public function findFirst(): Optional
@@ -448,7 +443,6 @@ public function findFirst(): Optional
448443
}
449444

450445
/**
451-
* @template T
452446
* @return Optional<T>
453447
*/
454448
public function findLast(): Optional
@@ -467,7 +461,6 @@ public function findLast(): Optional
467461
}
468462

469463
/**
470-
* @template T
471464
* @return Optional<T>
472465
*/
473466
public function findAny(): Optional
@@ -478,7 +471,6 @@ public function findAny(): Optional
478471
/**
479472
* Find one item by given matcher
480473
*
481-
* @template T
482474
* @param callable(mixed): bool $matcher
483475
*
484476
* @return Optional<T>
@@ -495,7 +487,6 @@ public function findOne(callable $matcher): Optional
495487
}
496488

497489
/**
498-
* @template T
499490
* @return Optional<T>
500491
*/
501492
public function findRandom(): Optional
@@ -506,7 +497,6 @@ public function findRandom(): Optional
506497
}
507498

508499
/**
509-
* @template T
510500
* @param callable(T, T): int $comparer
511501
*
512502
* @return Optional<T>
@@ -517,7 +507,6 @@ public function max(callable $comparer): Optional
517507
}
518508

519509
/**
520-
* @template T
521510
* @param callable(T, T): int $comparer
522511
*
523512
* @return Optional<T>

0 commit comments

Comments
 (0)