Skip to content

Commit 9e0a0f0

Browse files
andreiraduPerryvw
authored andcommitted
Add some string functions (#377)
* -added string concat * -added string.slice * -added string.charCodeAt * -added string concat test * -removed copy-paste declares
1 parent da0056a commit 9e0a0f0

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export enum LuaLibFeature {
2525
Set = "Set",
2626
StringReplace = "StringReplace",
2727
StringSplit = "StringSplit",
28+
StringConcat = "StringConcat",
2829
Symbol = "Symbol",
2930
}
3031

src/LuaTransformer.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,8 @@ export class LuaTransformer {
28042804
switch (expressionName) {
28052805
case "replace":
28062806
return this.transformLuaLibFunction(LuaLibFeature.StringReplace, caller, ...params);
2807+
case "concat":
2808+
return this.transformLuaLibFunction(LuaLibFeature.StringConcat, caller, ...params);
28072809
case "indexOf":
28082810
const stringExpression =
28092811
node.arguments.length === 1
@@ -2843,6 +2845,18 @@ export class LuaTransformer {
28432845
const arg2 = params[1];
28442846
return this.createStringCall("sub", node, caller, arg1, arg2);
28452847
}
2848+
case "slice":
2849+
if (node.arguments.length === 0) {
2850+
return caller;
2851+
}
2852+
else if (node.arguments.length === 1) {
2853+
const arg1 = this.expressionPlusOne(params[0]);
2854+
return this.createStringCall("sub", node, caller, arg1);
2855+
} else {
2856+
const arg1 = this.expressionPlusOne(params[0]);
2857+
const arg2 = params[1];
2858+
return this.createStringCall("sub", node, caller, arg1, arg2);
2859+
}
28462860
case "toLowerCase":
28472861
return this.createStringCall("lower", node, caller);
28482862
case "toUpperCase":
@@ -2852,6 +2866,11 @@ export class LuaTransformer {
28522866
case "charAt":
28532867
const firstParamPlusOne = this.expressionPlusOne(params[0]);
28542868
return this.createStringCall("sub", node, caller, firstParamPlusOne, firstParamPlusOne);
2869+
case "charCodeAt":
2870+
{
2871+
const firstParamPlusOne = this.expressionPlusOne(params[0]);
2872+
return this.createStringCall("byte", node, caller, firstParamPlusOne);
2873+
}
28552874
default:
28562875
throw TSTLErrors.UnsupportedProperty("string", expressionName, node);
28572876
}

src/lualib/StringConcat.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function __TS__StringConcat(str1: string, ...args: string[]): string {
2+
let out = str1;
3+
for (const arg of args) {
4+
out = out + arg;
5+
}
6+
return out;
7+
}

test/unit/string.spec.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,20 @@ export class StringTests
8181
// Assert
8282
Expect(result).toBe(expected);
8383
}
84-
84+
@TestCase("", ["", ""])
85+
@TestCase("hello", ["test"])
86+
@TestCase("hello", [])
87+
@TestCase("hello", ["test", "bye"])
88+
@Test("string.concatFct")
89+
public concatFct(str: string, param: string[]): void {
90+
const paramStr = param.map(elem => `"${elem}"`).join(", ");
91+
console.log(`return "${str}".concat(${paramStr})`);
92+
const result = util.transpileAndExecute(
93+
`return "${str}".concat(${paramStr})`
94+
);
95+
// Assert
96+
Expect(result).toBe(str.concat(...param));
97+
}
8598
@TestCase("hello test", "")
8699
@TestCase("hello test", "t")
87100
@TestCase("hello test", "h")
@@ -122,7 +135,23 @@ export class StringTests
122135
// Assert
123136
Expect(result).toBe(inp.indexOf(searchValue, x));
124137
}
138+
@TestCase("hello test")
139+
@TestCase("hello test", 0)
140+
@TestCase("hello test", 1)
141+
@TestCase("hello test", 1, 2)
142+
@TestCase("hello test", 1, 5)
143+
@Test("string.slice")
144+
public slice(inp: string, start?: number, end?: number): void
145+
{
146+
// Transpile/Execute
147+
const paramStr = start? (end ? `${start}, ${end}` : `${start}`):'';
148+
const result = util.transpileAndExecute(
149+
`return "${inp}".slice(${paramStr})`
150+
);
125151

152+
// Assert
153+
Expect(result).toBe(inp.slice(start, end));
154+
}
126155
@TestCase("hello test", 0)
127156
@TestCase("hello test", 1)
128157
@TestCase("hello test", 1, 2)
@@ -258,6 +287,20 @@ export class StringTests
258287
Expect(result).toBe(inp.charAt(index));
259288
}
260289

290+
@TestCase("hello test", 1)
291+
@TestCase("hello test", 2)
292+
@TestCase("hello test", 3)
293+
@Test("string.charCodeAt")
294+
public charCodeAt(inp: string, index: number): void
295+
{
296+
const result = util.transpileAndExecute(
297+
`return "${inp}".charCodeAt(${index})`
298+
);
299+
300+
// Assert
301+
Expect(result).toBe(inp.charCodeAt(index));
302+
}
303+
261304
@TestCase("hello test", 1, 0)
262305
@TestCase("hello test", 1, 2)
263306
@TestCase("hello test", 3, 2)

0 commit comments

Comments
 (0)