File size: 4,891 Bytes
391a73c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { toUint8Array, toUint8ArrayCopy } from "./buffer-source.js";
function clampIndex(value, fallback, length) {
    const normalized = Number.isFinite(value) ? Math.trunc(value) : fallback;
    if (normalized <= 0) {
        return 0;
    }
    if (normalized >= length) {
        return length;
    }
    return normalized;
}
function normalizeForwardRange(length, options) {
    const start = clampIndex(options?.start, 0, length);
    const end = clampIndex(options?.end, length, length);
    return end >= start ? [start, end] : [start, start];
}
function normalizeReverseRange(length, options) {
    const start = clampIndex(options?.start, length, length);
    const end = clampIndex(options?.end, 0, length);
    return start >= end ? [end, start] : [start, start];
}
function normalizeSliceIndex(value, fallback, length) {
    const normalized = Number.isFinite(value) ? Math.trunc(value) : fallback;
    if (normalized < 0) {
        return Math.max(length + normalized, 0);
    }
    if (normalized > length) {
        return length;
    }
    return normalized;
}
function encodeAscii(text) {
    const bytes = new Uint8Array(text.length);
    for (let i = 0; i < text.length; i++) {
        bytes[i] = text.charCodeAt(i) & 0xff;
    }
    return bytes;
}
function encodeUtf8(text) {
    return new TextEncoder().encode(text);
}
function toPatternBytes(pattern, options) {
    if (typeof pattern === "string") {
        return options?.encoding === "utf8" ? encodeUtf8(pattern) : encodeAscii(pattern);
    }
    return toUint8Array(pattern);
}
function bytesEqualAt(data, pattern, offset) {
    for (let index = 0; index < pattern.byteLength; index++) {
        if (data[offset + index] !== pattern[index]) {
            return false;
        }
    }
    return true;
}
export function indexOf(data, pattern, options) {
    const bytes = toUint8Array(data);
    const needle = toPatternBytes(pattern, options);
    const [start, end] = normalizeForwardRange(bytes.byteLength, options);
    if (needle.byteLength === 0) {
        return start;
    }
    const lastOffset = end - needle.byteLength;
    if (lastOffset < start) {
        return -1;
    }
    for (let offset = start; offset <= lastOffset; offset++) {
        if (bytesEqualAt(bytes, needle, offset)) {
            return offset;
        }
    }
    return -1;
}
export function lastIndexOf(data, pattern, options) {
    const bytes = toUint8Array(data);
    const needle = toPatternBytes(pattern, options);
    const [end, start] = normalizeReverseRange(bytes.byteLength, options);
    if (needle.byteLength === 0) {
        return start;
    }
    const firstOffset = start - needle.byteLength;
    if (firstOffset < end) {
        return -1;
    }
    for (let offset = firstOffset; offset >= end; offset--) {
        if (bytesEqualAt(bytes, needle, offset)) {
            return offset;
        }
    }
    return -1;
}
export function includes(data, pattern, options) {
    return indexOf(data, pattern, options) !== -1;
}
export function startsWith(data, pattern, options) {
    const bytes = toUint8Array(data);
    const needle = toPatternBytes(pattern, options);
    if (needle.byteLength > bytes.byteLength) {
        return false;
    }
    return bytesEqualAt(bytes, needle, 0);
}
export function endsWith(data, pattern, options) {
    const bytes = toUint8Array(data);
    const needle = toPatternBytes(pattern, options);
    if (needle.byteLength > bytes.byteLength) {
        return false;
    }
    return bytesEqualAt(bytes, needle, bytes.byteLength - needle.byteLength);
}
export function slice(data, start, end) {
    const bytes = toUint8Array(data);
    const normalizedStart = normalizeSliceIndex(start, 0, bytes.byteLength);
    const normalizedEnd = normalizeSliceIndex(end, bytes.byteLength, bytes.byteLength);
    if (normalizedEnd <= normalizedStart) {
        return bytes.subarray(normalizedStart, normalizedStart);
    }
    return bytes.subarray(normalizedStart, normalizedEnd);
}
export function tail(data, length) {
    const bytes = toUint8Array(data);
    const normalizedLength = Number.isFinite(length) ? Math.max(0, Math.trunc(length)) : 0;
    if (normalizedLength >= bytes.byteLength) {
        return bytes;
    }
    return bytes.subarray(bytes.byteLength - normalizedLength);
}
export function copy(data) {
    return toUint8ArrayCopy(data);
}
export function compare(a, b) {
    const left = toUint8Array(a);
    const right = toUint8Array(b);
    const limit = Math.min(left.byteLength, right.byteLength);
    for (let index = 0; index < limit; index++) {
        if (left[index] < right[index]) {
            return -1;
        }
        if (left[index] > right[index]) {
            return 1;
        }
    }
    if (left.byteLength < right.byteLength) {
        return -1;
    }
    if (left.byteLength > right.byteLength) {
        return 1;
    }
    return 0;
}