* Get a number of last bytes in an Uint8Array `data` ending at `len` that don't * form a codepoint yet, but can be a part of a single codepoint on more data. * @param {Uint8Array} data Uint8Array of potentially UTF-8 bytes * @param {number} len Position to look behind from * @returns {number} Nu
(data, len)
| 16 | * @returns {number} Number of unfinished potentially valid UTF-8 bytes ending at position `len` |
| 17 | */ |
| 18 | function unfinishedBytesUtf8(data, len) { |
| 19 | // 0-3 |
| 20 | let pos = 0; |
| 21 | while (pos < 2 && pos < len && (data[len - pos - 1] & 0xc0) === 0x80) pos++; // Go back 0-2 trailing bytes |
| 22 | if (pos === len) return 0; // no space for lead |
| 23 | const lead = data[len - pos - 1]; |
| 24 | if (lead < 0xc2 || lead > 0xf4) return 0; // not a lead |
| 25 | if (pos === 0) return 1; // Nothing to recheck, we have only lead, return it. 2-byte must return here |
| 26 | if (lead < 0xe0 || (lead < 0xf0 && pos >= 2)) return 0; // 2-byte, or 3-byte or less and we already have 2 trailing |
| 27 | const lower = lead === 0xf0 ? 0x90 : lead === 0xe0 ? 0xa0 : 0x80; |
| 28 | const upper = lead === 0xf4 ? 0x8f : lead === 0xed ? 0x9f : 0xbf; |
| 29 | const next = data[len - pos]; |
| 30 | return next >= lower && next <= upper ? pos + 1 : 0; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Merge prefix `chunk` with `data` and return new combined prefix. |
no outgoing calls
no test coverage detected
searching dependent graphs…