Skip to content

Commit

Permalink
feat(TextIterator): add most used setup
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jul 10, 2024
1 parent 8aa25b0 commit 228ceab
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Iterators/TextIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

/**
* Iterate via line
* @phpstan-type LINE string|array<int, string|null>
* @extends \ArrayIterator<int, LINE>
* @template TValue
* @extends \ArrayIterator<int, TValue>
*/
class TextIterator extends \ArrayIterator
{

public const SKIP_EMPTY_LINE = 1048576; // 2^20
public const CSV_MODE = 2097152; // 2^21
public const SKIP_FIRST_LINE = 4194304; // 2^22
public const TRIM_LINE = 8388608; // 2^23
public const SKIP_EMPTY_AND_TRIM_LINE = self::TRIM_LINE | self::SKIP_EMPTY_LINE;

private string $_current = '';

Expand All @@ -31,7 +31,7 @@ class TextIterator extends \ArrayIterator


/**
* @param array<string>|string $text
* @param array<string|null>|string $text
*/
public function __construct(array|string $text)
{
Expand Down Expand Up @@ -111,17 +111,18 @@ public function rewind(): void

public function valid(): bool
{
$flags = $this->getFlags();
do {
if (parent::valid() === false) {
return false;
}
$current = parent::current();
assert(is_string($current));
$this->_current = $current;
if (BitwiseOperations::check($this->getFlags(), self::TRIM_LINE)) {
if (BitwiseOperations::check($flags, self::TRIM_LINE)) {
$this->_current = trim($this->_current);
}
} while (BitwiseOperations::check($this->getFlags(), self::SKIP_EMPTY_LINE) && $this->_current === '' && $this->moveInternalPointer());
} while (BitwiseOperations::check($flags, self::SKIP_EMPTY_LINE) && $this->_current === '' && $this->moveInternalPointer());

return true;
}
Expand Down

0 comments on commit 228ceab

Please sign in to comment.