(src: string, baseLine: number)
| 18 | * never produce a match — then answers each call with a binary search. |
| 19 | */ |
| 20 | export function makeLineAt(src: string, baseLine: number): (idx: number) => number { |
| 21 | let nl: number[] | null = null; |
| 22 | return (idx: number) => { |
| 23 | if (!nl) { |
| 24 | nl = []; |
| 25 | for (let i = src.indexOf('\n'); i !== -1; i = src.indexOf('\n', i + 1)) nl.push(i); |
| 26 | } |
| 27 | // Count newlines strictly before idx. |
| 28 | let lo = 0; |
| 29 | let hi = nl.length; |
| 30 | while (lo < hi) { |
| 31 | const mid = (lo + hi) >> 1; |
| 32 | if (nl[mid]! < idx) lo = mid + 1; |
| 33 | else hi = mid; |
| 34 | } |
| 35 | return baseLine + lo; |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | /** The last few long texts lineAt was asked about, each with its resolver; the most recent last. */ |
| 40 | const LINE_AT_TEXTS: Array<{ text: string; at: (idx: number) => number }> = []; |
no test coverage detected