2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-11-29 13:55:38 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
|
|
|
export const DEFAULT_CHUNK_SIZE = 16_640;
|
|
|
|
export const DEFAULT_BUFFER_SIZE = 32 * 1024;
|
|
|
|
|
|
|
|
/** Generate longest proper prefix which is also suffix array. */
|
|
|
|
export function createLPS(pat: Uint8Array): Uint8Array {
|
2023-08-31 10:40:32 +00:00
|
|
|
const length = pat.length;
|
|
|
|
const lps = new Uint8Array(length);
|
2022-11-29 13:55:38 +00:00
|
|
|
lps[0] = 0;
|
|
|
|
let prefixEnd = 0;
|
|
|
|
let i = 1;
|
2023-08-31 10:40:32 +00:00
|
|
|
while (i < length) {
|
2023-08-25 09:04:43 +00:00
|
|
|
if (pat[i] === pat[prefixEnd]) {
|
2022-11-29 13:55:38 +00:00
|
|
|
prefixEnd++;
|
|
|
|
lps[i] = prefixEnd;
|
|
|
|
i++;
|
|
|
|
} else if (prefixEnd === 0) {
|
|
|
|
lps[i] = 0;
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
prefixEnd = lps[prefixEnd - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lps;
|
|
|
|
}
|