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

/**
 * Returns `true` if the suffix array appears at the end of the source array,
 * `false` otherwise.
 *
 * The complexity of this function is `O(suffix.length)`.
 *
 * @param source Source array to check.
 * @param suffix Suffix array to check for.
 * @returns `true` if the suffix array appears at the end of the source array,
 * `false` otherwise.
 *
 * @example Basic usage
 * ```ts
 * import { endsWith } from "@std/bytes/ends-with";
 *
 * const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
 * const suffix = new Uint8Array([1, 2, 3]);
 *
 * endsWith(source, suffix); // true
 * ```
 */
export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean {
  const diff = source.length - suffix.length;
  for (let i = suffix.length - 1; i >= 0; i--) {
    if (source[diff + i] !== suffix[i]) {
      return false;
    }
  }
  return true;
}

// denoCacheMetadata={"headers":{"access-control-allow-origin":"*","cf-cache-status":"HIT","accept-ranges":"bytes","date":"Tue, 28 Jul 2026 12:31:14 GMT","age":"2812499","cache-control":"public, max-age=31536000, immutable","cf-ray":"a223e6564eca7db6-LAX","server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400","x-content-type-options":"nosniff","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=7H3O1YzsS3m0lUeXW6b31J5%2BoytqVFfv0RTFJroeMbJahShei5LP1DkoOUqZmIO%2BnTrmy%2BTqFpxvs3iWrwP1ZqTjyIN0JO7uAay8AJr8lW616B7UP%2FqhZlM%3D\"}]}","access-control-expose-headers":"*","content-type":"text/typescript","cross-origin-resource-policy":"cross-origin","x-robots-tag":"noindex","content-length":"974","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';","set-cookie":"__cf_bm=V.5w9CE_SLyW3xp0CevU5JooWv7073Prhkvl6rmYU6w-1785241874.945504-1.0.1.1-aNZ2_wyP2NgkjVqw9jtlFguQ8BhmDlgcw9Loq.Q7PNbVOJFUUV9glNS0CjKW_veCGzh.1nOoVO0Pq4S7evWbwiO1VNA62FBWjq8myXIUABDqbGSGSV.8fcsawbmEbWx9; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:14 GMT","vary":"Origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","x-jsr-backend":"modules","etag":"\"2e8ae0d58a9d0dff3179c67ffed058e1\""},"url":"https://jsr.io/@std/bytes/0.224.0/ends_with.ts","time":1785241874}