Skip to content

Commit 64cbbe8

Browse files
committed
Fixed StringReplace and StringSplit
1 parent 5d563b6 commit 64cbbe8

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/lualib/StringReplace.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
declare namespace string {
2-
function gsub(source: string, searchValue: string, replaceValue: string): string;
2+
/** !TupleReturn */
3+
function gsub(source: string, searchValue: string, replaceValue: string): [string, number];
34
}
45

56
function __TS__StringReplace(source: string, searchValue: string, replaceValue: string): string {
6-
return string.gsub(source, searchValue, replaceValue);
7+
return string.gsub(source, searchValue, replaceValue)[0];
78
}

src/lualib/StringSplit.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function __TS__StringSplit(source: string, separator?: string, limit?: number): string[] {
2+
if (limit === undefined) {
3+
limit = 4294967295;
4+
}
5+
6+
if (limit === 0) {
7+
return [];
8+
}
9+
10+
const out = [];
11+
let index = 0;
12+
let count = 0;
13+
14+
if (separator === undefined || separator === "") {
15+
while (index < source.length - 1 && count < limit) {
16+
out[count] = source[index];
17+
count++;
18+
index++;
19+
}
20+
} else {
21+
const separatorLength = separator.length;
22+
let nextIndex = source.indexOf(separator);
23+
while (nextIndex >= 0 && count < limit) {
24+
out[count] = source.substring(index, nextIndex);
25+
count++;
26+
index = nextIndex + separatorLength;
27+
nextIndex = source.indexOf(separator, index);
28+
}
29+
}
30+
31+
if (count < limit) {
32+
out[count] = source.substring(index);
33+
}
34+
35+
return out;
36+
}

0 commit comments

Comments
 (0)