diff options
author | Chris Knight <cknight1234@gmail.com> | 2020-06-25 11:40:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-25 06:40:51 -0400 |
commit | d9896d64ce919a46a6e8c6666c3b87cc9ae79b7b (patch) | |
tree | d91a28318521361d7c559cb5380051df466b7589 /std/io | |
parent | c98038a03228d527cd90d9f1fc0118a3fe47dce0 (diff) |
refactor: shift copyBytes and tweak deps to reduce dependencies (#6469)
Diffstat (limited to 'std/io')
-rw-r--r-- | std/io/bufio.ts | 6 | ||||
-rw-r--r-- | std/io/bufio_test.ts | 3 | ||||
-rw-r--r-- | std/io/util.ts | 19 | ||||
-rw-r--r-- | std/io/util_test.ts | 38 |
4 files changed, 7 insertions, 59 deletions
diff --git a/std/io/bufio.ts b/std/io/bufio.ts index e4759dcb7..cd2573683 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 2a32ba135..6bfd2cff9 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 22ecb1331..f4b7bf8bb 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 d33a328d6..c602a90d3 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", |