// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
 * 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.
 *
 * @param src Source array to copy from.
 * @param dst Destination array to copy to.
 * @param offset Offset in the destination array to start copying to. Defaults
 * to 0.
 * @returns Number of bytes copied.
 *
 * @example Basic usage
 * ```ts
 * import { copy } from "@std/bytes/copy";
 *
 * 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]
 * ```
 *
 * @example Copy with offset
 * ```ts
 * import { copy } from "@std/bytes/copy";
 *
 * 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.
 */
export function copy(src: Uint8Array, dst: Uint8Array, offset = 0): number {
  offset = Math.max(0, Math.min(offset, dst.byteLength));
  const dstBytesAvailable = dst.byteLength - offset;
  if (src.byteLength > dstBytesAvailable) {
    src = src.subarray(0, dstBytesAvailable);
  }
  dst.set(src, offset);
  return src.byteLength;
}

// denoCacheMetadata={"headers":{"x-jsr-backend":"modules","access-control-allow-origin":"*","set-cookie":"__cf_bm=nWxdZfe2ZWbxQHAw3QJCz0XXmCIF0XqBueR_b5R1oP0-1785241874.9447796-1.0.1.1-hjdFHpyvv0VtXGkwFv9VtofRY9JpsX.5vk.uT.EIR2CT0DOHUDIPrQwEEvNQum8c8Jn4KYj44IOU0oETGl1Z285F22_DU0VO_mwHXmziTFsUU12U5AZfqVxqnZa6xcMB; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:14 GMT","age":"3876923","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","date":"Tue, 28 Jul 2026 12:31:14 GMT","cache-control":"public, max-age=31536000, immutable","access-control-expose-headers":"*","alt-svc":"h3=\":443\"; ma=86400","cf-ray":"a223e6564ec97db6-LAX","etag":"\"c3fee939fd0720185243db9f45f826b5\"","x-content-type-options":"nosniff","server":"cloudflare","cross-origin-resource-policy":"cross-origin","cf-cache-status":"HIT","content-length":"1509","content-type":"text/typescript","accept-ranges":"bytes","vary":"Origin","x-robots-tag":"noindex","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=%2F%2BqFxNrMt3ksExVIqf%2BiXmVCnzLqojou1CZIxD%2BFqYUOYSz92c24yVn3ZGJ6LugeG3%2BKYAuvZ6a8ElYT4qzSfkYW8GJlHh9McS5f%2BwpxzhOTH2x3uNhM76w%3D\"}]}"},"url":"https://jsr.io/@std/bytes/0.224.0/copy.ts","time":1785241874}