|
6 | 6 | use React\Stream\ReadableStreamInterface; |
7 | 7 | use React\Stream\WritableStreamInterface; |
8 | 8 | use React\Stream\Util; |
| 9 | +use Clue\React\Utf8\Sequencer as Utf8Sequencer; |
| 10 | +use React\Stream\ReadableStream; |
9 | 11 |
|
10 | 12 | class Readline extends EventEmitter implements ReadableStreamInterface |
11 | 13 | { |
@@ -62,35 +64,19 @@ public function __construct(ReadableStreamInterface $input, WritableStreamInterf |
62 | 64 | $this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_DEL, array($this, 'onKeyDelete')); |
63 | 65 | $this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_END, array($this, 'onKeyEnd')); |
64 | 66 |
|
65 | | - $expect = 0; |
66 | | - $char = ''; |
67 | | - $that = $this; |
68 | | - $this->sequencer->addFallback('', function ($byte) use (&$expect, &$char, $that) { |
69 | | - if ($expect === 0) { |
70 | | - $code = ord($byte); |
71 | | - // count number of bytes expected for this UTF-8 multi-byte character |
72 | | - $expect = 1; |
73 | | - if ($code & 128 && $code & 64) { |
74 | | - ++$expect; |
75 | | - if ($code & 32) { |
76 | | - ++$expect; |
77 | | - if ($code & 16) { |
78 | | - ++$expect; |
79 | | - } |
80 | | - } |
81 | | - } |
82 | | - } |
83 | | - $char .= $byte; |
84 | | - --$expect; |
85 | | - |
86 | | - // forward buffered bytes as a single multi byte character once last byte has been read |
87 | | - if ($expect === 0) { |
88 | | - $save = $char; |
89 | | - $char = ''; |
90 | | - $that->onFallback($save); |
91 | | - } |
| 67 | + // push input into sequencer |
| 68 | + $input->on('data', array($this->sequencer, 'push')); |
| 69 | + |
| 70 | + // push sequencer output through utf8sequencer |
| 71 | + $readable = new ReadableStream(); |
| 72 | + $this->sequencer->addFallback('', function ($data) use ($readable) { |
| 73 | + $readable->emit('data', array($data)); |
92 | 74 | }); |
93 | 75 |
|
| 76 | + // push utf8sequences as input |
| 77 | + $utf8 = new Utf8Sequencer($readable); |
| 78 | + $utf8->on('data', array($this, 'onFallback')); |
| 79 | + |
94 | 80 | $this->sequencer->addFallback(self::ESC_SEQUENCE, function ($bytes) { |
95 | 81 | echo 'unknown sequence: ' . ord($bytes) . PHP_EOL; |
96 | 82 | }); |
@@ -519,7 +505,7 @@ public function onFallback($chars) |
519 | 505 | $post = $this->substr($this->linebuffer, $this->linepos); |
520 | 506 |
|
521 | 507 | $this->linebuffer = $pre . $chars . $post; |
522 | | - ++$this->linepos; |
| 508 | + $this->linepos += $this->strlen($chars); |
523 | 509 |
|
524 | 510 | $this->redraw(); |
525 | 511 | } |
|
0 commit comments