diff --git a/std/bytes/README.md b/std/bytes/README.md index bf0fcc5b04..77f3fdfc85 100644 --- a/std/bytes/README.md +++ b/std/bytes/README.md @@ -77,3 +77,15 @@ import { concat } from "https://deno.land/std/bytes/mod.ts"; concat(new Uint8Array([1, 2]), new Uint8Array([3, 4])); // returns Uint8Array(4) [ 1, 2, 3, 4 ] ``` + +## copyBytes + +Copy bytes from one binary array to another. + +```typescript +import { concat } from "https://deno.land/std/bytes/mod.ts"; + +const dst = new Uint8Array(4); +const src = Uint8Array.of(1, 2, 3, 4); +const len = copyBytes(src, dest); // returns len = 4 +``` diff --git a/std/bytes/mod.ts b/std/bytes/mod.ts index e52e2365b5..8ae697c298 100644 --- a/std/bytes/mod.ts +++ b/std/bytes/mod.ts @@ -1,8 +1,7 @@ // Copyright 2018-2020 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 - * @param source soruce array + * @param source source array * @param pat pattern to find in source array */ export function findIndex(source: Uint8Array, pat: Uint8Array): number { @@ -27,7 +26,7 @@ export function findIndex(source: Uint8Array, pat: Uint8Array): number { } /** Find last index of binary pattern from a. If not found, then return -1. - * @param source soruce array + * @param source source array * @param pat pattern to find in source array */ export function findLastIndex(source: Uint8Array, pat: Uint8Array): number { @@ -75,7 +74,7 @@ export function hasPrefix(source: Uint8Array, prefix: Uint8Array): boolean { } /** Check whether binary array ends with suffix. - * @param source srouce array + * @param source source array * @param suffix suffix array to check in source */ export function hasSuffix(source: Uint8Array, suffix: Uint8Array): boolean { @@ -132,10 +131,29 @@ export function concat(origin: Uint8Array, b: Uint8Array): Uint8Array { return output; } -/** Check srouce array contains pattern array. - * @param source srouce array +/** Check source array contains pattern array. + * @param source source array * @param pat patter array */ export function contains(source: Uint8Array, pat: Uint8Array): boolean { return findIndex(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 copyBytes(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 index 8b03de9262..25af2a35be 100644 --- a/std/bytes/test.ts +++ b/std/bytes/test.ts @@ -9,6 +9,7 @@ import { repeat, concat, contains, + copyBytes, } from "./mod.ts"; import { assertEquals, assertThrows, assert } from "../testing/asserts.ts"; import { encode, decode } from "../encoding/utf8.ts"; @@ -117,3 +118,37 @@ Deno.test("[bytes] contain", () => { assert(contains(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3]))); }); + +Deno.test("[io/tuil] copyBytes", function (): void { + const dst = new Uint8Array(4); + + dst.fill(0); + let src = Uint8Array.of(1, 2); + let len = copyBytes(src, dst, 0); + assert(len === 2); + assertEquals(dst, Uint8Array.of(1, 2, 0, 0)); + + dst.fill(0); + src = Uint8Array.of(1, 2); + len = copyBytes(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 = copyBytes(src, dst); + assert(len === 4); + assertEquals(dst, Uint8Array.of(1, 2, 3, 4)); + + dst.fill(0); + src = Uint8Array.of(1, 2); + len = copyBytes(src, dst, 100); + assert(len === 0); + assertEquals(dst, Uint8Array.of(0, 0, 0, 0)); + + dst.fill(0); + src = Uint8Array.of(3, 4); + len = copyBytes(src, dst, -2); + assert(len === 2); + assertEquals(dst, Uint8Array.of(3, 4, 0, 0)); +}); diff --git a/std/io/bufio.ts b/std/io/bufio.ts index e4759dcb7d..cd25736830 100644 --- a/std/io/bufio.ts +++ b/std/io/bufio.ts @@ -6,14 +6,14 @@ type Reader = Deno.Reader; type Writer = Deno.Writer; type WriterSync = Deno.WriterSync; -import { charCode, copyBytes } from "./util.ts"; +import { copyBytes } from "../bytes/mod.ts"; import { assert } from "../_util/assert.ts"; const DEFAULT_BUF_SIZE = 4096; const MIN_BUF_SIZE = 16; const MAX_CONSECUTIVE_EMPTY_READS = 100; -const CR = charCode("\r"); -const LF = charCode("\n"); +const CR = "\r".charCodeAt(0); +const LF = "\n".charCodeAt(0); export class BufferFullError extends Error { name = "BufferFullError"; diff --git a/std/io/bufio_test.ts b/std/io/bufio_test.ts index 2a32ba135a..6bfd2cff90 100644 --- a/std/io/bufio_test.ts +++ b/std/io/bufio_test.ts @@ -16,7 +16,8 @@ import { import * as iotest from "./_iotest.ts"; import { StringReader } from "./readers.ts"; import { StringWriter } from "./writers.ts"; -import { charCode, copyBytes } from "./util.ts"; +import { charCode } from "./util.ts"; +import { copyBytes } from "../bytes/mod.ts"; const encoder = new TextEncoder(); diff --git a/std/io/util.ts b/std/io/util.ts index 22ecb13315..f4b7bf8bb4 100644 --- a/std/io/util.ts +++ b/std/io/util.ts @@ -1,25 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as path from "../path/mod.ts"; -/** - * 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 copyBytes(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; -} - export function charCode(s: string): number { return s.charCodeAt(0); } diff --git a/std/io/util_test.ts b/std/io/util_test.ts index d33a328d67..c602a90d36 100644 --- a/std/io/util_test.ts +++ b/std/io/util_test.ts @@ -1,41 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } from "../testing/asserts.ts"; +import { assert } from "../testing/asserts.ts"; import * as path from "../path/mod.ts"; -import { copyBytes, tempFile } from "./util.ts"; - -Deno.test("[io/tuil] copyBytes", function (): void { - const dst = new Uint8Array(4); - - dst.fill(0); - let src = Uint8Array.of(1, 2); - let len = copyBytes(src, dst, 0); - assert(len === 2); - assertEquals(dst, Uint8Array.of(1, 2, 0, 0)); - - dst.fill(0); - src = Uint8Array.of(1, 2); - len = copyBytes(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 = copyBytes(src, dst); - assert(len === 4); - assertEquals(dst, Uint8Array.of(1, 2, 3, 4)); - - dst.fill(0); - src = Uint8Array.of(1, 2); - len = copyBytes(src, dst, 100); - assert(len === 0); - assertEquals(dst, Uint8Array.of(0, 0, 0, 0)); - - dst.fill(0); - src = Uint8Array.of(3, 4); - len = copyBytes(src, dst, -2); - assert(len === 2); - assertEquals(dst, Uint8Array.of(3, 4, 0, 0)); -}); +import { tempFile } from "./util.ts"; Deno.test({ name: "[io/util] tempfile", diff --git a/std/textproto/mod.ts b/std/textproto/mod.ts index e5645a9ed0..f440ba5d50 100644 --- a/std/textproto/mod.ts +++ b/std/textproto/mod.ts @@ -4,7 +4,6 @@ // license that can be found in the LICENSE file. import { BufReader } from "../io/bufio.ts"; -import { charCode } from "../io/util.ts"; import { concat } from "../bytes/mod.ts"; import { decode } from "../encoding/utf8.ts"; @@ -19,6 +18,10 @@ function str(buf: Uint8Array | null | undefined): string { } } +function charCode(s: string): number { + return s.charCodeAt(0); +} + export class TextProtoReader { constructor(readonly r: BufReader) {}