Skip to main content
Module

std/bytes/copy.ts>copy

The Deno Standard Library
Latest
function copy
import { copy } from "https://deno.land/std@0.224.0/bytes/copy.ts";

Copy bytes from the source array to the destination array and returns the number of bytes copied.

If the source array is larger than what the dst array can hold, only the amount of bytes that fit in the dst array are copied.

Examples

Basic usage

import { copy } from "https://deno.land/std@0.224.0/bytes/copy.ts";

const src = new Uint8Array([9, 8, 7]);
const dst = new Uint8Array([0, 1, 2, 3, 4, 5]);

copy(src, dst); // 3
dst; // Uint8Array(6) [9, 8, 7, 3, 4, 5]

Copy with offset

import { copy } from "https://deno.land/std@0.224.0/bytes/copy.ts";

const src = new Uint8Array([1, 1, 1, 1]);
const dst = new Uint8Array([0, 0, 0, 0]);

copy(src, dst, 1); // 3
dst; // Uint8Array(4) [0, 1, 1, 1]

Defining an offset will start copying at the specified index in the destination array.

Parameters

src: Uint8Array

Source array to copy from.

dst: Uint8Array

Destination array to copy to.

optional
offset = [UNSUPPORTED]

Offset in the destination array to start copying to. Defaults to 0.

Returns

number

Number of bytes copied.