MCPcopy Index your code
hub / github.com/nodejs/node / mergePrefixUtf8

Function mergePrefixUtf8

lib/internal/encoding/util.js:42–70  ·  view source on GitHub ↗

* Merge prefix `chunk` with `data` and return new combined prefix. * For data.length < 3, fully consumes data and can return unfinished data, * otherwise returns a prefix with no unfinished bytes * @param {Uint8Array} data Uint8Array of potentially UTF-8 bytes * @param {Uint8Array} chunk Prefix

(data, chunk)

Source from the content-addressed store, hash-verified

40 * so that the result has no unfinished UTF-8 codepoints. If data.length < 3: concat(chunk, data).
41 */
42function mergePrefixUtf8(data, chunk) {
43 if (data.length === 0) return chunk;
44 if (data.length < 3) {
45 // No reason to bruteforce offsets, also it's possible this doesn't yet end the sequence
46 const res = new Uint8Array(data.length + chunk.length);
47 res.set(chunk);
48 res.set(data, chunk.length);
49 return res;
50 }
51
52 // Slice off a small portion of data into prefix chunk so we can decode them separately without extending array size
53 const temp = new Uint8Array(chunk.length + 3); // We have 1-3 bytes and need 1-3 more bytes
54 temp.set(chunk);
55 temp.set(data.subarray(0, 3), chunk.length);
56
57 // Stop at the first offset where unfinished bytes reaches 0 or fits into data
58 // If that doesn't happen (data too short), just concat chunk and data completely (above)
59 for (let i = 1; i <= 3; i++) {
60 const unfinished = unfinishedBytesUtf8(temp, chunk.length + i); // 0-3
61 if (unfinished <= i) {
62 // Always reachable at 3, but we still need 'unfinished' value for it
63 const add = i - unfinished; // 0-3
64 return add > 0 ? temp.subarray(0, chunk.length + add) : chunk;
65 }
66 }
67
68 // Unreachable
69 return null;
70}
71
72module.exports = { unfinishedBytesUtf8, mergePrefixUtf8 };

Callers 1

decodeMethod · 0.85

Calls 2

unfinishedBytesUtf8Function · 0.85
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…