Spaces:
Sleeping
Sleeping
File size: 1,201 Bytes
d605f27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
type Position = [number, number];
export default class TextSource {
source: string;
linePositions: number[];
static matchPositions (regex: RegExp, source: string) {
const positions = [];
let match;
while(match = regex.exec(source))
positions.push(match.index);
return positions;
}
constructor (source) {
this.source = source;
const newlines = TextSource.matchPositions(/\n/g, source);
this.linePositions = [0, ...newlines.map(p => p + 1)];
}
slice (lines: number | Position, columns: Position): string {
if (!Array.isArray(lines))
lines = [lines, lines];
const start = this.linePositions[lines[0] - 1] + columns[0];
const end = this.linePositions[lines[1] - 1] + columns[1];
return this.source.substr(start, end - start);
}
charsToPosition (chars: number): Position {
let lines = this.linePositions.findIndex(p => p > chars);
lines = lines < 0 ? this.linePositions.length : lines;
const columns = chars - this.linePositions[lines - 1];
return [lines, columns];
}
positionToChars ([lines, columns]: Position): number {
if (lines >= this.linePositions.length)
return NaN;
return this.linePositions[lines - 1] + columns;
}
};
|