deno.land / std@0.91.0 / bytes / mod.ts

نووسراو ببینە
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
/** Find first index of binary pattern from source. If not found, then return -1 * @param source source array * @param pat pattern to find in source array * @param start the index to start looking in the source */export function indexOf( source: Uint8Array, pat: Uint8Array, start = 0,): number { if (start >= source.length) { return -1; } if (start < 0) { start = 0; } const s = pat[0]; for (let i = start; i < source.length; i++) { if (source[i] !== s) continue; const pin = i; let matched = 1; let j = i; while (matched < pat.length) { j++; if (source[j] !== pat[j - pin]) { break; } matched++; } if (matched === pat.length) { return pin; } } return -1;}
/** Find last index of binary pattern from source. If not found, then return -1. * @param source source array * @param pat pattern to find in source array * @param start the index to start looking in the source */export function lastIndexOf( source: Uint8Array, pat: Uint8Array, start = source.length - 1,): number { if (start < 0) { return -1; } if (start >= source.length) { start = source.length - 1; } const e = pat[pat.length - 1]; for (let i = start; i >= 0; i--) { if (source[i] !== e) continue; const pin = i; let matched = 1; let j = i; while (matched < pat.length) { j--; if (source[j] !== pat[pat.length - 1 - (pin - j)]) { break; } matched++; } if (matched === pat.length) { return pin - pat.length + 1; } } return -1;}
/** Check whether binary arrays are equal to each other. * @param a first array to check equality * @param b second array to check equality */export function equals(a: Uint8Array, b: Uint8Array): boolean { if (a.length !== b.length) return false; for (let i = 0; i < b.length; i++) { if (a[i] !== b[i]) return false; } return true;}
/** Check whether binary array starts with prefix. * @param source source array * @param prefix prefix array to check in source */export function startsWith(source: Uint8Array, prefix: Uint8Array): boolean { for (let i = 0, max = prefix.length; i < max; i++) { if (source[i] !== prefix[i]) return false; } return true;}
/** Check whether binary array ends with suffix. * @param source source array * @param suffix suffix array to check in source */export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean { for ( let srci = source.length - 1, sfxi = suffix.length - 1; sfxi >= 0; srci--, sfxi-- ) { if (source[srci] !== suffix[sfxi]) return false; } return true;}
/** Repeat bytes. returns a new byte slice consisting of `count` copies of `b`. * @param origin The origin bytes * @param count The count you want to repeat. * @throws `RangeError` When count is negative */export function repeat(origin: Uint8Array, count: number): Uint8Array { if (count === 0) { return new Uint8Array(); }
if (count < 0) { throw new RangeError("bytes: negative repeat count"); } else if ((origin.length * count) / count !== origin.length) { throw new Error("bytes: repeat count causes overflow"); }
const int = Math.floor(count);
if (int !== count) { throw new Error("bytes: repeat count must be an integer"); }
const nb = new Uint8Array(origin.length * count);
let bp = copy(origin, nb);
for (; bp < nb.length; bp *= 2) { copy(nb.slice(0, bp), nb, bp); }
return nb;}
/** Concatenate multiple binary arrays and return new one. * @param buf binary arrays to concatenate */export function concat(...buf: Uint8Array[]): Uint8Array { let length = 0; for (const b of buf) { length += b.length; }
const output = new Uint8Array(length); let index = 0; for (const b of buf) { output.set(b, index); index += b.length; }
return output;}
/** Check source array contains pattern array. * @param source source array * @param pat patter array */export function contains(source: Uint8Array, pat: Uint8Array): boolean { return indexOf(source, pat) != -1;}
/** * Copy bytes from one Uint8Array to another. Bytes from `src` which don't fit * into `dst` will not be copied. * * @param src Source byte array * @param dst Destination byte array * @param off Offset into `dst` at which to begin writing values from `src`. * @return number of bytes copied */export function copy(src: Uint8Array, dst: Uint8Array, off = 0): number { off = Math.max(0, Math.min(off, dst.byteLength)); const dstBytesAvailable = dst.byteLength - off; if (src.byteLength > dstBytesAvailable) { src = src.subarray(0, dstBytesAvailable); } dst.set(src, off); return src.byteLength;}
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉