fix: keep the Journal folder pattern linear on a run of separators (#514) #538
2 changed files with 68 additions and 2 deletions
|
|
@ -140,6 +140,62 @@ describe("replaceKnownFolders (#497)", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("replaceKnownFolders on a long run of separators (#514)", () => {
|
||||
// A folder that starts with a separator (Linux, UNC) used to give a pattern
|
||||
// that starts with a greedy `[\\/]+`: on a run of separators that does not
|
||||
// lead to the folder, every position of the run started it again, so the
|
||||
// time grew with the square of the run (23 s for 100 000 separators).
|
||||
it("writes an entry of 100 000 `/` in less than a second, unchanged", () => {
|
||||
const text = "/".repeat(100_000);
|
||||
const start = performance.now();
|
||||
const out = replaceKnownFolders(text, LINUX);
|
||||
const elapsed = performance.now() - start;
|
||||
expect(out).toBe(text);
|
||||
expect(elapsed).toBeLessThan(1_000);
|
||||
});
|
||||
|
||||
it("does the same with a run of mixed `/` and `\\`", () => {
|
||||
const text = "/\\".repeat(50_000);
|
||||
const start = performance.now();
|
||||
const out = replaceKnownFolders(text, LINUX);
|
||||
const elapsed = performance.now() - start;
|
||||
expect(out).toBe(text);
|
||||
expect(elapsed).toBeLessThan(1_000);
|
||||
});
|
||||
|
||||
it("does the same with a run of `\\` and a UNC folder", () => {
|
||||
const text = "\\".repeat(100_000);
|
||||
const start = performance.now();
|
||||
const out = replaceKnownFolders(text, { home: "\\\\server\\share\\max" });
|
||||
const elapsed = performance.now() - start;
|
||||
expect(out).toBe(text);
|
||||
expect(elapsed).toBeLessThan(1_000);
|
||||
});
|
||||
|
||||
it.each([
|
||||
// A match inside a run of separators starts at its first separator.
|
||||
["//home/max/x", "~/x"],
|
||||
["a/home/max", "a~"],
|
||||
["/home/max//home/max", "~~"],
|
||||
// Separators doubled by JSON.stringify.
|
||||
["\\\\home\\\\max", "~"],
|
||||
["x//home/max", "x~"],
|
||||
["/\\/home/max", "~"],
|
||||
// The profile folder right after a run of separators.
|
||||
["x///home/max/.config/com.simpl.resultat/a.db", "x<profile folder>/a.db"],
|
||||
])("replaces %j with %j, as before", (text, expected) => {
|
||||
expect(replaceKnownFolders(text, LINUX)).toBe(expected);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["open \\\\server\\share\\max\\a.csv", "open ~\\a.csv"],
|
||||
["open \\\\\\\\server\\\\share\\\\max\\\\a.csv", "open ~\\\\a.csv"],
|
||||
["open \\\\server\\share\\maxime\\a.csv", "open \\\\server\\share\\maxime\\a.csv"],
|
||||
])("replaces a UNC home folder in %j", (text, expected) => {
|
||||
expect(replaceKnownFolders(text, { home: "\\\\server\\share\\max" })).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setKnownFolders (#497)", () => {
|
||||
afterEach(() => {
|
||||
setKnownFolders({});
|
||||
|
|
|
|||
|
|
@ -59,17 +59,27 @@ function escapeSegment(segment: string): string {
|
|||
* end is dropped. Its segments are joined by any run of `\` and `/`: the two
|
||||
* separators are interchangeable, and `JSON.stringify`, which logService
|
||||
* applies to an argument that is not a string, doubles every `\`. The case is
|
||||
* ignored when the folder starts with a drive letter (Windows).
|
||||
* ignored when the folder starts with a drive letter (Windows). A folder that
|
||||
* starts with a separator (Linux, UNC) may not start a match right after
|
||||
* another separator (#514).
|
||||
*/
|
||||
function folderPattern(folder: unknown, replacement: string): FolderPattern | null {
|
||||
if (typeof folder !== "string") return null;
|
||||
const segments = folder.replace(/[\\/]+$/, "").split(/[\\/]+/);
|
||||
const drive = /^[A-Za-z]:$/.test(segments[0]);
|
||||
if (!segments.some((segment, i) => segment !== "" && !(i === 0 && drive))) return null;
|
||||
// A folder that starts with a separator gives a pattern that starts with the
|
||||
// greedy `[\\/]+`. On a run of separators that does not lead to the folder,
|
||||
// every position of the run started that class again and backtracked: the
|
||||
// time grew with the square of the run (#514). The search goes from the
|
||||
// left, so a match inside a run always starts at its first separator, which
|
||||
// the class swallows; forbidding a separator right before the start removes
|
||||
// no match and keeps the time linear.
|
||||
const start = segments[0] === "" ? "(?<![\\\\/])" : "";
|
||||
try {
|
||||
return {
|
||||
pattern: new RegExp(
|
||||
segments.map(escapeSegment).join("[\\\\/]+") + `(?!${NAME_GOES_ON})`,
|
||||
start + segments.map(escapeSegment).join("[\\\\/]+") + `(?!${NAME_GOES_ON})`,
|
||||
drive ? "giu" : "gu"
|
||||
),
|
||||
replacement,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue