2020-04-24 14:24:29 -04:00
|
|
|
# bytes
|
|
|
|
|
|
|
|
bytes module is made to provide helpers to manipulation of bytes slice.
|
|
|
|
|
|
|
|
# usage
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
All the following functions are exposed in `mod.ts`.
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
## indexOf
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
Find first index of binary pattern from given binary array, or -1 if it is not
|
|
|
|
present.
|
2020-04-24 14:24:29 -04:00
|
|
|
|
|
|
|
```typescript
|
2020-12-06 09:51:13 -05:00
|
|
|
import { indexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
indexOf(
|
2020-04-24 14:24:29 -04:00
|
|
|
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0, 1, 2]),
|
2020-12-06 09:51:13 -05:00
|
|
|
); // => returns 2
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
indexOf(
|
|
|
|
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
|
|
|
|
new Uint8Array([0, 1, 2]),
|
|
|
|
3,
|
|
|
|
); // => returns 5
|
2020-04-24 14:24:29 -04:00
|
|
|
```
|
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
## lastIndexOf
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
Find last index of binary pattern from given binary array, or -1 if it is not
|
|
|
|
present.
|
2020-04-24 14:24:29 -04:00
|
|
|
|
|
|
|
```typescript
|
2020-12-06 09:51:13 -05:00
|
|
|
import { lastIndexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
lastIndexOf(
|
|
|
|
new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0, 1, 2]),
|
2020-12-06 09:51:13 -05:00
|
|
|
); // => returns 5
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
lastIndexOf(
|
|
|
|
new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
|
|
|
|
new Uint8Array([0, 1, 2]),
|
|
|
|
3,
|
|
|
|
); // => returns 0
|
2020-04-24 14:24:29 -04:00
|
|
|
```
|
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
## equals
|
2020-04-24 14:24:29 -04:00
|
|
|
|
|
|
|
Check whether given binary arrays are equal to each other.
|
|
|
|
|
|
|
|
```typescript
|
2020-12-06 09:51:13 -05:00
|
|
|
import { equals } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); // returns true
|
|
|
|
equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 4])); // returns false
|
2020-04-24 14:24:29 -04:00
|
|
|
```
|
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
## startsWith
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
Check whether binary array starts with prefix.
|
2020-04-24 14:24:29 -04:00
|
|
|
|
|
|
|
```typescript
|
2020-12-06 09:51:13 -05:00
|
|
|
import { startsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
2020-04-24 14:24:29 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns true
|
|
|
|
startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns false
|
2020-04-24 14:24:29 -04:00
|
|
|
```
|
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
## endsWith
|
2020-10-08 15:39:02 -04:00
|
|
|
|
|
|
|
Check whether binary array ends with suffix.
|
|
|
|
|
|
|
|
```typescript
|
2020-12-06 09:51:13 -05:00
|
|
|
import { endsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
2020-10-08 15:39:02 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns false
|
|
|
|
endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns true
|
2020-10-08 15:39:02 -04:00
|
|
|
```
|
|
|
|
|
2020-04-24 14:24:29 -04:00
|
|
|
## repeat
|
|
|
|
|
|
|
|
Repeat bytes of given binary array and return new one.
|
|
|
|
|
|
|
|
```typescript
|
2020-10-04 08:18:36 -04:00
|
|
|
import { repeat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
2020-04-24 14:24:29 -04:00
|
|
|
|
|
|
|
repeat(new Uint8Array([1]), 3); // returns Uint8Array(3) [ 1, 1, 1 ]
|
|
|
|
```
|
|
|
|
|
|
|
|
## concat
|
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
Concatenate multiple binary arrays and return new one.
|
2020-04-24 14:24:29 -04:00
|
|
|
|
|
|
|
```typescript
|
2020-10-04 08:18:36 -04:00
|
|
|
import { concat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
2020-04-24 14:24:29 -04:00
|
|
|
|
|
|
|
concat(new Uint8Array([1, 2]), new Uint8Array([3, 4])); // returns Uint8Array(4) [ 1, 2, 3, 4 ]
|
2020-12-06 09:51:13 -05:00
|
|
|
|
|
|
|
concat(
|
|
|
|
new Uint8Array([1, 2]),
|
|
|
|
new Uint8Array([3, 4]),
|
|
|
|
new Uint8Array([5, 6]),
|
|
|
|
new Uint8Array([7, 8]),
|
|
|
|
); // => returns Uint8Array(8) [ 1, 2, 3, 4, 5, 6, 7, 8 ]
|
2020-04-24 14:24:29 -04:00
|
|
|
```
|
2020-06-25 06:40:51 -04:00
|
|
|
|
2020-10-08 15:39:02 -04:00
|
|
|
## contains
|
|
|
|
|
|
|
|
Check source array contains pattern array.
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
import { contains } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
|
|
|
|
|
|
|
contains(
|
|
|
|
new Uint8Array([1, 2, 0, 1, 2, 0, 2, 1, 3]),
|
|
|
|
new Uint8Array([0, 1, 2]),
|
|
|
|
); // => returns true
|
|
|
|
|
|
|
|
contains(
|
|
|
|
new Uint8Array([1, 2, 0, 1, 2, 0, 2, 1, 3]),
|
|
|
|
new Uint8Array([2, 2]),
|
|
|
|
); // => returns false
|
|
|
|
```
|
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
## copy
|
2020-06-25 06:40:51 -04:00
|
|
|
|
|
|
|
Copy bytes from one binary array to another.
|
|
|
|
|
|
|
|
```typescript
|
2020-12-06 09:51:13 -05:00
|
|
|
import { copy } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
|
2020-06-25 06:40:51 -04:00
|
|
|
|
|
|
|
const dst = new Uint8Array(4);
|
|
|
|
const src = Uint8Array.of(1, 2, 3, 4);
|
2020-12-06 09:51:13 -05:00
|
|
|
const len = copy(src, dest); // returns len = 4
|
2020-06-25 06:40:51 -04:00
|
|
|
```
|