From 965c29828928222245a678ad1b5386327be78e4e Mon Sep 17 00:00:00 2001 From: Rangi Date: Thu, 12 Nov 2026 11:01:52 -0500 Subject: [PATCH] Escape Make special characters other than dollar signs --- src/asm/main.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/asm/main.cpp b/src/asm/main.cpp index bfd3702b..1a59eb99 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -111,18 +111,27 @@ static Usage usage = { std::string escapeMakeChars(std::string const &str) { std::string escaped; - size_t pos = 0; - for (;;) { - // All dollars needs to be doubled - size_t nextPos = str.find('$', pos); - if (nextPos == std::string::npos) { + escaped.reserve(str.length()); + for (char c : str) { + // Escape characters that need escaping + switch (c) { + case '$': + // Dollar signs needs to be doubled + escaped += "$$"; + break; + case ' ': + case '#': + case ':': + case '%': + case '\\': + // All other special characters need backslash escaping + escaped += '\\'; + [[fallthrough]]; + default: + escaped += c; break; } - escaped.append(str, pos, nextPos - pos); - escaped.append("$$"); - pos = nextPos + literal_strlen("$"); } - escaped.append(str, pos, str.length() - pos); return escaped; }