Last change
on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.9 KB
|
Line | |
---|
1 | "use strict";
|
---|
2 | var LF = '\n';
|
---|
3 | var CR = '\r';
|
---|
4 | var LinesAndColumns = (function () {
|
---|
5 | function LinesAndColumns(string) {
|
---|
6 | this.string = string;
|
---|
7 | var offsets = [0];
|
---|
8 | for (var offset = 0; offset < string.length;) {
|
---|
9 | switch (string[offset]) {
|
---|
10 | case LF:
|
---|
11 | offset += LF.length;
|
---|
12 | offsets.push(offset);
|
---|
13 | break;
|
---|
14 | case CR:
|
---|
15 | offset += CR.length;
|
---|
16 | if (string[offset] === LF) {
|
---|
17 | offset += LF.length;
|
---|
18 | }
|
---|
19 | offsets.push(offset);
|
---|
20 | break;
|
---|
21 | default:
|
---|
22 | offset++;
|
---|
23 | break;
|
---|
24 | }
|
---|
25 | }
|
---|
26 | this.offsets = offsets;
|
---|
27 | }
|
---|
28 | LinesAndColumns.prototype.locationForIndex = function (index) {
|
---|
29 | if (index < 0 || index > this.string.length) {
|
---|
30 | return null;
|
---|
31 | }
|
---|
32 | var line = 0;
|
---|
33 | var offsets = this.offsets;
|
---|
34 | while (offsets[line + 1] <= index) {
|
---|
35 | line++;
|
---|
36 | }
|
---|
37 | var column = index - offsets[line];
|
---|
38 | return { line: line, column: column };
|
---|
39 | };
|
---|
40 | LinesAndColumns.prototype.indexForLocation = function (location) {
|
---|
41 | var line = location.line, column = location.column;
|
---|
42 | if (line < 0 || line >= this.offsets.length) {
|
---|
43 | return null;
|
---|
44 | }
|
---|
45 | if (column < 0 || column > this.lengthOfLine(line)) {
|
---|
46 | return null;
|
---|
47 | }
|
---|
48 | return this.offsets[line] + column;
|
---|
49 | };
|
---|
50 | LinesAndColumns.prototype.lengthOfLine = function (line) {
|
---|
51 | var offset = this.offsets[line];
|
---|
52 | var nextOffset = line === this.offsets.length - 1 ? this.string.length : this.offsets[line + 1];
|
---|
53 | return nextOffset - offset;
|
---|
54 | };
|
---|
55 | return LinesAndColumns;
|
---|
56 | }());
|
---|
57 | exports.__esModule = true;
|
---|
58 | exports["default"] = LinesAndColumns;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.