| 14 | const MAX_CACHE_SIZE = 10; |
| 15 | |
| 16 | export function getLineStarts(content: string): number[] { |
| 17 | let lineStarts = lineStartsCache.get(content); |
| 18 | if (lineStarts) { |
| 19 | // LRU: delete and re-set to move to end |
| 20 | lineStartsCache.delete(content); |
| 21 | lineStartsCache.set(content, lineStarts); |
| 22 | return lineStarts; |
| 23 | } |
| 24 | |
| 25 | lineStarts = [0]; |
| 26 | let pos = 0; |
| 27 | while ((pos = content.indexOf('\n', pos)) !== -1) { |
| 28 | pos++; |
| 29 | lineStarts.push(pos); |
| 30 | } |
| 31 | |
| 32 | if (lineStartsCache.size >= MAX_CACHE_SIZE) { |
| 33 | const firstKey = lineStartsCache.keys().next().value; |
| 34 | if (firstKey !== undefined) { |
| 35 | lineStartsCache.delete(firstKey); |
| 36 | } |
| 37 | } |
| 38 | lineStartsCache.set(content, lineStarts); |
| 39 | |
| 40 | return lineStarts; |
| 41 | } |
| 42 | |
| 43 | export function positionToOffset( |
| 44 | content: string, |