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

/**
 * Concatenate an array of byte slices into a single slice.
 *
 * @param buffers Array of byte slices to concatenate.
 * @returns A new byte slice containing all the input slices concatenated.
 *
 * @example Basic usage
 * ```ts
 * import { concat } from "@std/bytes/concat";
 * import { assertEquals } from "@std/assert";
 *
 * const a = new Uint8Array([0, 1, 2]);
 * const b = new Uint8Array([3, 4, 5]);
 *
 * assertEquals(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
 * ```
 */
export function concat(buffers: Uint8Array[]): Uint8Array {
  let length = 0;
  for (const buffer of buffers) {
    length += buffer.length;
  }
  const output = new Uint8Array(length);
  let index = 0;
  for (const buffer of buffers) {
    output.set(buffer, index);
    index += buffer.length;
  }

  return output;
}

// denoCacheMetadata={"headers":{"access-control-allow-origin":"*","content-type":"text/typescript","cf-ray":"a223e658cd727db6-LAX","server":"cloudflare","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';","cross-origin-resource-policy":"cross-origin","x-content-type-options":"nosniff","x-jsr-backend":"modules","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=GNE8KWpMsmUymDqDmzDRgey3orhKbbkWGEe1VSwyPcpcM%2FzTEdWQmfl1dTyWvrGR1r%2F1Caz5dAi7YaLjKS8olcLR35oPPZr2jyYiguS2KFBfdKK6t%2BhZzFA%3D\"}]}","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","cf-cache-status":"HIT","set-cookie":"__cf_bm=_WC.UWUOctSbIl5Yvrv71rkCJdt513xmcW.wRmxf6Ho-1785241875.3277743-1.0.1.1-3DB2fbQT4D2PiIbFDMXBeyuje4SCxf478mr.Lg0DECVkJigl2zLry.AdA3sdZjKi4GOTU8wchPKQHQKgfyeqtAkg0owwj3qg0kgfEQ7LucLjoIYySvp8Tn8tq5a8d6li; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","etag":"\"1d3a187229fc7062c480800c62d838fd\"","alt-svc":"h3=\":443\"; ma=86400","accept-ranges":"bytes","age":"698545","content-length":"927","vary":"Origin","x-robots-tag":"noindex","cache-control":"public, max-age=31536000, immutable","access-control-expose-headers":"*","date":"Tue, 28 Jul 2026 12:31:15 GMT"},"url":"https://jsr.io/@std/bytes/1.0.2/concat.ts","time":1785241875}