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/bytes/mod.ts | |
parent | c98038a03228d527cd90d9f1fc0118a3fe47dce0 (diff) |
refactor: shift copyBytes and tweak deps to reduce dependencies (#6469)
Diffstat (limited to 'std/bytes/mod.ts')
-rw-r--r-- | std/bytes/mod.ts | 30 |
1 files changed, 24 insertions, 6 deletions
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; +} |