diff options
Diffstat (limited to 'std/bytes')
-rw-r--r-- | std/bytes/mod.ts | 96 | ||||
-rw-r--r-- | std/bytes/test.ts | 74 |
2 files changed, 170 insertions, 0 deletions
diff --git a/std/bytes/mod.ts b/std/bytes/mod.ts new file mode 100644 index 000000000..a42eaffd2 --- /dev/null +++ b/std/bytes/mod.ts @@ -0,0 +1,96 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { copyBytes } from "../io/util.ts"; + +/** Find first index of binary pattern from a. If not found, then return -1 **/ +export function findIndex(a: Uint8Array, pat: Uint8Array): number { + const s = pat[0]; + for (let i = 0; i < a.length; i++) { + if (a[i] !== s) continue; + const pin = i; + let matched = 1, + j = i; + while (matched < pat.length) { + j++; + if (a[j] !== pat[j - pin]) { + break; + } + matched++; + } + if (matched === pat.length) { + return pin; + } + } + return -1; +} + +/** Find last index of binary pattern from a. If not found, then return -1 **/ +export function findLastIndex(a: Uint8Array, pat: Uint8Array): number { + const e = pat[pat.length - 1]; + for (let i = a.length - 1; i >= 0; i--) { + if (a[i] !== e) continue; + const pin = i; + let matched = 1, + j = i; + while (matched < pat.length) { + j--; + if (a[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 **/ +export function equal(a: Uint8Array, match: Uint8Array): boolean { + if (a.length !== match.length) return false; + for (let i = 0; i < match.length; i++) { + if (a[i] !== match[i]) return false; + } + return true; +} + +/** Check whether binary array has binary prefix **/ +export function hasPrefix(a: Uint8Array, prefix: Uint8Array): boolean { + for (let i = 0, max = prefix.length; i < max; i++) { + if (a[i] !== prefix[i]) return false; + } + return true; +} + +/** + * Repeat bytes. returns a new byte slice consisting of `count` copies of `b`. + * @param b The origin bytes + * @param count The count you want to repeat. + */ +export function repeat(b: Uint8Array, count: number): Uint8Array { + if (count === 0) { + return new Uint8Array(); + } + + if (count < 0) { + throw new Error("bytes: negative repeat count"); + } else if ((b.length * count) / count !== b.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(b.length * count); + + let bp = copyBytes(nb, b); + + for (; bp < nb.length; bp *= 2) { + copyBytes(nb, nb.slice(0, bp), bp); + } + + return nb; +} diff --git a/std/bytes/test.ts b/std/bytes/test.ts new file mode 100644 index 000000000..0a779dd3d --- /dev/null +++ b/std/bytes/test.ts @@ -0,0 +1,74 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +import { findIndex, findLastIndex, equal, hasPrefix, repeat } from "./mod.ts"; +import { test } from "../testing/mod.ts"; +import { assertEquals, assertThrows } from "../testing/asserts.ts"; + +test(function bytesfindIndex1(): void { + const i = findIndex( + new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]), + new Uint8Array([0, 1, 2]) + ); + assertEquals(i, 2); +}); + +test(function bytesfindIndex2(): void { + const i = findIndex(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1])); + assertEquals(i, 1); +}); + +test(function bytesfindLastIndex1(): void { + const i = findLastIndex( + new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]), + new Uint8Array([0, 1, 2]) + ); + assertEquals(i, 3); +}); + +test(function bytesfindLastIndex2(): void { + const i = findLastIndex(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1])); + assertEquals(i, 0); +}); + +test(function bytesBytesequal(): void { + const v = equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); + assertEquals(v, true); +}); + +test(function byteshasPrefix(): void { + const v = hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); + assertEquals(v, true); +}); + +test(function bytesrepeat(): void { + // 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); + } + } +}); |