forked from MCJack123/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringReplaceAll.ts
More file actions
41 lines (39 loc) · 1.36 KB
/
Copy pathStringReplaceAll.ts
File metadata and controls
41 lines (39 loc) · 1.36 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
const sub = string.sub;
const find = string.find;
export function __TS__StringReplaceAll(
this: void,
source: string,
searchValue: string,
replaceValue: string | ((match: string, offset: number, string: string) => string)
): string {
if (typeof replaceValue === "string") {
const concat = table.concat(source.split(searchValue), replaceValue);
if (searchValue.length === 0) {
return replaceValue + concat + replaceValue;
}
return concat;
}
const parts: string[] = [];
let partsIndex = 1;
if (searchValue.length === 0) {
parts[0] = replaceValue("", 0, source);
partsIndex = 2;
for (const i of $range(1, source.length)) {
parts[partsIndex - 1] = sub(source, i, i);
parts[partsIndex] = replaceValue("", i, source);
partsIndex += 2;
}
} else {
let currentPos = 1;
while (true) {
const [startPos, endPos] = find(source, searchValue, currentPos, true);
if (!startPos) break;
parts[partsIndex - 1] = sub(source, currentPos, startPos - 1);
parts[partsIndex] = replaceValue(searchValue, startPos - 1, source);
partsIndex += 2;
currentPos = endPos + 1;
}
parts[partsIndex - 1] = sub(source, currentPos);
}
return table.concat(parts);
}