@criogaid/pi-hashline-edit 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { applyReplacements, type Replacement } from "./replace.ts";
|
|
4
|
+
|
|
5
|
+
test("replacement joins beside LF → preserve standalone CR content", async (t) => {
|
|
6
|
+
const literal = (find: string, replace: string): Replacement => ({
|
|
7
|
+
find,
|
|
8
|
+
replace,
|
|
9
|
+
literal: true,
|
|
10
|
+
});
|
|
11
|
+
const cases = [
|
|
12
|
+
{
|
|
13
|
+
name: "replacement before original LF",
|
|
14
|
+
source: "a\nb",
|
|
15
|
+
rules: [literal("a", "x\r")],
|
|
16
|
+
expected: "x\r\r\nb",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: "original CR before replacement LF",
|
|
20
|
+
source: "a\rb",
|
|
21
|
+
rules: [literal("b", "\nB")],
|
|
22
|
+
expected: "a\r\r\nB",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "deletion joins original CR and LF",
|
|
26
|
+
source: "a\rx\nb",
|
|
27
|
+
rules: [literal("x", "")],
|
|
28
|
+
expected: "a\r\r\nb",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: "replacement contains CR before a new LF",
|
|
32
|
+
source: "a\nb",
|
|
33
|
+
rules: [literal("a", "x\r\r\ny")],
|
|
34
|
+
expected: "x\r\r\ny\nb",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "replacement contains CR before a reused LF",
|
|
38
|
+
source: "a\nb",
|
|
39
|
+
rules: [literal("a\nb", "x\r\r\ny")],
|
|
40
|
+
expected: "x\r\r\ny",
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "captured CR before replacement LF",
|
|
44
|
+
source: "a\rb\nc",
|
|
45
|
+
rules: [{ find: "(a\r)b", replace: "$1\nB", literal: false }],
|
|
46
|
+
expected: "a\r\r\nB\nc",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: "adjacent replacements",
|
|
50
|
+
source: "ab",
|
|
51
|
+
rules: [literal("a", "x\r"), literal("b", "\ny")],
|
|
52
|
+
expected: "x\r\r\ny",
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: "adjacent replacements separated by a deletion",
|
|
56
|
+
source: "axb",
|
|
57
|
+
rules: [literal("a", "x\r"), literal("x", ""), literal("b", "\ny")],
|
|
58
|
+
expected: "x\r\r\ny",
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: "zero-width insertion before LF",
|
|
62
|
+
source: "a\nb",
|
|
63
|
+
rules: [{ find: "(?=\\n)", replace: "\r", literal: false }],
|
|
64
|
+
expected: "a\r\r\nb",
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: "replacement before original CRLF",
|
|
68
|
+
source: "a\r\nb",
|
|
69
|
+
rules: [literal("a", "x\r")],
|
|
70
|
+
expected: "x\r\r\nb",
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: "unchanged CR content and CRLF separator",
|
|
74
|
+
source: "a\r\r\nb",
|
|
75
|
+
rules: [literal("a\r\r\nb", "a\r\r\nb")],
|
|
76
|
+
expected: "a\r\r\nb",
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: "CR at EOF needs no separator",
|
|
80
|
+
source: "ab",
|
|
81
|
+
rules: [literal("b", "x\r")],
|
|
82
|
+
expected: "ax\r",
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
for (const { name, source, rules, expected } of cases) {
|
|
86
|
+
await t.test(name, () => {
|
|
87
|
+
assert.equal(applyReplacements(source, rules).text, expected);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
package/src/core/replace.ts
CHANGED
|
@@ -1,37 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Replacement engine: apply literal or regex rules (built by regex.ts) over the
|
|
3
|
+
* shared LF view, expand JavaScript replacement tokens, reject rules that match
|
|
4
|
+
* nothing or overlap, and map each change back to the original text with its
|
|
5
|
+
* line endings restored.
|
|
6
|
+
*
|
|
7
|
+
* @module pi-hashline-edit/core
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { buildRegex } from "./regex.ts";
|
|
2
11
|
import {
|
|
3
12
|
createLfTextView,
|
|
4
13
|
detectLineEnding,
|
|
14
|
+
joinTextFragments,
|
|
5
15
|
normalizeLineEndings,
|
|
6
16
|
restoreLineEndings,
|
|
7
17
|
} from "./lines.ts";
|
|
8
18
|
import { findSortedRangeConflict } from "./ranges.ts";
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
export interface Replacement {
|
|
12
|
-
find: string;
|
|
13
|
-
replace: string;
|
|
14
|
-
regex?: boolean;
|
|
15
|
-
flags?: string;
|
|
16
|
-
}
|
|
19
|
+
import { HashlineError } from "./errors.ts";
|
|
17
20
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* schema; replace's prepareArguments also compiles regex rules here before any file is read.
|
|
21
|
-
*/
|
|
22
|
-
export function buildRegex(find: string, isRegex: boolean, flagsRaw: string | undefined): RegExp {
|
|
23
|
-
const set = new Set((flagsRaw ?? "").split(""));
|
|
24
|
-
set.add("g");
|
|
25
|
-
const flagStr = [...set].join("");
|
|
26
|
-
const logical = normalizeLineEndings(find);
|
|
27
|
-
const source = isRegex ? logical : escapeRegex(logical);
|
|
28
|
-
try {
|
|
29
|
-
return new RegExp(source, flagStr);
|
|
30
|
-
} catch (e) {
|
|
31
|
-
const msg = errorMessage(e);
|
|
32
|
-
throw new Error(`invalid regex /${source}/${flagStr}: ${msg}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
21
|
+
import type { Replacement, ReplacementResult } from "./replace-schema.ts";
|
|
22
|
+
export type { Replacement } from "./replace-schema.ts";
|
|
35
23
|
|
|
36
24
|
/** Expand JS replacement tokens against the original match, including prefix/suffix context. */
|
|
37
25
|
function expandReplacement(template: string, match: RegExpMatchArray, source: string): string {
|
|
@@ -57,46 +45,42 @@ function expandReplacement(template: string, match: RegExpMatchArray, source: st
|
|
|
57
45
|
export function applyReplacements(
|
|
58
46
|
source: string,
|
|
59
47
|
rules: readonly Replacement[],
|
|
60
|
-
):
|
|
48
|
+
): ReplacementResult {
|
|
61
49
|
const changes: { start: number; end: number; text: string; rule: number }[] = [];
|
|
62
50
|
const view = createLfTextView(source);
|
|
63
51
|
const fallbackEnding = detectLineEnding(source) === "crlf" ? "\r\n" : "\n";
|
|
64
52
|
for (const [index, rule] of rules.entries()) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
if (count === 0)
|
|
85
|
-
throw new Error(
|
|
86
|
-
`no matches for ${rule.regex ? `/${rule.find}/` : JSON.stringify(rule.find)}. Verify the target text with read or grep; check case sensitivity or regex flags if applicable.`,
|
|
87
|
-
);
|
|
88
|
-
} catch (error) {
|
|
89
|
-
throw new Error(`rule ${index}: ${errorMessage(error)}`);
|
|
53
|
+
const regex = buildRegex(rule.find, rule.literal, rule.flags);
|
|
54
|
+
let count = 0;
|
|
55
|
+
const replacement = normalizeLineEndings(rule.replace);
|
|
56
|
+
for (const match of view.text.matchAll(regex)) {
|
|
57
|
+
count++;
|
|
58
|
+
const start = view.sourceOffset(match.index!);
|
|
59
|
+
const end = view.sourceOffset(match.index! + match[0].length);
|
|
60
|
+
changes.push({
|
|
61
|
+
start,
|
|
62
|
+
end,
|
|
63
|
+
rule: index,
|
|
64
|
+
text: restoreLineEndings(
|
|
65
|
+
rule.literal ? replacement : expandReplacement(replacement, match, view.text),
|
|
66
|
+
source.slice(start, end),
|
|
67
|
+
fallbackEnding,
|
|
68
|
+
),
|
|
69
|
+
});
|
|
90
70
|
}
|
|
71
|
+
if (count === 0)
|
|
72
|
+
throw new HashlineError("NO_MATCH", "The rule matches nothing in the file.", {
|
|
73
|
+
facts: { rule: index },
|
|
74
|
+
});
|
|
91
75
|
}
|
|
92
76
|
changes.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
93
77
|
const conflict = findSortedRangeConflict(changes.map((change) => [change.start, change.end]));
|
|
94
78
|
if (conflict !== undefined) {
|
|
95
79
|
const previous = changes[conflict - 1];
|
|
96
80
|
const current = changes[conflict];
|
|
97
|
-
throw new
|
|
98
|
-
|
|
99
|
-
);
|
|
81
|
+
throw new HashlineError("OVERLAPPING_MATCHES", "Matches of the rules overlap.", {
|
|
82
|
+
facts: { rules: [previous.rule, current.rule], offset: current.start },
|
|
83
|
+
});
|
|
100
84
|
}
|
|
101
85
|
const parts: string[] = [];
|
|
102
86
|
let cursor = 0;
|
|
@@ -105,5 +89,5 @@ export function applyReplacements(
|
|
|
105
89
|
cursor = change.end;
|
|
106
90
|
}
|
|
107
91
|
parts.push(source.slice(cursor));
|
|
108
|
-
return { text: parts
|
|
92
|
+
return { text: joinTextFragments(parts), count: changes.length };
|
|
109
93
|
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { extractPrefilter, smartIgnoreCase, type Literal } from "./search-analysis.ts";
|
|
4
|
+
import { searchText, type GrepQuery } from "./search.ts";
|
|
5
|
+
import { hasCandidateBytes } from "./search-matrix.testing.ts";
|
|
6
|
+
|
|
7
|
+
const query = (pattern: string, flags = "m", literal = false): GrepQuery => ({
|
|
8
|
+
patterns: [pattern],
|
|
9
|
+
flags,
|
|
10
|
+
literal,
|
|
11
|
+
multiline: true,
|
|
12
|
+
});
|
|
13
|
+
const literal = (text: string): Literal => [...text].map((char) => [char]);
|
|
14
|
+
|
|
15
|
+
for (const [pattern, flags, expected] of [
|
|
16
|
+
["colou?r", "m", [literal("color"), literal("colour")]],
|
|
17
|
+
["a*", "m", undefined],
|
|
18
|
+
["(?:word|)", "m", undefined],
|
|
19
|
+
["[^a]+", "m", undefined],
|
|
20
|
+
["(?=word)", "m", [literal("word")]],
|
|
21
|
+
["(?i:a)(?-i:b)", "m", [[["A", "a"], ["b"]]]],
|
|
22
|
+
["(?-i:a)(?i:b)", "mi", [[["a"], ["B", "b"]]]],
|
|
23
|
+
["k", "mi", [[["K", "k"]]]],
|
|
24
|
+
["s", "mi", [[["S", "s"]]]],
|
|
25
|
+
["k", "miu", [[["K", "k", "K"]]]],
|
|
26
|
+
["s", "miv", [[["S", "s", "ſ"]]]],
|
|
27
|
+
[
|
|
28
|
+
"abéz",
|
|
29
|
+
"miu",
|
|
30
|
+
[
|
|
31
|
+
[
|
|
32
|
+
["A", "a"],
|
|
33
|
+
["B", "b"],
|
|
34
|
+
["É", "é"],
|
|
35
|
+
["Z", "z"],
|
|
36
|
+
],
|
|
37
|
+
],
|
|
38
|
+
],
|
|
39
|
+
["é", "miu", [[["É", "é"]]]],
|
|
40
|
+
["[\\u0100-\\u0200]", "m", undefined],
|
|
41
|
+
] as const) {
|
|
42
|
+
test(`${pattern.slice(0, 80)} /${flags} → conservative candidate literals`, () => {
|
|
43
|
+
assert.deepEqual(
|
|
44
|
+
extractPrefilter(query(pattern, flags)),
|
|
45
|
+
expected ? { kind: "literals", literals: expected } : { kind: "all" },
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const boundary of ["\n", "\r\n", "\uFFFD", "\uD800", "\uDC00", "\0"]) {
|
|
51
|
+
test(`literal ${JSON.stringify(boundary)} boundary → no candidate spans changed bytes`, () => {
|
|
52
|
+
const prefilter = extractPrefilter(query(`left${boundary}xy`, "m", true));
|
|
53
|
+
assert.ok(hasCandidateBytes(Buffer.from(`left${boundary}xy`), prefilter));
|
|
54
|
+
assert.ok(hasCandidateBytes(Buffer.from("left\nxy"), prefilter));
|
|
55
|
+
assert.equal(hasCandidateBytes(Buffer.from("left"), prefilter), false);
|
|
56
|
+
assert.equal(hasCandidateBytes(Buffer.from("xy"), prefilter), false);
|
|
57
|
+
assert.deepEqual(extractPrefilter(query(boundary, "m", true)), { kind: "all" });
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
for (const [patterns, isLiteral, expected, flags] of [
|
|
62
|
+
[["foo", "bar"], false, true],
|
|
63
|
+
[["foo", "Bar"], false, false],
|
|
64
|
+
[["foo\\S*", "\\D", "\\W"], false, true],
|
|
65
|
+
[["\\u0041"], false, false],
|
|
66
|
+
[["\\x41"], false, false],
|
|
67
|
+
[["\\u0061"], false, true],
|
|
68
|
+
[["[A-Z]"], false, false],
|
|
69
|
+
[["(?<UPPER>foo)"], false, true],
|
|
70
|
+
[["ς", "σ"], false, true],
|
|
71
|
+
[["σ", "Σ"], false, false],
|
|
72
|
+
[["foo\\S*"], true, false],
|
|
73
|
+
[["\\u0041"], true, true],
|
|
74
|
+
[["foo", "BAR"], true, false],
|
|
75
|
+
[["\\u{41}"], false, false, "u"],
|
|
76
|
+
[["\\u{41}"], false, false, "v"],
|
|
77
|
+
[["\\u{61}"], false, true, "u"],
|
|
78
|
+
[["\\u{61}"], false, true, "v"],
|
|
79
|
+
[["𐐀"], false, false, "u"],
|
|
80
|
+
[["𐐀"], false, false, "v"],
|
|
81
|
+
[["𐐨"], false, true, "u"],
|
|
82
|
+
[["𐐨"], false, true, "v"],
|
|
83
|
+
] as const) {
|
|
84
|
+
test(`${JSON.stringify(patterns)} literal=${isLiteral} flags=${flags ?? ""} → smart case follows decoded letters`, () => {
|
|
85
|
+
assert.equal(smartIgnoreCase(patterns, isLiteral, flags), expected);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
test("seeded random patterns × raw bytes → a JS hit always retains a candidate byte literal", () => {
|
|
90
|
+
let seed = 0x5eed1234;
|
|
91
|
+
const next = (bound: number) => {
|
|
92
|
+
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
|
|
93
|
+
return seed % bound;
|
|
94
|
+
};
|
|
95
|
+
const atoms = [
|
|
96
|
+
"a",
|
|
97
|
+
"b",
|
|
98
|
+
"k",
|
|
99
|
+
"s",
|
|
100
|
+
"é",
|
|
101
|
+
"😀",
|
|
102
|
+
"\\n",
|
|
103
|
+
"\\r",
|
|
104
|
+
"\\uFFFD",
|
|
105
|
+
"\\uD83D",
|
|
106
|
+
"[ab]",
|
|
107
|
+
"[^b]",
|
|
108
|
+
"\\w",
|
|
109
|
+
".",
|
|
110
|
+
];
|
|
111
|
+
const pieces = [
|
|
112
|
+
Buffer.from("abksABKSéÉKſ😀"),
|
|
113
|
+
Buffer.from("\r\n"),
|
|
114
|
+
Buffer.from("\n"),
|
|
115
|
+
Buffer.from([0xff]),
|
|
116
|
+
Buffer.from([0xc3, 0x28]),
|
|
117
|
+
Buffer.from([0xed, 0xa0, 0x80]),
|
|
118
|
+
];
|
|
119
|
+
const sources = Array.from({ length: 60 }, (_, index) =>
|
|
120
|
+
index % 2 === 0
|
|
121
|
+
? Buffer.concat(Array.from({ length: 1 + next(8) }, () => pieces[next(pieces.length)]))
|
|
122
|
+
: Buffer.from(Array.from({ length: 1 + next(80) }, () => 1 + next(255))),
|
|
123
|
+
);
|
|
124
|
+
const patterns = [
|
|
125
|
+
"colou?r",
|
|
126
|
+
"a\\uFFFDb",
|
|
127
|
+
"😀",
|
|
128
|
+
"k",
|
|
129
|
+
"s",
|
|
130
|
+
"(?:a|é)?b",
|
|
131
|
+
"a\\nb",
|
|
132
|
+
"[ab]+",
|
|
133
|
+
"(?i:a)(?-i:b)",
|
|
134
|
+
];
|
|
135
|
+
for (let index = 0; index < 200; index++) {
|
|
136
|
+
const a = atoms[next(atoms.length)];
|
|
137
|
+
const b = atoms[next(atoms.length)];
|
|
138
|
+
patterns.push(
|
|
139
|
+
[`${a}${b}`, `(?:${a}|${b})`, `(?:${a})?${b}`, `(?:${a})+${b}`, `(?=${a})${b}`][next(5)],
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
let checkedHits = 0;
|
|
143
|
+
let filteredHits = 0;
|
|
144
|
+
for (const pattern of patterns) {
|
|
145
|
+
for (const flags of ["m", "mi", "miu", "miv"]) {
|
|
146
|
+
// Some generated legacy patterns are invalid in Unicode mode; the real boundary rejects them.
|
|
147
|
+
try {
|
|
148
|
+
new RegExp(pattern, flags);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (error instanceof SyntaxError) continue;
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
for (const multiline of [false, true]) {
|
|
154
|
+
const request = { ...query(pattern, flags), multiline };
|
|
155
|
+
const prefilter = extractPrefilter(request);
|
|
156
|
+
for (const bytes of sources) {
|
|
157
|
+
if (
|
|
158
|
+
!searchText({ source: bytes.toString("utf8"), query: request, limit: 1 }).lines.length
|
|
159
|
+
)
|
|
160
|
+
continue;
|
|
161
|
+
checkedHits++;
|
|
162
|
+
if (prefilter.kind === "all") continue;
|
|
163
|
+
filteredHits++;
|
|
164
|
+
assert.ok(
|
|
165
|
+
hasCandidateBytes(bytes, prefilter),
|
|
166
|
+
JSON.stringify({ pattern, flags, multiline, bytes: bytes.toString("hex"), prefilter }),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
assert.ok(checkedHits > 1000, `Only ${checkedHits} matching cases exercised`);
|
|
173
|
+
assert.ok(filteredHits > 1000, `Only ${filteredHits} filtered matches exercised`);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("oversized literal alternation → retains necessary prefixes without dropping a branch", () => {
|
|
177
|
+
const words = Array.from({ length: 100 }, (_, index) => `word${index}`);
|
|
178
|
+
const prefilter = extractPrefilter(query(words.join("|")));
|
|
179
|
+
for (const word of words) assert.ok(hasCandidateBytes(Buffer.from(word), prefilter));
|
|
180
|
+
assert.equal(hasCandidateBytes(Buffer.from("unrelated"), prefilter), false);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("AND and OR file conditions → retain each alternative and require each conjunction", () => {
|
|
184
|
+
const prefilter = extractPrefilter(query("(?:foo.*bar|baz.*qux)"));
|
|
185
|
+
for (const value of ["foo\nbar", "baz\nqux"])
|
|
186
|
+
assert.ok(hasCandidateBytes(Buffer.from(value), prefilter));
|
|
187
|
+
for (const value of ["foo", "bar", "baz", "qux", "foo\nqux", "baz\nbar"])
|
|
188
|
+
assert.equal(hasCandidateBytes(Buffer.from(value), prefilter), false, value);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("empty expression with a huge repeat → completes without iterating the repeat count", () => {
|
|
192
|
+
assert.deepEqual(extractPrefilter(query("(?:){1000000000}")), { kind: "all" });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("word escape → keeps word-containing files and rejects punctuation-only files", () => {
|
|
196
|
+
const prefilter = extractPrefilter(query("\\w+"));
|
|
197
|
+
assert.ok(hasCandidateBytes(Buffer.from("---a---"), prefilter));
|
|
198
|
+
assert.equal(hasCandidateBytes(Buffer.from("---"), prefilter), false);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("deeply nested uppercase pattern → smart case stays sensitive and prefilter retains native hits", () => {
|
|
202
|
+
for (const depth of [50, 200, 500, 1_000, 2_000, 4_000]) {
|
|
203
|
+
const pattern = `${"(?:".repeat(depth)}A${")".repeat(depth)}`;
|
|
204
|
+
assert.equal(new RegExp(pattern).test("A"), true);
|
|
205
|
+
assert.equal(smartIgnoreCase([pattern], false), false);
|
|
206
|
+
assert.equal(hasCandidateBytes(Buffer.from("A"), extractPrefilter(query(pattern))), true);
|
|
207
|
+
}
|
|
208
|
+
});
|