15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function firstIndexOf(haystack, needles, byteOffset) {
    return needles.reduce((prevBest, needle, needleIndex) => {
        const index = haystack.indexOf(needle, byteOffset);
        if (index >= 0 && (!prevBest || index < prevBest.index))
            return { index, needle, needleIndex };
        else
            return prevBest;
    }, null);
}
exports.firstIndexOf = firstIndexOf;
const namedDelimiters = {
    cr: [Buffer.from([13])],
    crlf: [Buffer.from([13, 10])],
    eol: [],
    lf: [Buffer.from([10])],
    nul: [Buffer.from([0])],
    tab: [Buffer.from([9])]
};
namedDelimiters.eol = [...namedDelimiters.crlf, ...namedDelimiters.cr, ...namedDelimiters.lf];
function bufferSplitMulti(buffer, delimiters, includeDelimiters = false) {
    const binaryDelimiters = [];
    for (const delimiter of delimiters) {
        if (typeof delimiter === "string")
            binaryDelimiters.push(...namedDelimiters[delimiter]);
        else
            binaryDelimiters.push(delimiter);
    }
    const ret = [];
    let pos = 0;
    const max = buffer.length;
    while (pos < max) {
        const next = firstIndexOf(buffer, binaryDelimiters, pos);
        if (next === null) {
            ret.push(buffer.slice(pos));
            break;
        }
        else {
            ret.push(buffer.slice(pos, next.index));
            if (includeDelimiters) {
                // Don't return next.needle; it's internal mutable data that must not leak.
                ret.push(buffer.slice(next.index, next.needle.length));
            }
            pos = next.index + next.needle.length;
        }
    }
    return ret;
}
exports.bufferSplitMulti = bufferSplitMulti;
//# sourceMappingURL=buffer-split.js.map