summaryrefslogtreecommitdiff
path: root/std/bytes
diff options
context:
space:
mode:
Diffstat (limited to 'std/bytes')
-rw-r--r--std/bytes/README.md137
-rw-r--r--std/bytes/mod.ts190
-rw-r--r--std/bytes/test.ts213
3 files changed, 0 insertions, 540 deletions
diff --git a/std/bytes/README.md b/std/bytes/README.md
deleted file mode 100644
index ae0c988ec..000000000
--- a/std/bytes/README.md
+++ /dev/null
@@ -1,137 +0,0 @@
-# bytes
-
-bytes module is made to provide helpers to manipulation of bytes slice.
-
-# usage
-
-All the following functions are exposed in `mod.ts`.
-
-## indexOf
-
-Find first index of binary pattern from given binary array, or -1 if it is not
-present.
-
-```typescript
-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
-
-Find last index of binary pattern from given binary array, or -1 if it is not
-present.
-
-```typescript
-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
-
-Check whether given binary arrays are equal to each other.
-
-```typescript
-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
-
-Check whether binary array starts with prefix.
-
-```typescript
-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
-
-Check whether binary array ends with suffix.
-
-```typescript
-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
-
-Repeat bytes of given binary array and return new one.
-
-```typescript
-import { repeat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
-
-repeat(new Uint8Array([1]), 3); // returns Uint8Array(3) [ 1, 1, 1 ]
-```
-
-## concat
-
-Concatenate multiple binary arrays and return new one.
-
-```typescript
-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 ]
-```
-
-## 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
-```
-
-## copy
-
-Copy bytes from one binary array to another.
-
-```typescript
-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
-```
diff --git a/std/bytes/mod.ts b/std/bytes/mod.ts
deleted file mode 100644
index bb2b7d46e..000000000
--- a/std/bytes/mod.ts
+++ /dev/null
@@ -1,190 +0,0 @@
-// 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;
-}
diff --git a/std/bytes/test.ts b/std/bytes/test.ts
deleted file mode 100644
index 17db0672d..000000000
--- a/std/bytes/test.ts
+++ /dev/null
@@ -1,213 +0,0 @@
-// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-
-import {
- concat,
- contains,
- copy,
- endsWith,
- equals,
- indexOf,
- lastIndexOf,
- repeat,
- startsWith,
-} from "./mod.ts";
-import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
-import { decode, encode } from "../encoding/utf8.ts";
-
-Deno.test("[bytes] indexOf1", () => {
- const i = indexOf(
- new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
- new Uint8Array([0, 1, 2]),
- );
- assertEquals(i, 2);
-});
-
-Deno.test("[bytes] indexOf2", () => {
- const i = indexOf(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1]));
- assertEquals(i, 1);
-});
-
-Deno.test("[bytes] indexOf3", () => {
- const i = indexOf(encode("Deno"), encode("D"));
- assertEquals(i, 0);
-});
-
-Deno.test("[bytes] indexOf4", () => {
- const i = indexOf(new Uint8Array(), new Uint8Array([0, 1]));
- assertEquals(i, -1);
-});
-
-Deno.test("[bytes] indexOf with start index", () => {
- const i = indexOf(
- new Uint8Array([0, 1, 2, 0, 1, 2]),
- new Uint8Array([0, 1]),
- 1,
- );
- assertEquals(i, 3);
-});
-
-Deno.test("[bytes] indexOf with start index 2", () => {
- const i = indexOf(
- new Uint8Array([0, 1, 2, 0, 1, 2]),
- new Uint8Array([0, 1]),
- 7,
- );
- assertEquals(i, -1);
-});
-
-Deno.test("[bytes] lastIndexOf1", () => {
- const i = lastIndexOf(
- new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]),
- new Uint8Array([0, 1, 2]),
- );
- assertEquals(i, 3);
-});
-
-Deno.test("[bytes] lastIndexOf2", () => {
- const i = lastIndexOf(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1]));
- assertEquals(i, 0);
-});
-
-Deno.test("[bytes] lastIndexOf3", () => {
- const i = lastIndexOf(new Uint8Array(), new Uint8Array([0, 1]));
- assertEquals(i, -1);
-});
-
-Deno.test("[bytes] lastIndexOf with start index", () => {
- const i = lastIndexOf(
- new Uint8Array([0, 1, 2, 0, 1, 2]),
- new Uint8Array([0, 1]),
- 2,
- );
- assertEquals(i, 0);
-});
-
-Deno.test("[bytes] lastIndexOf with start index 2", () => {
- const i = lastIndexOf(
- new Uint8Array([0, 1, 2, 0, 1, 2]),
- new Uint8Array([0, 1]),
- -1,
- );
- assertEquals(i, -1);
-});
-
-Deno.test("[bytes] equals", () => {
- const v = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3]));
- assertEquals(v, true);
-});
-
-Deno.test("[bytes] startsWith", () => {
- const v = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
- assertEquals(v, true);
-});
-
-Deno.test("[bytes] endsWith", () => {
- const v = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2]));
- assertEquals(v, true);
-});
-
-Deno.test("[bytes] repeat", () => {
- // input / output / count / error message
- const repeatTestCase = [
- ["", "", 0],
- ["", "", 1],
- ["", "", 1.1, "bytes: repeat count must be an integer"],
- ["", "", 2],
- ["", "", 0],
- ["-", "", 0],
- ["-", "-", -1, "bytes: negative repeat count"],
- ["-", "----------", 10],
- ["abc ", "abc abc abc ", 3],
- ];
- for (const [input, output, count, errMsg] of repeatTestCase) {
- if (errMsg) {
- assertThrows(
- (): void => {
- repeat(new TextEncoder().encode(input as string), count as number);
- },
- Error,
- errMsg as string,
- );
- } else {
- const newBytes = repeat(
- new TextEncoder().encode(input as string),
- count as number,
- );
-
- assertEquals(new TextDecoder().decode(newBytes), output);
- }
- }
-});
-
-Deno.test("[bytes] concat", () => {
- const u1 = encode("Hello ");
- const u2 = encode("World");
- const joined = concat(u1, u2);
- assertEquals(decode(joined), "Hello World");
- assert(u1 !== joined);
- assert(u2 !== joined);
-});
-
-Deno.test("[bytes] concat empty arrays", () => {
- const u1 = new Uint8Array();
- const u2 = new Uint8Array();
- const joined = concat(u1, u2);
- assertEquals(joined.byteLength, 0);
- assert(u1 !== joined);
- assert(u2 !== joined);
-});
-
-Deno.test("[bytes] concat multiple arrays", () => {
- const u1 = encode("Hello ");
- const u2 = encode("W");
- const u3 = encode("o");
- const u4 = encode("r");
- const u5 = encode("l");
- const u6 = encode("d");
- const joined = concat(u1, u2, u3, u4, u5, u6);
- assertEquals(decode(joined), "Hello World");
- assert(u1 !== joined);
- assert(u2 !== joined);
-});
-
-Deno.test("[bytes] contains", () => {
- const source = encode("deno.land");
- const pattern = encode("deno");
- assert(contains(source, pattern));
-
- assert(contains(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3])));
-});
-
-Deno.test("[bytes] copy", function (): void {
- const dst = new Uint8Array(4);
-
- dst.fill(0);
- let src = Uint8Array.of(1, 2);
- let len = copy(src, dst, 0);
- assert(len === 2);
- assertEquals(dst, Uint8Array.of(1, 2, 0, 0));
-
- dst.fill(0);
- src = Uint8Array.of(1, 2);
- len = copy(src, dst, 1);
- assert(len === 2);
- assertEquals(dst, Uint8Array.of(0, 1, 2, 0));
-
- dst.fill(0);
- src = Uint8Array.of(1, 2, 3, 4, 5);
- len = copy(src, dst);
- assert(len === 4);
- assertEquals(dst, Uint8Array.of(1, 2, 3, 4));
-
- dst.fill(0);
- src = Uint8Array.of(1, 2);
- len = copy(src, dst, 100);
- assert(len === 0);
- assertEquals(dst, Uint8Array.of(0, 0, 0, 0));
-
- dst.fill(0);
- src = Uint8Array.of(3, 4);
- len = copy(src, dst, -2);
- assert(len === 2);
- assertEquals(dst, Uint8Array.of(3, 4, 0, 0));
-});