String escape

This commit is contained in:
Kampfkarren 2022-07-26 18:49:17 -07:00
parent 7d7ad6ba46
commit 5a6912fe75
2 changed files with 14 additions and 4 deletions

View file

@ -1480,14 +1480,24 @@ struct Compiler
void compileExprInterpString(AstExprInterpString* expr, uint8_t target, bool targetTemp)
{
// INTERP TODO: percent sign escape
std::string formatString;
unsigned int stringsLeft = expr->strings.size;
for (AstArray<char> const& string : expr->strings)
{
formatString += string.data;
std::string stringEscaped(string.data);
for (size_t characterIndex = 0; characterIndex < stringEscaped.size(); ++characterIndex)
{
if (stringEscaped[characterIndex] == '%')
{
stringEscaped.insert(characterIndex, 1, '%');
characterIndex++;
}
}
formatString += stringEscaped;
stringsLeft--;

View file

@ -43,8 +43,8 @@ assertEq(`Hello {`from inside {"a nested string"}`}`, "Hello from inside a neste
assertEq(`1 {`2 {`3 {4}`}`}`, "1 2 3 4")
-- local health = 50
-- assert(`You have {health}% health` == "You have 50% health")
local health = 50
assert(`You have {health}% health` == "You have 50% health")
-- INTERP TODO: Test with shadowing `string` (both as a string and not)