summaryrefslogtreecommitdiff
path: root/bytes
diff options
context:
space:
mode:
authorAxetroy <troy450409405@gmail.com>2019-05-24 19:44:13 +0800
committerRyan Dahl <ry@tinyclouds.org>2019-05-24 14:44:13 +0300
commit4ab0e0e9187c77244ddff11c389e0b39bda8fe4d (patch)
tree1dbba449a27b1c6247ee99dc53e15b0a8fe634eb /bytes
parent0803912c7fe5f034914e3c63347d5b6a4d2f23c8 (diff)
Enable bytes tests and add bytesRepeat (denoland/deno_std#446)
Original: https://github.com/denoland/deno_std/commit/bd46d60ded3197d93a52ede92eba7302df9b4713
Diffstat (limited to 'bytes')
-rw-r--r--bytes/bytes.ts34
-rw-r--r--bytes/bytes_test.ts43
-rw-r--r--bytes/test.ts2
3 files changed, 77 insertions, 2 deletions
diff --git a/bytes/bytes.ts b/bytes/bytes.ts
index a79a9ed56..127dad1c2 100644
--- a/bytes/bytes.ts
+++ b/bytes/bytes.ts
@@ -1,4 +1,5 @@
// Copyright 2018-2019 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 **/
export function bytesFindIndex(a: Uint8Array, pat: Uint8Array): number {
@@ -60,3 +61,36 @@ export function bytesHasPrefix(a: Uint8Array, prefix: Uint8Array): boolean {
}
return true;
}
+
+/**
+ * Repeat bytes. returns a new byte slice consisting of `count` copies of `b`.
+ * @param b The origin bytes
+ * @param count The count you want to repeat.
+ */
+export function bytesRepeat(b: Uint8Array, count: number): Uint8Array {
+ if (count === 0) {
+ return new Uint8Array();
+ }
+
+ if (count < 0) {
+ throw new Error("bytes: negative repeat count");
+ } else if ((b.length * count) / count !== b.length) {
+ throw new Error("bytes: repeat count causes overflow");
+ }
+
+ const int = Math.floor(count);
+
+ if (int !== count) {
+ throw new Error("bytes: repeat count must be an integer");
+ }
+
+ const nb = new Uint8Array(b.length * count);
+
+ let bp = copyBytes(nb, b);
+
+ for (; bp < nb.length; bp *= 2) {
+ copyBytes(nb, nb.slice(0, bp), bp);
+ }
+
+ return nb;
+}
diff --git a/bytes/bytes_test.ts b/bytes/bytes_test.ts
index 9d4754a39..8ca4cb386 100644
--- a/bytes/bytes_test.ts
+++ b/bytes/bytes_test.ts
@@ -1,11 +1,14 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+
import {
bytesFindIndex,
bytesFindLastIndex,
bytesEqual,
- bytesHasPrefix
+ bytesHasPrefix,
+ bytesRepeat
} from "./bytes.ts";
import { test } from "../testing/mod.ts";
-import { assertEquals } from "../testing/asserts.ts";
+import { assertEquals, assertThrows } from "../testing/asserts.ts";
test(function bytesBytesFindIndex1(): void {
const i = bytesFindIndex(
@@ -48,3 +51,39 @@ test(function bytesBytesHasPrefix(): void {
const v = bytesHasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
assertEquals(v, true);
});
+
+test(function bytesBytesRepeat(): void {
+ // input / output / count / error message
+ const repeatTestCase = [
+ ["", "", 0],
+ ["", "", 1],
+ ["", "", 1.1, "bytes: repeat count must be an integer"],
+ ["", "", 2],
+ ["", "", 0],
+ ["-", "", 0],
+ ["-", "-", -1, "bytes: negative repeat count"],
+ ["-", "----------", 10],
+ ["abc ", "abc abc abc ", 3]
+ ];
+ for (const [input, output, count, errMsg] of repeatTestCase) {
+ if (errMsg) {
+ assertThrows(
+ (): void => {
+ bytesRepeat(
+ new TextEncoder().encode(input as string),
+ count as number
+ );
+ },
+ Error,
+ errMsg as string
+ );
+ } else {
+ const newBytes = bytesRepeat(
+ new TextEncoder().encode(input as string),
+ count as number
+ );
+
+ assertEquals(new TextDecoder().decode(newBytes), output);
+ }
+ }
+});
diff --git a/bytes/test.ts b/bytes/test.ts
new file mode 100644
index 000000000..711bf62c4
--- /dev/null
+++ b/bytes/test.ts
@@ -0,0 +1,2 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import "./bytes_test.ts";