deno.land / std@0.166.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
/** * Provides helper functions to manipulate `Uint8Array` byte slices that are not * included on the `Uint8Array` prototype. * * @module */
/** Returns the index of the first occurrence of the needle array in the source * array, or -1 if it is not present. * * A start index can be specified as the third argument that begins the search * at that given index. The start index defaults to the start of the array. * * The complexity of this function is O(source.lenth * needle.length). * * ```ts * import { indexOfNeedle } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]); * const needle = new Uint8Array([1, 2]); * console.log(indexOfNeedle(source, needle)); // 1 * console.log(indexOfNeedle(source, needle, 2)); // 3 * ``` */export function indexOfNeedle( source: Uint8Array, needle: Uint8Array, start = 0,): number { if (start >= source.length) { return -1; } if (start < 0) { start = Math.max(0, source.length + start); } const s = needle[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 < needle.length) { j++; if (source[j] !== needle[j - pin]) { break; } matched++; } if (matched === needle.length) { return pin; } } return -1;}
/** Returns the index of the last occurrence of the needle array in the source * array, or -1 if it is not present. * * A start index can be specified as the third argument that begins the search * at that given index. The start index defaults to the end of the array. * * The complexity of this function is O(source.lenth * needle.length). * * ```ts * import { lastIndexOfNeedle } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]); * const needle = new Uint8Array([1, 2]); * console.log(lastIndexOfNeedle(source, needle)); // 5 * console.log(lastIndexOfNeedle(source, needle, 4)); // 3 * ``` */export function lastIndexOfNeedle( source: Uint8Array, needle: Uint8Array, start = source.length - 1,): number { if (start < 0) { return -1; } if (start >= source.length) { start = source.length - 1; } const e = needle[needle.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 < needle.length) { j--; if (source[j] !== needle[needle.length - 1 - (pin - j)]) { break; } matched++; } if (matched === needle.length) { return pin - needle.length + 1; } } return -1;}
/** Returns true if the prefix array appears at the start of the source array, * false otherwise. * * The complexity of this function is O(prefix.length). * * ```ts * import { startsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]); * const prefix = new Uint8Array([0, 1, 2]); * console.log(startsWith(source, prefix)); // true * ``` */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;}
/** Returns true if the suffix array appears at the end of the source array, * false otherwise. * * The complexity of this function is O(suffix.length). * * ```ts * import { endsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]); * const suffix = new Uint8Array([1, 2, 3]); * console.log(endsWith(source, suffix)); // true * ``` */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;}
/** Returns a new Uint8Array composed of `count` repetitions of the `source` * array. * * If `count` is negative, a `RangeError` is thrown. * * ```ts * import { repeat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const source = new Uint8Array([0, 1, 2]); * console.log(repeat(source, 3)); // [0, 1, 2, 0, 1, 2, 0, 1, 2] * console.log(repeat(source, 0)); // [] * console.log(repeat(source, -1)); // RangeError * ``` */export function repeat(source: Uint8Array, count: number): Uint8Array { if (count === 0) { return new Uint8Array(); }
if (count < 0) { throw new RangeError("bytes: negative repeat count"); } else if ((source.length * count) / count !== source.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(source.length * count);
let bp = copy(source, nb);
for (; bp < nb.length; bp *= 2) { copy(nb.slice(0, bp), nb, bp); }
return nb;}
/** Concatenate the given arrays into a new Uint8Array. * * ```ts * import { concat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const a = new Uint8Array([0, 1, 2]); * const b = new Uint8Array([3, 4, 5]); * console.log(concat(a, b)); // [0, 1, 2, 3, 4, 5] */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;}
/** Returns true if the source array contains the needle array, false otherwise. * * A start index can be specified as the third argument that begins the search * at that given index. The start index defaults to the beginning of the array. * * The complexity of this function is O(source.length * needle.length). * * ```ts * import { includesNeedle } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]); * const needle = new Uint8Array([1, 2]); * console.log(includesNeedle(source, needle)); // true * console.log(includesNeedle(source, needle, 6)); // false * ``` */export function includesNeedle( source: Uint8Array, needle: Uint8Array, start = 0,): boolean { return indexOfNeedle(source, needle, start) !== -1;}
/** Copy bytes from the `src` array to the `dst` array. Returns the number of * bytes copied. * * If the `src` array is larger than what the `dst` array can hold, only the * amount of bytes that fit in the `dst` array are copied. * * An offset can be specified as the third argument that begins the copy at * that given index in the `dst` array. The offset defaults to the beginning of * the array. * * ```ts * import { copy } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const src = new Uint8Array([9, 8, 7]); * const dst = new Uint8Array([0, 1, 2, 3, 4, 5]); * console.log(copy(src, dst)); // 3 * console.log(dst); // [9, 8, 7, 3, 4, 5] * ``` * * ```ts * import { copy } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts"; * const src = new Uint8Array([1, 1, 1, 1]); * const dst = new Uint8Array([0, 0, 0, 0]); * console.log(copy(src, dst, 1)); // 3 * console.log(dst); // [0, 1, 1, 1] * ``` */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;}
export { equals } from "./equals.ts";
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago