import type { Finding, FindingCategory } from './traceExport'; // Findings name a string inside a trace.jsonl row by JSON Pointer and count // Unicode scalars of its decoded value; the preview shows the row's JSON // text. This finds each string's place in that text and turns the scalar // offsets into UTF-16 offsets of the text, escapes included, so a marker // is highlighted exactly where the archive has it. /** A highlighted stretch of a row's text, in UTF-16 offsets. */ export interface Mark { start: number; end: number; category: FindingCategory; } interface Span { /** The text between the quotes. */ start: number; end: number; } /** Where every string value and key of one JSON row sits in its text, by pointer. */ function strings(row: string): { values: Map; keys: Map; } { const values = new Map(); const keys = new Map(); let at = 0; const space = () => { while (at < row.length && ' \t\r\n'.includes(row[at])) at++; }; const string = (): Span => { const start = ++at; while (row[at] !== '"') { if (at >= row.length) throw new Error('unterminated string'); at += row[at] === '\\' ? 2 : 1; } return { start, end: at++ }; }; const value = (pointer: string) => { space(); const c = row[at]; if (c === '"') values.set(pointer, string()); else if (c === '{') { at++; space(); if (row[at] === '}') return void at++; for (;;) { space(); const key = string(); const name = JSON.parse(row.slice(key.start - 1, key.end + 1)); const child = `${pointer}/${name.replaceAll('~', '~0').replaceAll('/', '~1')}`; keys.set(child, key); space(); if (row[at++] !== ':') throw new Error('expected :'); value(child); space(); if (row[at] === ',') at++; else if (row[at++] === '}') return; else throw new Error('expected , or }'); } } else if (c === '[') { at++; space(); if (row[at] === ']') return void at++; for (let index = 0; ; index++) { value(`${pointer}/${index}`); space(); if (row[at] === ',') at++; else if (row[at++] === ']') return; else throw new Error('expected , or ]'); } } else { const end = row.slice(at).search(/[,\]}\s]/); at = end < 0 ? row.length : at + end; } }; value(''); return { values, keys }; } /** The UTF-16 offset in the text of each of `scalars` (ascending), counted in the decoded string. */ function offsets(row: string, span: Span, scalars: number[]): number[] { const found: number[] = []; let at = span.start; let count = 0; for (const want of scalars) { while (count < want && at < span.end) { if (row[at] !== '\\') at += row.codePointAt(at)! > 0xffff ? 2 : 1; else if (row[at + 1] !== 'u') at += 2; else { const unit = parseInt(row.slice(at + 2, at + 6), 16); const low = unit >= 0xd800 && unit < 0xdc00 && row.startsWith('\\u', at + 6) ? parseInt(row.slice(at + 8, at + 12), 16) : 0; at += low >= 0xdc00 && low < 0xe000 ? 12 : 6; } count++; } if (count < want) return []; found.push(at); } return found; } /** * The marks of `findings` (those of this row) in the row's text, in order * and without overlaps, and how many could not be placed. */ export function marks( row: string, findings: Finding[], ): { marks: Mark[]; missed: number } { if (!findings.length) return { marks: [], missed: 0 }; let places: ReturnType; try { places = strings(row); } catch { return { marks: [], missed: findings.length }; } const placed: Mark[] = []; let missed = 0; for (const finding of findings) { const span = (finding.target === 'key' ? places.keys : places.values).get( finding.path, ); const [start, end] = span ? offsets(row, span, [finding.start, finding.end]) : []; if (start === undefined || end === undefined || end < start) missed++; else placed.push({ start, end, category: finding.category }); } placed.sort((a, b) => a.start - b.start || b.end - a.end); const out: Mark[] = []; for (const mark of placed) { const last = out[out.length - 1]; if (last && mark.start < last.end) last.end = Math.max(last.end, mark.end); else out.push({ ...mark }); } return { marks: out, missed }; } /** The row's text cut at its marks, for rendering. */ export function segments( row: string, placed: Mark[], ): { text: string; mark: Mark | null }[] { const out: { text: string; mark: Mark | null }[] = []; let at = 0; for (const mark of placed) { if (mark.start > at) out.push({ text: row.slice(at, mark.start), mark: null }); out.push({ text: row.slice(mark.start, mark.end), mark }); at = mark.end; } if (at < row.length) out.push({ text: row.slice(at), mark: null }); return out; }