Skip to content

Commit

Permalink
Merge pull request #132 from mourner/faster-locate
Browse files Browse the repository at this point in the history
Optimize location with binary search
  • Loading branch information
Rich-Harris committed Mar 14, 2018
2 parents fa21135 + e63f97b commit 6cd6713
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions src/utils/getLocator.js
@@ -1,35 +1,25 @@
export default function getLocator ( source ) {
const originalLines = source.split( '\n' );
const lineOffsets = [];

let start = 0;
const lineRanges = originalLines.map( ( line, i ) => {
const end = start + line.length + 1;
const range = { start, end, line: i };

start = end;
return range;
});

let i = 0;

function rangeContains ( range, index ) {
return range.start <= index && index < range.end;
}

function getLocation ( range, index ) {
return { line: range.line, column: index - range.start };
for ( let i = 0, pos = 0; i < originalLines.length; i++ ) {
lineOffsets.push( pos );
pos += originalLines[i].length + 1;
}

return function locate ( index ) {
let range = lineRanges[i];

const d = index >= range.end ? 1 : -1;

while ( range ) {
if ( rangeContains( range, index ) ) return getLocation( range, index );

i += d;
range = lineRanges[i];
let i = 0;
let j = lineOffsets.length;
while ( i < j ) {
const m = ( i + j ) >> 1;
if ( index < lineOffsets[m] ) {
j = m;
} else {
i = m + 1;
}
}
const line = i - 1;
const column = index - lineOffsets[line];
return { line, column };
};
}

0 comments on commit 6cd6713

Please sign in to comment.