From 33f62789cde407059abba0a7ac18b2145c648ea7 Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Mon, 11 Feb 2019 08:49:48 +0900 Subject: feat: multipart, etc.. (denoland/deno_std#180) Original: https://github.com/denoland/deno_std/commit/fda9c98d055091fa886fa444ebd1adcd2ecd21bc --- bytes/bytes.ts | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ bytes/bytes_test.ts | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 bytes/bytes.ts create mode 100644 bytes/bytes_test.ts (limited to 'bytes') diff --git a/bytes/bytes.ts b/bytes/bytes.ts new file mode 100644 index 000000000..ef333288e --- /dev/null +++ b/bytes/bytes.ts @@ -0,0 +1,60 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +/** Find first index of binary pattern from a. If not found, then return -1 **/ +export function bytesFindIndex(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; + while (matched < pat.length) { + i++; + if (a[i] !== pat[i - 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 bytesFindLastIndex(a: Uint8Array, pat: Uint8Array) { + 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; + while (matched < pat.length) { + i--; + if (a[i] !== pat[pat.length - 1 - (pin - i)]) { + break; + } + matched++; + } + if (matched === pat.length) { + return pin - pat.length + 1; + } + } + return -1; +} + +/** Check whether binary arrays are equal to each other **/ +export function bytesEqual(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 bytesHasPrefix(a: Uint8Array, prefix: Uint8Array): boolean { + for (let i = 0, max = prefix.length; i < max; i++) { + if (a[i] !== prefix[i]) return false; + } + return true; +} diff --git a/bytes/bytes_test.ts b/bytes/bytes_test.ts new file mode 100644 index 000000000..3d87497fe --- /dev/null +++ b/bytes/bytes_test.ts @@ -0,0 +1,36 @@ +import { + bytesFindIndex, + bytesFindLastIndex, + bytesEqual, + bytesHasPrefix +} from "./bytes.ts"; +import { assertEqual, test } from "./deps.ts"; + +test(function bytesBytesFindIndex() { + const i = bytesFindIndex( + new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]), + new Uint8Array([0, 1, 2]) + ); + assertEqual(i, 2); +}); + +test(function bytesBytesFindLastIndex1() { + const i = bytesFindLastIndex( + new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]), + new Uint8Array([0, 1, 2]) + ); + assertEqual(i, 3); +}); + +test(function bytesBytesBytesEqual() { + const v = bytesEqual( + new Uint8Array([0, 1, 2, 3]), + new Uint8Array([0, 1, 2, 3]) + ); + assertEqual(v, true); +}); + +test(function bytesBytesHasPrefix() { + const v = bytesHasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); + assertEqual(v, true); +}); -- cgit v1.2.3