Skip to content

Commit bb1ae31

Browse files
committed
Added support for string.substring
1 parent 89f5a6f commit bb1ae31

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

dist/Transpiler.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,15 @@ var LuaTranspiler = /** @class */ (function () {
485485
else {
486486
return "(string.find(" + caller + "," + params + "+1,true) or 0)-1";
487487
}
488+
case "substring":
489+
if (node.arguments.length == 1) {
490+
return "string.sub(" + caller + "," + params + "+1)";
491+
}
492+
else {
493+
var arg1 = this.transpileExpression(node.arguments[0]);
494+
var arg2 = this.transpileExpression(node.arguments[1]);
495+
return "string.sub(" + caller + "," + arg1 + "+1," + arg2 + "+1)";
496+
}
488497
default:
489498
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);
490499
}

src/Transpiler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ export class LuaTranspiler {
529529
} else {
530530
return `(string.find(${caller},${params}+1,true) or 0)-1`;
531531
}
532+
case "substring":
533+
if (node.arguments.length == 1) {
534+
return `string.sub(${caller},${params}+1)`;
535+
} else {
536+
const arg1 = this.transpileExpression(node.arguments[0]);
537+
const arg2 = this.transpileExpression(node.arguments[1]);
538+
return `string.sub(${caller},${arg1}+1,${arg2}+1)`;
539+
}
532540
default:
533541
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);
534542
}

test/src/test2.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function TestClass.Test(self)
3131
print((string.find(str,"b",2+1,true) or 0)-1)
3232
print((string.find("abc","c",1,true) or 0)-1)
3333
print((string.find(str,"b",1,true) or 0)-1)
34+
print(string.sub("abcde",2+1))
35+
print(string.sub("abcde",1+1,3+1))
3436
end
3537
function TestClass.Test3(self,a,b)
3638
return ""

test/src/test2.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export class TestClass {
4747
print(str[str.indexOf("b")]);
4848
print(str.indexOf("b", 2));
4949
print("abc".indexOf("c"));
50-
print(str.indexOf("b"))
50+
print(str.indexOf("b"));
51+
print("abcde".substring(2))
52+
print("abcde".substring(1, 3))
5153
}
5254

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

0 commit comments

Comments
 (0)