aboutsummaryrefslogtreecommitdiff
path: root/src/Search/CharSource.php
blob: 165e5380893c3aab91595675cdd4cbadc53b6eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php


namespace Micropoly\Search;


class CharSource
{
    private string $s;
    private int $i = 0;
    private int $len;

    public function __construct(string $s)
    {
        $this->s = $s;
        $this->len = mb_strlen($s);
    }

    public function getNext(): ?string
    {
        if ($this->i >= $this->len)
            return null;

        $c = mb_substr($this->s, $this->i, 1);
        $this->i++;
        return $c;
    }

    public function unget(): void
    {
        $this->i = max(0, $this->i - 1);
    }
}