| 2177 | } |
| 2178 | |
| 2179 | class TextInput implements Machine<string, Event> { |
| 2180 | private readonly parser: Parser |
| 2181 | // A high surrogate at the end of a chunk, which pairs with the next chunk |
| 2182 | private surrogate = "" |
| 2183 | |
| 2184 | constructor(parser: Parser) { |
| 2185 | this.parser = parser |
| 2186 | } |
| 2187 | |
| 2188 | get out(): Array<Event> { |
| 2189 | return this.parser.out |
| 2190 | } |
| 2191 | set out(out: Array<Event>) { |
| 2192 | this.parser.out = out |
| 2193 | } |
| 2194 | |
| 2195 | push(chunk: string): void { |
| 2196 | if (this.surrogate !== "") { |
| 2197 | chunk = this.surrogate + chunk |
| 2198 | this.surrogate = "" |
| 2199 | } |
| 2200 | const last = chunk.charCodeAt(chunk.length - 1) |
| 2201 | if (last >= 0xD800 && last <= 0xDBFF) { |
| 2202 | this.surrogate = chunk.slice(-1) |
| 2203 | chunk = chunk.slice(0, -1) |
| 2204 | } |
| 2205 | this.parser.feed(chunk) |
| 2206 | } |
| 2207 | |
| 2208 | end(): void { |
| 2209 | // An unpaired high surrogate fails as an invalid character |
| 2210 | this.parser.feed(this.surrogate) |
| 2211 | this.parser.end() |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | const utf8Length = (lead: number): number => lead >= 0xF0 ? 4 : lead >= 0xE0 ? 3 : lead >= 0xC0 ? 2 : 1 |
| 2216 |
nothing calls this directly
no outgoing calls
no test coverage detected