Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum LuaLibFeature {
Set = "Set",
StringReplace = "StringReplace",
StringSplit = "StringSplit",
StringConcat = "StringConcat",
Symbol = "Symbol",
}

Expand Down
19 changes: 19 additions & 0 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2708,6 +2708,8 @@ export class LuaTransformer {
switch (expressionName) {
case "replace":
return this.transformLuaLibFunction(LuaLibFeature.StringReplace, caller, ...params);
case "concat":
return this.transformLuaLibFunction(LuaLibFeature.StringConcat, caller, ...params);
case "indexOf":
const stringExpression =
node.arguments.length === 1
Expand Down Expand Up @@ -2747,6 +2749,18 @@ export class LuaTransformer {
const arg2 = params[1];
return this.createStringCall("sub", node, caller, arg1, arg2);
}
case "slice":
if (node.arguments.length === 0) {
return caller;
}
else if (node.arguments.length === 1) {
const arg1 = this.expressionPlusOne(params[0]);
return this.createStringCall("sub", node, caller, arg1);
} else {
const arg1 = this.expressionPlusOne(params[0]);
const arg2 = params[1];
return this.createStringCall("sub", node, caller, arg1, arg2);
}
case "toLowerCase":
return this.createStringCall("lower", node, caller);
case "toUpperCase":
Expand All @@ -2756,6 +2770,11 @@ export class LuaTransformer {
case "charAt":
const firstParamPlusOne = this.expressionPlusOne(params[0]);
return this.createStringCall("sub", node, caller, firstParamPlusOne, firstParamPlusOne);
case "charCodeAt":
{
const firstParamPlusOne = this.expressionPlusOne(params[0]);
return this.createStringCall("byte", node, caller, firstParamPlusOne);
}
default:
throw TSTLErrors.UnsupportedProperty("string", expressionName, node);
}
Expand Down
7 changes: 7 additions & 0 deletions src/lualib/StringConcat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function __TS__StringConcat(str1: string, ...args: string[]): string {
let out = str1;
for (const arg of args) {
out = out + arg;
}
return out;
}
45 changes: 44 additions & 1 deletion test/unit/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,20 @@ export class StringTests
// Assert
Expect(result).toBe(expected);
}

@TestCase("", ["", ""])
@TestCase("hello", ["test"])
@TestCase("hello", [])
@TestCase("hello", ["test", "bye"])
@Test("string.concatFct")
public concatFct(str: string, param: string[]): void {
const paramStr = param.map(elem => `"${elem}"`).join(", ");
console.log(`return "${str}".concat(${paramStr})`);
const result = util.transpileAndExecute(
`return "${str}".concat(${paramStr})`
);
// Assert
Expect(result).toBe(str.concat(...param));
}
@TestCase("hello test", "")
@TestCase("hello test", "t")
@TestCase("hello test", "h")
Expand Down Expand Up @@ -122,7 +135,23 @@ export class StringTests
// Assert
Expect(result).toBe(inp.indexOf(searchValue, x));
}
@TestCase("hello test")
@TestCase("hello test", 0)
@TestCase("hello test", 1)
@TestCase("hello test", 1, 2)
@TestCase("hello test", 1, 5)
@Test("string.slice")
public slice(inp: string, start?: number, end?: number): void
{
// Transpile/Execute
const paramStr = start? (end ? `${start}, ${end}` : `${start}`):'';
const result = util.transpileAndExecute(
`return "${inp}".slice(${paramStr})`
);

// Assert
Expect(result).toBe(inp.slice(start, end));
}
@TestCase("hello test", 0)
@TestCase("hello test", 1)
@TestCase("hello test", 1, 2)
Expand Down Expand Up @@ -258,6 +287,20 @@ export class StringTests
Expect(result).toBe(inp.charAt(index));
}

@TestCase("hello test", 1)
@TestCase("hello test", 2)
@TestCase("hello test", 3)
@Test("string.charCodeAt")
public charCodeAt(inp: string, index: number): void
{
const result = util.transpileAndExecute(
`return "${inp}".charCodeAt(${index})`
);

// Assert
Expect(result).toBe(inp.charCodeAt(index));
}

@TestCase("hello test", 1, 0)
@TestCase("hello test", 1, 2)
@TestCase("hello test", 3, 2)
Expand Down