1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// What: `use memchr::memchr_iter;` imports a SIMD-accelerated
// "find every occurrence of byte B in slice S" iterator.
// memchr is the foundation that aho-corasick is also built
// on, so this dep is essentially free in our build.
// Why: `build_line_index` walks every byte of one file and
// records the offset of each `\n`. memchr_iter does that
// with AVX2/NEON (when available) instead of byte-at-a-time
// scalar code, so a 1M-line file builds the index in
// milliseconds instead of tens of milliseconds.
// TS map: No 1:1 equivalent. Closest is `String.prototype.matchAll`
// with a `/\n/g` regex, but that is slower than SIMD memchr.
//
// In TS you'd write (pseudocode):
// ```ts
// // No equivalent. Imagine:
// // for (const m of content.matchAll(/\n/g)) starts.push(m.index + 1);
// ```
use memchr_iter;
// What: The previous `is_likely_binary` heuristic lived here. It
// was removed (see BUG 5): a NUL byte in the first 8 KiB
// caused the entire scan to short-circuit, producing silent
// false negatives when a deny-listed literal appeared before
// a NUL in the file. AC scans raw bytes content-agnostic and
// the redacted output format makes a "binary file leaks
// secret" report useful; the perf saving from skipping
// binaries does not justify the soundness gap.
// What: `pub fn build_line_index(content: &[u8]) -> Vec<usize>`
// produces a sorted `Vec<usize>` of byte offsets where each
// line starts. The first entry is always `0` (line 1's start);
// subsequent entries are the offset of the byte JUST AFTER
// each `\n`. So a file `"abc\ndef"` yields `[0, 4]` --
// line 1 begins at 0, line 2 begins at 4.
// Why: Replacing the old per-hit byte walk with an O(n)-once index
// plus O(log L) lookups (L = line count). The win matters
// when a single file has many hits -- e.g. an agent that
// wrote a forbidden literal a million times: 2M walks of
// average length n/2 collapse to one O(n) build plus 2M
// binary searches. Building only happens lazily on the
// first hit, so 99%-clean files never pay this cost.
// TS map: `function buildLineIndex(content: Uint8Array): number[]`.
// Gotcha: The returned vec's length is `1 + count(\\n in content)`,
// NOT the visible line count when the file ends without a
// trailing newline. The last entry can equal `content.len()`
// when the file ends with `\n`; lookups must tolerate that.
//
// In TS you'd write (pseudocode):
// ```ts
// function buildLineIndex(content: Uint8Array): number[] {
// const starts = [0];
// for (let loopIndex = 0; loopIndex < content.length; loopIndex++) {
// if (content[i] === 0x0a) starts.push(i + 1);
// }
// return starts;
// }
// ```
// What: `pub fn line_and_col_indexed(line_starts: &[usize], offset: usize) -> (usize, usize)`
// is the indexed replacement for the old `line_and_col`. It
// does an O(log L) binary search instead of an O(offset)
// walk to find which line owns `offset`.
// Why: Same `(line, col)` output as before; faster when called
// many times on one file because the index is shared.
// TS map: `function lineAndColIndexed(lineStarts: number[], offset: number): [number, number]`.
//
// In TS you'd write (pseudocode):
// ```ts
// function lineAndColIndexed(lineStarts: number[], offset: number): [number, number] {
// // partition_point: first index whose value is > offset
// let lo = 0, hi = lineStarts.length;
// while (lo < hi) {
// const mid = (lo + hi) >> 1;
// if (lineStarts[mid] <= offset) lo = mid + 1; else hi = mid;
// }
// const lineIdx = Math.max(0, lo - 1);
// return [lineIdx + 1, offset - lineStarts[lineIdx] + 1];
// }
// ```
// What: `pub fn end_in_line_indexed(line_starts: &[usize], start: usize, end: usize) -> usize`
// returns the byte offset of the first `\n` in `[start, end)`
// if one exists, else returns `end` unchanged. Indexed
// replacement for the old `end_in_line`.
// Why: Same semantics as before -- clamping multi-line matches
// to one line for the report. Now O(log L) instead of
// O(end - start).
// TS map: `function endInLineIndexed(lineStarts: number[], start: number, end: number): number`.
//
// In TS you'd write (pseudocode):
// ```ts
// function endInLineIndexed(lineStarts: number[], start: number, end: number): number {
// const lineIdx = Math.max(0, partitionPoint(lineStarts, s => s <= start) - 1);
// if (lineIdx + 1 < lineStarts.length) {
// const nextLineStart = lineStarts[lineIdx + 1];
// if (nextLineStart > 0 && nextLineStart - 1 < end) return nextLineStart - 1;
// }
// return end;
// }
// ```
// What: `pub fn format_hit(path, line, col_start, col_end, rule_idx) -> String`
// builds the redacted `path:line:col_start..col_end rule=N`
// output string. Public so `scan.rs` can call it.
// Why: Output format must NEVER include the matched substring --
// the failing CI log itself is a leak surface. Centralizing
// the format string here ensures every hit is redacted the
// same way.
// TS map: `function formatHit(path: string, line: number, colStart: number, colEnd: number, ruleIdx: number): string`.
//
// In TS you'd write (pseudocode):
// ```ts
// function formatHit(path, line, colStart, colEnd, ruleIdx) {
// return `${path}:${line}:${colStart}..${colEnd} rule=${ruleIdx}`;
// }
// ```
// What: `pub fn emit_hit(li, path, start, end, rule_idx) -> String`
// composes the three-step (line, col_start, col_end) compute
// and `format_hit` call that every hit-emission site in
// `scan.rs` previously inlined. Takes `&[usize]` (the
// already-initialised line-start index) so the lazy
// `OnceLock::get_or_init` invariant stays at each call site.
// Why: `scan.rs::scan_content` emits hits from four near-identical
// code paths (AC literal, prefix-matched par_iter, residual
// Single shard, residual Combined par_iter). Each previously
// spelled out: `line_and_col_indexed` for start, then
// `end_in_line_indexed` to clamp multi-line matches to one
// line, then a second `line_and_col_indexed` on `end - 1` (or
// 0 when `end` is 0) to derive col_end, then `format_hit`.
// Centralising the sequence here removes ~60 logic lines from
// `scan.rs` and ensures every site emits the same
// `path:line:col_start..col_end rule=N` shape. `#[inline]`
// guarantees the helper compiles to the same machine code the
// inlined version produced (private to this crate, called
// from one consumer, LTO already inlines tiny crate-internal
// helpers, but the attribute removes any doubt on the hot
// path).
// TS map: `function emitHit(li: number[], path: string, start: number, end: number, ruleIdx: number): string`.
// Gotcha: This helper does NOT skip empty-span matches (`start ==
// end`). The three regex-result emission sites in scan.rs
// guard that with `if m.start == m.end { continue; }` before
// calling emit_hit; the AC literal site does not need the
// guard because AC patterns are non-empty by construction.
// Keeping the guard at the call site preserves that
// asymmetry and lets the `continue` skip the push entirely
// instead of building a hit string we would discard.
//
// In TS you'd write (pseudocode):
// ```ts
// function emitHit(li, path, start, end, ruleIdx) {
// const [line, colStart] = lineAndColIndexed(li, start);
// const endInLine = endInLineIndexed(li, start, end);
// const [, colEnd] = lineAndColIndexed(li, endInLine > 0 ? endInLine - 1 : 0);
// return formatHit(path, line, colStart, colEnd, ruleIdx);
// }
// ```
// What: `#[cfg(test)] #[path = "scan_format_tests.rs"] mod tests;`
// declares a test-only submodule whose code lives in the sibling
// file `scan_format_tests.rs`. `#[cfg(test)]` gates it to test
// builds only; `#[path = "..."]` aims the module at a flat
// sibling file instead of the default `scan_format/tests.rs`
// subdirectory lookup. The file stays the `tests` CHILD of
// `scan_format`, so its `use super::*` reaches the leaf helpers
// above unchanged.
// Why: Keep `scan_format.rs` to its production helpers; the
// invariant-pinning tests live beside it without inflating this
// file or its max-lines budget (sibling `*_tests.rs` are exempt).
// TS map: the `scan_format.unit.test.ts` file beside `scan_format.ts`,
// excluded from the production bundle.
//
// In TS you'd write (pseudocode):
// ```ts
// // scan_format.unit.test.ts, run only by the test runner
// ```