forked from MCJack123/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSplit.ts
More file actions
36 lines (31 loc) · 1010 Bytes
/
StringSplit.ts
File metadata and controls
36 lines (31 loc) · 1010 Bytes
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
const sub = string.sub;
const find = string.find;
export function __TS__StringSplit(this: void, source: string, separator?: string, limit?: number): string[] {
if (limit === undefined) {
limit = 4294967295;
}
if (limit === 0) {
return [];
}
const result = [];
let resultIndex = 1;
if (separator === undefined || separator === "") {
for (const i of $range(1, source.length)) {
result[resultIndex - 1] = sub(source, i, i);
resultIndex++;
}
} else {
let currentPos = 1;
while (resultIndex <= limit) {
const [startPos, endPos] = find(source, separator, currentPos, true);
if (!startPos) break;
result[resultIndex - 1] = sub(source, currentPos, startPos - 1);
resultIndex++;
currentPos = endPos + 1;
}
if (resultIndex <= limit) {
result[resultIndex - 1] = sub(source, currentPos);
}
}
return result;
}