export function escapeRegExp(s) {
    return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
export function occurrenceToRange(occ) {
    return {
        start: { line: occ.line, character: occ.character },
        end: { line: occ.line, character: occ.character + occ.length },
    };
}
/**
 * Compute the character offset of the start of the given line without splitting.
 */
export function offsetOfLine(source, line) {
    let offset = 0;
    for (let i = 0; i < line; i++) {
        const idx = source.indexOf("\n", offset);
        if (idx === -1)
            return source.length;
        offset = idx + 1;
    }
    return offset;
}
/**
 * Find all whole-word occurrences of `word` in `source`.
 * Known limitation: matches inside string literals and comments.
 */
export function findAllOccurrences(source, word) {
    const occurrences = [];
    const lines = source.split("\n");
    const pattern = new RegExp(`\\b${escapeRegExp(word)}\\b`, "g");
    for (let line = 0; line < lines.length; line++) {
        let match;
        while ((match = pattern.exec(lines[line])) !== null) {
            occurrences.push({ line, character: match.index, length: word.length });
        }
    }
    return occurrences;
}
