Skip to content

Commit 5d563b6

Browse files
committed
Added string.substr
1 parent 61fa3df commit 5d563b6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Transpiler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,14 @@ export abstract class LuaTranspiler {
10091009
} else {
10101010
return `(string.find(${caller},${params}+1,true) or 0)-1`;
10111011
}
1012+
case "substr":
1013+
if (node.arguments.length === 1) {
1014+
return `string.sub(${caller},${params}+1)`;
1015+
} else {
1016+
const arg1 = this.transpileExpression(node.arguments[0]);
1017+
const arg2 = this.transpileExpression(node.arguments[1]);
1018+
return `string.sub(${caller},${arg1}+1,${arg1}+${arg2})`;
1019+
}
10121020
case "substring":
10131021
if (node.arguments.length === 1) {
10141022
return `string.sub(${caller},${params}+1)`;

test/unit/string.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,25 @@ export class StringTests {
146146
Expect(result).toBe(inp.substring(start, end));
147147
}
148148

149+
@TestCase("hello test", 0)
150+
@TestCase("hello test", 1)
151+
@TestCase("hello test", 1, 2)
152+
@TestCase("hello test", 1, 5)
153+
@Test("string.substring")
154+
public substr(inp: string, start: number, end?: number) {
155+
// Transpile
156+
const paramStr = end ? `${start}, ${end}` : `${start}`;
157+
const lua = util.transpileString(
158+
`return "${inp}".substr(${paramStr})`
159+
);
160+
161+
// Execute
162+
const result = util.executeLua(lua);
163+
164+
// Assert
165+
Expect(result).toBe(inp.substr(start, end));
166+
}
167+
149168
@TestCase("", 0)
150169
@TestCase("h", 1)
151170
@TestCase("hello", 5)

0 commit comments

Comments
 (0)