summaryrefslogtreecommitdiffstats
path: root/vendor/clue/utf8-react/src/Sequencer.php
blob: e9bf433cdfaa4e32165169ec140e789bf947fb5f (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php

namespace Clue\React\Utf8;

use Evenement\EventEmitter;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
use React\Stream\Util;

/**
 * forwards only complete UTF-8 sequences
 */
class Sequencer extends EventEmitter implements ReadableStreamInterface
{
    private $input;
    private $invalid;

    private $buffer = '';
    private $closed = false;

    public function __construct(ReadableStreamInterface $input, $replacementCharacter = '?')
    {
        $this->input = $input;
        $this->invalid = $replacementCharacter;

        if (!$input->isReadable()) {
            return $this->close();
        }

        $this->input->on('data', array($this, 'handleData'));
        $this->input->on('end', array($this, 'handleEnd'));
        $this->input->on('error', array($this, 'handleError'));
        $this->input->on('close', array($this, 'close'));
    }

    /** @internal */
    public function handleData($data)
    {
        $this->buffer .= $data;
        $len = strlen($this->buffer);

        $sequence = '';
        $expect = 0;
        $out = '';

        for ($i = 0; $i < $len; ++$i) {
            $char = $this->buffer[$i];
            $code = ord($char);

            if ($code & 128) {
                // multi-byte sequence
                if ($code & 64) {
                    // this is the start of a sequence

                    // unexpected start of sequence because already within sequence
                    if ($expect !== 0) {
                        $out .= str_repeat($this->invalid, strlen($sequence));
                        $sequence = '';
                    }

                    $sequence = $char;
                    $expect = 2;

                    if ($code & 32) {
                        ++$expect;
                        if ($code & 16) {
                            ++$expect;

                            if ($code & 8) {
                                // invalid sequence start length
                                $out .= $this->invalid;
                                $sequence = '';
                                $expect = 0;
                            }
                        }
                    }
                } else {
                    // this is a follow-up byte in a sequence
                    if ($expect === 0) {
                        // we're not within a sequence in first place
                        $out .= $this->invalid;
                    } else {
                        // valid following byte in sequence
                        $sequence .= $char;

                        // sequence reached expected length => add to output
                        if (strlen($sequence) === $expect) {
                            $out .= $sequence;
                            $sequence = '';
                            $expect = 0;
                        }
                    }
                }
            } else {
                // simple ASCII character found

                // unexpected because already within sequence
                if ($expect !== 0) {
                    $out .= str_repeat($this->invalid, strlen($sequence));
                    $sequence = '';
                    $expect = 0;
                }

                $out .= $char;
            }
        }

        if ($out !== '') {
            $this->buffer = substr($this->buffer, strlen($out));

            $this->emit('data', array($out));
        }
    }

    /** @internal */
    public function handleEnd()
    {
        if ($this->buffer !== '' && $this->invalid !== '') {
            $data = str_repeat($this->invalid, strlen($this->buffer));
            $this->buffer = '';

            $this->emit('data', array($data));
        }

        if (!$this->closed) {
            $this->emit('end');
            $this->close();
        }
    }

    /** @internal */
    public function handleError(\Exception $error)
    {
        $this->emit('error', array($error));
        $this->close();
    }

    public function isReadable()
    {
        return !$this->closed && $this->input->isReadable();
    }

    public function close()
    {
        if ($this->closed) {
            return;
        }

        $this->closed = true;
        $this->buffer = '';

        $this->input->close();

        $this->emit('close');
        $this->removeAllListeners();
    }

    public function pause()
    {
        $this->input->pause();
    }

    public function resume()
    {
        $this->input->resume();
    }

    public function pipe(WritableStreamInterface $dest, array $options = array())
    {
        Util::pipe($this, $dest, $options);

        return $dest;
    }
}