summaryrefslogtreecommitdiff
path: root/std/bytes
diff options
context:
space:
mode:
authorChris Knight <cknight1234@gmail.com>2020-06-25 11:40:51 +0100
committerGitHub <noreply@github.com>2020-06-25 06:40:51 -0400
commitd9896d64ce919a46a6e8c6666c3b87cc9ae79b7b (patch)
treed91a28318521361d7c559cb5380051df466b7589 /std/bytes
parentc98038a03228d527cd90d9f1fc0118a3fe47dce0 (diff)
refactor: shift copyBytes and tweak deps to reduce dependencies (#6469)
Diffstat (limited to 'std/bytes')
-rw-r--r--std/bytes/README.md12
-rw-r--r--std/bytes/mod.ts30
-rw-r--r--std/bytes/test.ts35
3 files changed, 71 insertions, 6 deletions
diff --git a/std/bytes/README.md b/std/bytes/README.md
index bf0fcc5b0..77f3fdfc8 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 e52e2365b..8ae697c29 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 8b03de926..25af2a35b 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));
+});