Skip to content

Commit

Permalink
clean up textproto code in std (denoland#4458)
Browse files Browse the repository at this point in the history
- moved and renamed append() into bytes from ws and textproto
- renamed textproto/readder_tests.ts -> textproto/test.ts
  • Loading branch information
keroxp committed Mar 22, 2020
1 parent 07ea145 commit c337d2c
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 235 deletions.
16 changes: 12 additions & 4 deletions std/bytes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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)]) {
Expand Down Expand Up @@ -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;
}
44 changes: 35 additions & 9 deletions std/bytes/test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,53 @@
// 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])
);
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])
);
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],
Expand Down Expand Up @@ -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);
});
20 changes: 4 additions & 16 deletions std/textproto/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@

import { BufReader } from "../io/bufio.ts";
import { charCode } from "../io/util.ts";
import { concat } from "../bytes/mod.ts";
import { decode } from "../strings/mod.ts";

const asciiDecoder = new TextDecoder();
function str(buf: Uint8Array | null | undefined): string {
if (buf == null) {
return "";
} else {
return asciiDecoder.decode(buf);
}
}

export function append(a: Uint8Array, b: Uint8Array): Uint8Array {
if (a == null) {
return b;
} else {
const output = new Uint8Array(a.length + b.length);
output.set(a, 0);
output.set(b, a.length);
return output;
return decode(buf);
}
}

Expand Down Expand Up @@ -146,9 +136,7 @@ export class TextProtoReader {
}
return l;
}

// @ts-ignore
line = append(line, l);
line = line ? concat(line, l) : l;
if (!more) {
break;
}
Expand Down
180 changes: 0 additions & 180 deletions std/textproto/reader_test.ts

This file was deleted.

Loading

0 comments on commit c337d2c

Please sign in to comment.