Skip to content

Commit 89f5a6f

Browse files
committed
Added support for string.indexOf and string indexing
1 parent c964803 commit 89f5a6f

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

dist/TSHelper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ var TSHelper = /** @class */ (function () {
2828
}
2929
return "unknown";
3030
};
31+
TSHelper.isStringType = function (type) {
32+
return (type.flags & ts.TypeFlags.String) != 0
33+
|| (type.flags & ts.TypeFlags.StringLike) != 0
34+
|| (type.flags & ts.TypeFlags.StringLiteral) != 0;
35+
};
3136
TSHelper.isValueType = function (node) {
3237
return ts.isIdentifier(node) || ts.isLiteralExpression(node) || ts.isArrayLiteralExpression(node) || ts.isObjectLiteralExpression(node);
3338
};

dist/Transpiler.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,14 @@ var LuaTranspiler = /** @class */ (function () {
477477
var caller = this.transpileExpression(expression.expression);
478478
switch (expression.name.escapedText) {
479479
case "replace":
480-
return caller + ":sub(" + params + ")";
480+
return "string.sub(" + caller + "," + params + ")";
481+
case "indexOf":
482+
if (node.arguments.length == 1) {
483+
return "(string.find(" + caller + "," + params + ",1,true) or 0)-1";
484+
}
485+
else {
486+
return "(string.find(" + caller + "," + params + "+1,true) or 0)-1";
487+
}
481488
default:
482489
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);
483490
}
@@ -563,6 +570,9 @@ var LuaTranspiler = /** @class */ (function () {
563570
if (TSHelper_1.TSHelper.isArrayType(type) || TSHelper_1.TSHelper.isTupleType(type)) {
564571
return element + "[" + index + "+1]";
565572
}
573+
else if (TSHelper_1.TSHelper.isStringType(type)) {
574+
return "string.sub(" + element + "," + index + "+1," + index + "+1)";
575+
}
566576
else {
567577
return element + "[" + index + "]";
568578
}

src/TSHelper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export class TSHelper {
2929
return "unknown"
3030
}
3131

32+
static isStringType(type: ts.Type): boolean {
33+
return (type.flags & ts.TypeFlags.String) != 0
34+
|| (type.flags & ts.TypeFlags.StringLike) != 0
35+
|| (type.flags & ts.TypeFlags.StringLiteral) != 0
36+
}
37+
3238
static isValueType(node: ts.Node): boolean {
3339
return ts.isIdentifier(node) || ts.isLiteralExpression(node) || ts.isArrayLiteralExpression(node) || ts.isObjectLiteralExpression(node);
3440
}

src/Transpiler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,13 @@ export class LuaTranspiler {
522522
const caller = this.transpileExpression(expression.expression);
523523
switch (expression.name.escapedText) {
524524
case "replace":
525-
return `${caller}:sub(${params})`;
525+
return `string.sub(${caller},${params})`;
526+
case "indexOf":
527+
if (node.arguments.length == 1) {
528+
return `(string.find(${caller},${params},1,true) or 0)-1`;
529+
} else {
530+
return `(string.find(${caller},${params}+1,true) or 0)-1`;
531+
}
526532
default:
527533
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);
528534
}
@@ -619,6 +625,8 @@ export class LuaTranspiler {
619625
const type = this.checker.getTypeAtLocation(node.expression);
620626
if (tsEx.isArrayType(type) || tsEx.isTupleType(type)) {
621627
return `${element}[${index}+1]`;
628+
} else if (tsEx.isStringType(type)) {
629+
return `string.sub(${element},${index}+1,${index}+1)`;
622630
} else {
623631
return `${element}[${index}]`;
624632
}

test/src/test/test3.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ local f = #c.d
77
local g = #c.e
88
local h = #"abc"
99
table.insert(b, 3)
10-
a:sub("a","b")
10+
string.sub(a,"a","b")

test/src/test2.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ function TestClass.Test(self)
2626
print("sup")
2727
self.Test3(self,3,"")
2828
self.unit.GetParent(self.unit).GetParent(self.unit.GetParent(self.unit)).GetAbsOrigin(self.unit.GetParent(self.unit).GetParent(self.unit.GetParent(self.unit)))
29+
local str = "abcbc"
30+
print(string.sub(str,(string.find(str,"b",1,true) or 0)-1+1,(string.find(str,"b",1,true) or 0)-1+1))
31+
print((string.find(str,"b",2+1,true) or 0)-1)
32+
print((string.find("abc","c",1,true) or 0)-1)
33+
print((string.find(str,"b",1,true) or 0)-1)
2934
end
3035
function TestClass.Test3(self,a,b)
3136
return ""

test/src/test2.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ export class TestClass {
4343
print("sup");
4444
this.Test3(3, "");
4545
this.unit.GetParent().GetParent().GetAbsOrigin();
46+
let str="abcbc";
47+
print(str[str.indexOf("b")]);
48+
print(str.indexOf("b", 2));
49+
print("abc".indexOf("c"));
50+
print(str.indexOf("b"))
4651
}
4752

4853
Test3(a: number, b: string): string {

0 commit comments

Comments
 (0)