From c337d2c4349abd710b1ab0d4de3f1041cd02715c Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Mon, 23 Mar 2020 03:49:09 +0900 Subject: clean up textproto code in std (#4458) - moved and renamed append() into bytes from ws and textproto - renamed textproto/readder_tests.ts -> textproto/test.ts --- std/bytes/mod.ts | 16 ++++++++++++---- std/bytes/test.ts | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 13 deletions(-) (limited to 'std/bytes') diff --git a/std/bytes/mod.ts b/std/bytes/mod.ts index 0157b39a5..88eb97710 100644 --- a/std/bytes/mod.ts +++ b/std/bytes/mod.ts @@ -7,8 +7,8 @@ export function findIndex(a: Uint8Array, pat: Uint8Array): number { for (let i = 0; i < a.length; i++) { if (a[i] !== s) continue; const pin = i; - let matched = 1, - j = i; + let matched = 1; + let j = i; while (matched < pat.length) { j++; if (a[j] !== pat[j - pin]) { @@ -29,8 +29,8 @@ export function findLastIndex(a: Uint8Array, pat: Uint8Array): number { for (let i = a.length - 1; i >= 0; i--) { if (a[i] !== e) continue; const pin = i; - let matched = 1, - j = i; + let matched = 1; + let j = i; while (matched < pat.length) { j--; if (a[j] !== pat[pat.length - 1 - (pin - j)]) { @@ -94,3 +94,11 @@ export function repeat(b: Uint8Array, count: number): Uint8Array { return nb; } + +/** Concatenate two binary arrays and return new one */ +export function concat(a: Uint8Array, b: Uint8Array): Uint8Array { + const output = new Uint8Array(a.length + b.length); + output.set(a, 0); + output.set(b, a.length); + return output; +} diff --git a/std/bytes/test.ts b/std/bytes/test.ts index c1609ebdc..3ded0cd85 100644 --- a/std/bytes/test.ts +++ b/std/bytes/test.ts @@ -1,9 +1,17 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { findIndex, findLastIndex, equal, hasPrefix, repeat } from "./mod.ts"; -import { assertEquals, assertThrows } from "../testing/asserts.ts"; +import { + findIndex, + findLastIndex, + equal, + hasPrefix, + repeat, + concat +} from "./mod.ts"; +import { assertEquals, assertThrows, assert } from "../testing/asserts.ts"; +import { encode, decode } from "../strings/mod.ts"; -Deno.test(function bytesfindIndex1(): void { +Deno.test("[bytes] findIndex1", () => { const i = findIndex( new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]), new Uint8Array([0, 1, 2]) @@ -11,12 +19,12 @@ Deno.test(function bytesfindIndex1(): void { assertEquals(i, 2); }); -Deno.test(function bytesfindIndex2(): void { +Deno.test("[bytes] findIndex2", () => { const i = findIndex(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1])); assertEquals(i, 1); }); -Deno.test(function bytesfindLastIndex1(): void { +Deno.test("[bytes] findLastIndex1", () => { const i = findLastIndex( new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]), new Uint8Array([0, 1, 2]) @@ -24,22 +32,22 @@ Deno.test(function bytesfindLastIndex1(): void { assertEquals(i, 3); }); -Deno.test(function bytesfindLastIndex2(): void { +Deno.test("[bytes] findLastIndex2", () => { const i = findLastIndex(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1])); assertEquals(i, 0); }); -Deno.test(function bytesBytesequal(): void { +Deno.test("[bytes] equal", () => { const v = equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); assertEquals(v, true); }); -Deno.test(function byteshasPrefix(): void { +Deno.test("[bytes] hasPrefix", () => { const v = hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); assertEquals(v, true); }); -Deno.test(function bytesrepeat(): void { +Deno.test("[bytes] repeat", () => { // input / output / count / error message const repeatTestCase = [ ["", "", 0], @@ -71,3 +79,21 @@ Deno.test(function bytesrepeat(): void { } } }); + +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); +}); -- cgit v1.2.3