X Tutup
Skip to content

Commit 0d7c5cd

Browse files
authored
Fix some string operations (#895)
* Fix string.slice, string.substring, string.substr * Fix string.charAt and string.charCodeAt * Fix string.indexOf with negative indexes * Fix out-of-bounds string access being "" instead of undefined * Fix array.join flacky test * Feedback * Replace createExpressionPlusOne with more general addToNumericExpression
1 parent cd2932e commit 0d7c5cd

File tree

16 files changed

+233
-123
lines changed

16 files changed

+233
-123
lines changed

src/LuaLib.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,19 @@ export enum LuaLibFeature {
5454
WeakSet = "WeakSet",
5555
SourceMapTraceBack = "SourceMapTraceBack",
5656
Spread = "Spread",
57+
StringAccess = "StringAccess",
58+
StringCharAt = "StringCharAt",
59+
StringCharCodeAt = "StringCharCodeAt",
5760
StringConcat = "StringConcat",
5861
StringEndsWith = "StringEndsWith",
5962
StringPadEnd = "StringPadEnd",
6063
StringPadStart = "StringPadStart",
6164
StringReplace = "StringReplace",
65+
StringSlice = "StringSlice",
6266
StringSplit = "StringSplit",
6367
StringStartsWith = "StringStartsWith",
68+
StringSubstr = "StringSubstr",
69+
StringSubstring = "StringSubstring",
6470
StringTrim = "StringTrim",
6571
StringTrimEnd = "StringTrimEnd",
6672
StringTrimStart = "StringTrimStart",

src/lualib/StringAccess.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function __TS__StringAccess(this: string, index: number) {
2+
if (index >= 0 && index < this.length) {
3+
return string.sub(this, index + 1, index + 1);
4+
}
5+
}

src/lualib/StringCharAt.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function __TS__StringCharAt(this: string, pos: number): string {
2+
if (pos !== pos) pos = 0;
3+
if (pos < 0) return "";
4+
return string.sub(this, pos + 1, pos + 1);
5+
}

src/lualib/StringCharCodeAt.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function __TS__StringCharCodeAt(this: string, index: number): number {
2+
if (index !== index) index = 0;
3+
if (index < 0) return NaN;
4+
return string.byte(this, index + 1) ?? NaN;
5+
}

src/lualib/StringPadEnd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ function __TS__StringPadEnd(this: string, maxLength: number, fillString = " "):
1313
fillString += fillString.repeat(maxLength / fillString.length);
1414
}
1515

16-
return this + fillString.slice(0, Math.floor(maxLength));
16+
return this + string.sub(fillString, 1, Math.floor(maxLength));
1717
}

src/lualib/StringPadStart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ function __TS__StringPadStart(this: string, maxLength: number, fillString = " ")
1313
fillString += fillString.repeat(maxLength / fillString.length);
1414
}
1515

16-
return fillString.slice(0, Math.floor(maxLength)) + this;
16+
return string.sub(fillString, 1, Math.floor(maxLength)) + this;
1717
}

src/lualib/StringSlice.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function __TS__StringSlice(this: string, start?: number, end?: number): string {
2+
if (start === undefined || start !== start) start = 0;
3+
if (end !== end) end = 0;
4+
5+
if (start >= 0) start += 1;
6+
if (end !== undefined && end < 0) end -= 1;
7+
8+
return string.sub(this, start, end);
9+
}

src/lualib/StringSubstr.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function __TS__StringSubstr(this: string, from: number, length?: number): string {
2+
if (from !== from) from = 0;
3+
4+
if (length !== undefined) {
5+
if (length !== length || length <= 0) return "";
6+
length += from;
7+
}
8+
9+
if (from >= 0) from += 1;
10+
11+
return string.sub(this, from, length);
12+
}

src/lualib/StringSubstring.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function __TS__StringSubstring(this: string, start: number, end?: number): string {
2+
if (end !== end) end = 0;
3+
4+
if (end !== undefined && start > end) {
5+
[start, end] = [end, start];
6+
}
7+
8+
if (start >= 0) {
9+
start += 1;
10+
} else {
11+
start = 1;
12+
}
13+
14+
if (end !== undefined && end < 0) end = 0;
15+
16+
return string.sub(this, start, end);
17+
}

src/lualib/declarations/string.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/** @noSelf */
22
declare namespace string {
3+
function byte(s: string, i?: number): number | undefined;
4+
/** @tupleReturn */
5+
function byte(s: string, i?: number, j?: number): number[];
6+
37
/** @tupleReturn */
48
function gsub(
59
source: string,

0 commit comments

Comments
 (0)
X Tutup