TransformableString.js
3.04 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"use strict"
const lineEndingsRe = /\r\n|\r|\n/g
function lineStarts(str) {
const result = [0]
lineEndingsRe.lastIndex = 0
while (true) {
const match = lineEndingsRe.exec(str)
if (!match) break
result.push(lineEndingsRe.lastIndex)
}
return result
}
function locationToIndex(location, lineStarts) {
if (
!location.line ||
location.line < 0 ||
!location.column ||
location.column < 0
) {
throw new Error("Invalid location")
}
return lineStarts[location.line - 1] + location.column - 1
}
function indexToLocation(index, lineStarts) {
if (index < 0) throw new Error("Invalid index")
let line = 0
while (line + 1 < lineStarts.length && lineStarts[line + 1] <= index) {
line += 1
}
return {
line: line + 1,
column: index - lineStarts[line] + 1,
}
}
module.exports = class TransformableString {
constructor(original) {
this._original = original
this._blocks = []
this._lineStarts = lineStarts(original)
this._cache = null
}
_compute() {
if (!this._cache) {
let result = ""
let index = 0
for (const block of this._blocks) {
result += this._original.slice(index, block.from) + block.str
index = block.to
}
result += this._original.slice(index)
this._cache = {
lineStarts: lineStarts(result),
result,
}
}
return this._cache
}
getOriginalLine(n) {
if (n < 1 || n > this._lineStarts.length) {
throw new Error("Invalid line number")
}
return this._original
.slice(this._lineStarts[n - 1], this._lineStarts[n])
.replace(lineEndingsRe, "")
}
toString() {
return this._compute().result
}
replace(from, to, str) {
this._cache = null
if (from > to || from < 0 || to > this._original.length) {
throw new Error("Invalid slice indexes")
}
const newBlock = { from, to, str }
if (
!this._blocks.length ||
this._blocks[this._blocks.length - 1].to <= from
) {
this._blocks.push(newBlock)
} else {
const index = this._blocks.findIndex(other => other.to > from)
if (this._blocks[index].from < to) throw new Error("Can't replace slice")
this._blocks.splice(index, 0, newBlock)
}
}
originalIndex(index) {
let block
for (block of this._blocks) {
if (index < block.from) break
if (index < block.from + block.str.length) {
return
} else {
index += block.to - block.from - block.str.length
}
}
if (index < 0 || index > this._original.length) {
throw new Error("Invalid index")
}
if (index == this._original.length) {
if (block.to && block.to === this._original.length) {
return block.from + block.str.length
}
return this._original.length
}
return index
}
originalLocation(location) {
const index = locationToIndex(location, this._compute().lineStarts)
const originalIndex = this.originalIndex(index)
if (originalIndex !== undefined) {
return indexToLocation(originalIndex, this._lineStarts)
}
}
}