forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext.ts
More file actions
153 lines (140 loc) · 4.41 KB
/
Copy pathtext.ts
File metadata and controls
153 lines (140 loc) · 4.41 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
export interface TextRange {
start: number;
end: number;
}
export function getTextPositionMessage(text: string, index: number): string {
if (!isFinite(index)) {
throw new Error(`Wrong char index ${index}`);
}
let message = '';
let line = 0;
let prevLn: number;
let nextLn = 0;
do {
line++;
prevLn = nextLn;
nextLn = text.indexOf('\n', prevLn + 1);
} while (nextLn >= 0 && nextLn <= index);
const column = index - prevLn;
message += `line ${line}, column ${column}`;
message += '\n';
if (index < text.length) {
message += text.substring(prevLn + 1, nextLn);
} else {
message += text.substring(text.lastIndexOf('\n') + 1);
}
message += '\n';
message += `${new Array(column).join('-')}^`;
return message;
}
export function getTextDiffIndex(a: string, b: string): number {
const short = Math.min(a.length, b.length);
for (let i = 0; i < short; i++) {
if (a[i] !== b[i]) {
return i;
}
}
if (a.length !== b.length) {
return short;
}
return -1;
}
export function parseArray(text: string): string[] {
return text.replace(/\r/g, '')
.split('\n')
.map((s) => s.trim())
.filter((s) => s);
}
export function formatArray(arr: readonly string[]): string {
return arr.concat('').join('\n');
}
export function getMatches(regex: RegExp, input: string, group = 0): string[] {
const matches: string[] = [];
let m: RegExpMatchArray | null;
while ((m = regex.exec(input))) {
matches.push(m[group]);
}
return matches;
}
export function getStringSize(value: string): number {
return value.length * 2;
}
export function getHashCode(text: string): number {
const len = text.length;
let hash = 0;
for (let i = 0; i < len; i++) {
const c = text.charCodeAt(i);
hash = ((hash << 5) - hash + c) & 4294967295;
}
return hash;
}
export function escapeRegExpSpecialChars(input: string): string {
return input.replaceAll(/[\^$.*+?\(\)\[\]{}|\-\\]/g, '\\$&');
}
export function getParenthesesRange(input: string, searchStartIndex = 0): TextRange | null {
return getOpenCloseRange(input, searchStartIndex, '(', ')', []);
}
export function getOpenCloseRange(
input: string,
searchStartIndex: number,
openToken: string,
closeToken: string,
excludeRanges: TextRange[],
): TextRange | null {
let indexOf: (token: string, pos: number) => number;
if (excludeRanges.length === 0) {
indexOf = (token: string, pos: number) => input.indexOf(token, pos);
} else {
indexOf = (token: string, pos: number) => indexOfExcluding(input, token, pos, excludeRanges);
}
const {length} = input;
let depth = 0;
let firstOpenIndex = -1;
for (let i = searchStartIndex; i < length; i++) {
if (depth === 0) {
const openIndex = indexOf(openToken, i);
if (openIndex < 0) {
break;
}
firstOpenIndex = openIndex;
depth++;
i = openIndex;
} else {
const closeIndex = indexOf(closeToken, i);
if (closeIndex < 0) {
break;
}
const openIndex = indexOf(openToken, i);
if (openIndex < 0 || closeIndex <= openIndex) {
depth--;
if (depth === 0) {
return {start: firstOpenIndex, end: closeIndex + 1};
}
i = closeIndex;
} else {
depth++;
i = openIndex;
}
}
}
return null;
}
function indexOfExcluding(input: string, search: string, position: number, excludeRanges: TextRange[]) {
const i = input.indexOf(search, position);
const exclusion = excludeRanges.find((r) => i >= r.start && i < r.end);
if (exclusion) {
return indexOfExcluding(input, search, exclusion.end, excludeRanges);
}
return i;
}
export function splitExcluding(input: string, separator: string, excludeRanges: TextRange[]): string[] {
const parts: string[] = [];
let commaIndex = -1;
let currIndex = 0;
while ((commaIndex = indexOfExcluding(input, separator, currIndex, excludeRanges)) >= 0) {
parts.push(input.substring(currIndex, commaIndex).trim());
currIndex = commaIndex + 1;
}
parts.push(input.substring(currIndex).trim());
return parts;
}