std/bytes
2021-10-07 02:10:43 +09:00
..
bytes_list_test.ts docs: correct typos (#1207) 2021-09-07 16:40:05 +09:00
bytes_list.ts docs(bytes): fix typo in *iterator() description (#1162) 2021-08-25 12:41:08 +05:30
equals_bench.ts perf(bytes): switch equals to simd for large byte arrays (#1349) 2021-10-07 02:10:43 +09:00
equals.ts perf(bytes): switch equals to simd for large byte arrays (#1349) 2021-10-07 02:10:43 +09:00
mod.ts perf(bytes): switch equals to simd for large byte arrays (#1349) 2021-10-07 02:10:43 +09:00
README.md refactor(bytes): rename contains to includes with optional argument fromIndex (#1133) 2021-08-16 14:22:48 +09:00
test.ts perf(bytes): switch equals to simd for large byte arrays (#1349) 2021-10-07 02:10:43 +09:00

Bytes

The bytes module provides helper functions to manipulate byte slices.

Usage

The following functions are exposed in mod.ts.

indexOf

Finds the first index of a binary pattern within a given binary array, or returns -1 if the pattern is not present.

import { indexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

indexOf(
  new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
  new Uint8Array([0, 1, 2]),
); // => returns 2

indexOf(
  new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
  new Uint8Array([0, 1, 2]),
  3,
); // => returns 5

lastIndexOf

Finds the last index of a binary pattern within a given binary array, or returns -1 if the pattern is not present.

import { lastIndexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

lastIndexOf(
  new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
  new Uint8Array([0, 1, 2]),
); // => returns 5

lastIndexOf(
  new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
  new Uint8Array([0, 1, 2]),
  3,
); // => returns 0

equals

Checks whether two given binary arrays are equal to each other.

import { equals } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

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

startsWith

Checks whether a binary array starts with a binary array prefix.

import { startsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns true
startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns false

endsWith

Checks whether binary array ends with a binary array suffix.

import { endsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns false
endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns true

repeat

Creates a binary array consisting of multiple copies of a given binary array.

import { repeat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

repeat(new Uint8Array([1]), 3); // returns Uint8Array(3) [ 1, 1, 1 ]

concat

Concatenates multiple binary arrays into a new one.

import { concat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

concat(new Uint8Array([1, 2]), new Uint8Array([3, 4])); // returns Uint8Array(4) [ 1, 2, 3, 4 ]

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 ]

includes

Checks that a given binary array includes a sequence corresponding to a pattern array.

import { includes } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

includes(
  new Uint8Array([1, 2, 0, 1, 2, 0, 2, 1, 3]),
  new Uint8Array([0, 1, 2]),
); // => returns true

includes(
  new Uint8Array([1, 2, 0, 1, 2, 0, 2, 1, 3]),
  new Uint8Array([2, 2]),
); // => returns false

copy

Copies bytes from one binary array to another one.

import { copy } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";

const dest = new Uint8Array(4);
const src = Uint8Array.of(1, 2, 3, 4);
const len = copy(src, dest); // returns len = 4