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
// 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: `pub fn is_likely_binary(content: &[u8]) -> bool` takes a
// borrowed byte slice and returns `true` if a NUL byte appears
// in the first 8KB.
// Why: Plain-text source code never contains NUL; binaries
// commonly do near the start. Skipping these files avoids
// regex cost on content with no meaningful "line:col" output
// and prevents accidentally-tracked blobs (qcow2 fragments,
// bundled images, lockfile-with-blob) from blowing up scan
// time.
// TS map: `function isLikelyBinary(content: Uint8Array): boolean`.
//
// In TS you'd write (pseudocode):
// ```ts
// function isLikelyBinary(content: Uint8Array): boolean {
// const probeLen = Math.min(content.length, 8192);
// for (let i = 0; i < probeLen; i++) if (content[i] === 0) return true;
// return false;
// }
// ```
// 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 i = 0; i < content.length; i++) {
// 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}`;
// }
// ```