mirror of
https://github.com/denoland/deno.git
synced 2025-01-10 16:11:13 -05:00
refactor: shift copyBytes and tweak deps to reduce dependencies (#6469)
This commit is contained in:
parent
c98038a032
commit
d9896d64ce
8 changed files with 82 additions and 66 deletions
|
@ -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 ]
|
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
|
||||||
|
```
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// 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
|
/** 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
|
* @param pat pattern to find in source array
|
||||||
*/
|
*/
|
||||||
export function findIndex(source: Uint8Array, pat: Uint8Array): number {
|
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.
|
/** 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
|
* @param pat pattern to find in source array
|
||||||
*/
|
*/
|
||||||
export function findLastIndex(source: Uint8Array, pat: Uint8Array): number {
|
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.
|
/** Check whether binary array ends with suffix.
|
||||||
* @param source srouce array
|
* @param source source array
|
||||||
* @param suffix suffix array to check in source
|
* @param suffix suffix array to check in source
|
||||||
*/
|
*/
|
||||||
export function hasSuffix(source: Uint8Array, suffix: Uint8Array): boolean {
|
export function hasSuffix(source: Uint8Array, suffix: Uint8Array): boolean {
|
||||||
|
@ -132,10 +131,29 @@ export function concat(origin: Uint8Array, b: Uint8Array): Uint8Array {
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check srouce array contains pattern array.
|
/** Check source array contains pattern array.
|
||||||
* @param source srouce array
|
* @param source source array
|
||||||
* @param pat patter array
|
* @param pat patter array
|
||||||
*/
|
*/
|
||||||
export function contains(source: Uint8Array, pat: Uint8Array): boolean {
|
export function contains(source: Uint8Array, pat: Uint8Array): boolean {
|
||||||
return findIndex(source, pat) != -1;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import {
|
||||||
repeat,
|
repeat,
|
||||||
concat,
|
concat,
|
||||||
contains,
|
contains,
|
||||||
|
copyBytes,
|
||||||
} from "./mod.ts";
|
} from "./mod.ts";
|
||||||
import { assertEquals, assertThrows, assert } from "../testing/asserts.ts";
|
import { assertEquals, assertThrows, assert } from "../testing/asserts.ts";
|
||||||
import { encode, decode } from "../encoding/utf8.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])));
|
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));
|
||||||
|
});
|
||||||
|
|
|
@ -6,14 +6,14 @@
|
||||||
type Reader = Deno.Reader;
|
type Reader = Deno.Reader;
|
||||||
type Writer = Deno.Writer;
|
type Writer = Deno.Writer;
|
||||||
type WriterSync = Deno.WriterSync;
|
type WriterSync = Deno.WriterSync;
|
||||||
import { charCode, copyBytes } from "./util.ts";
|
import { copyBytes } from "../bytes/mod.ts";
|
||||||
import { assert } from "../_util/assert.ts";
|
import { assert } from "../_util/assert.ts";
|
||||||
|
|
||||||
const DEFAULT_BUF_SIZE = 4096;
|
const DEFAULT_BUF_SIZE = 4096;
|
||||||
const MIN_BUF_SIZE = 16;
|
const MIN_BUF_SIZE = 16;
|
||||||
const MAX_CONSECUTIVE_EMPTY_READS = 100;
|
const MAX_CONSECUTIVE_EMPTY_READS = 100;
|
||||||
const CR = charCode("\r");
|
const CR = "\r".charCodeAt(0);
|
||||||
const LF = charCode("\n");
|
const LF = "\n".charCodeAt(0);
|
||||||
|
|
||||||
export class BufferFullError extends Error {
|
export class BufferFullError extends Error {
|
||||||
name = "BufferFullError";
|
name = "BufferFullError";
|
||||||
|
|
|
@ -16,7 +16,8 @@ import {
|
||||||
import * as iotest from "./_iotest.ts";
|
import * as iotest from "./_iotest.ts";
|
||||||
import { StringReader } from "./readers.ts";
|
import { StringReader } from "./readers.ts";
|
||||||
import { StringWriter } from "./writers.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();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,6 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
import * as path from "../path/mod.ts";
|
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 {
|
export function charCode(s: string): number {
|
||||||
return s.charCodeAt(0);
|
return s.charCodeAt(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// 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 * as path from "../path/mod.ts";
|
||||||
import { copyBytes, tempFile } from "./util.ts";
|
import { 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));
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "[io/util] tempfile",
|
name: "[io/util] tempfile",
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
import { BufReader } from "../io/bufio.ts";
|
import { BufReader } from "../io/bufio.ts";
|
||||||
import { charCode } from "../io/util.ts";
|
|
||||||
import { concat } from "../bytes/mod.ts";
|
import { concat } from "../bytes/mod.ts";
|
||||||
import { decode } from "../encoding/utf8.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 {
|
export class TextProtoReader {
|
||||||
constructor(readonly r: BufReader) {}
|
constructor(readonly r: BufReader) {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue