main
Last change
on this file since 3ba01da was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * Tracks newlines during parsing in order to provide an efficient API for
|
---|
| 5 | * determining the one-indexed `{ line, col }` position for any offset
|
---|
| 6 | * within the input.
|
---|
| 7 | */
|
---|
| 8 | class LineCounter {
|
---|
| 9 | constructor() {
|
---|
| 10 | this.lineStarts = [];
|
---|
| 11 | /**
|
---|
| 12 | * Should be called in ascending order. Otherwise, call
|
---|
| 13 | * `lineCounter.lineStarts.sort()` before calling `linePos()`.
|
---|
| 14 | */
|
---|
| 15 | this.addNewLine = (offset) => this.lineStarts.push(offset);
|
---|
| 16 | /**
|
---|
| 17 | * Performs a binary search and returns the 1-indexed { line, col }
|
---|
| 18 | * position of `offset`. If `line === 0`, `addNewLine` has never been
|
---|
| 19 | * called or `offset` is before the first known newline.
|
---|
| 20 | */
|
---|
| 21 | this.linePos = (offset) => {
|
---|
| 22 | let low = 0;
|
---|
| 23 | let high = this.lineStarts.length;
|
---|
| 24 | while (low < high) {
|
---|
| 25 | const mid = (low + high) >> 1; // Math.floor((low + high) / 2)
|
---|
| 26 | if (this.lineStarts[mid] < offset)
|
---|
| 27 | low = mid + 1;
|
---|
| 28 | else
|
---|
| 29 | high = mid;
|
---|
| 30 | }
|
---|
| 31 | if (this.lineStarts[low] === offset)
|
---|
| 32 | return { line: low + 1, col: 1 };
|
---|
| 33 | if (low === 0)
|
---|
| 34 | return { line: 0, col: offset };
|
---|
| 35 | const start = this.lineStarts[low - 1];
|
---|
| 36 | return { line: low, col: offset - start + 1 };
|
---|
| 37 | };
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | exports.LineCounter = LineCounter;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.