From 1cdba39aa0d8b5797584c9150bc327a48d5d2b7e Mon Sep 17 00:00:00 2001 From: Mateon1 Date: Mon, 14 Oct 2024 23:21:23 +0200 Subject: [PATCH] Preproc: output string literals, and fix escape bug --- tools/preproc/c_file.cpp | 19 +++++++++++++++---- tools/preproc/string_parser.cpp | 2 ++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/tools/preproc/c_file.cpp b/tools/preproc/c_file.cpp index 94c0366f..98e65ef6 100644 --- a/tools/preproc/c_file.cpp +++ b/tools/preproc/c_file.cpp @@ -220,7 +220,10 @@ void CFile::TryConvertString() SkipWhitespace(); - std::printf("{ "); + if (noTerminator) + std::printf("{ "); + else + std::printf("("); while (1) { @@ -241,8 +244,16 @@ void CFile::TryConvertString() RaiseError(e.what()); } - for (int i = 0; i < length; i++) - printf("0x%02X, ", s[i]); + if (!noTerminator) + printf("\""); + for (int i = 0; i < length; i++) { + if (noTerminator) + printf("0x%02X, ", s[i]); + else + printf(' ' <= s[i] && s[i] <= '~' && s[i] != '\\' && s[i] != '\"' ? "%c" : "\\%03o", s[i]); + } + if (!noTerminator) + printf("\""); } else if (m_buffer[m_pos] == ')') { @@ -263,7 +274,7 @@ void CFile::TryConvertString() if (noTerminator) std::printf(" }"); else - std::printf("0x00 }"); + std::printf(")"); } bool CFile::CheckIdentifier(const std::string& ident) diff --git a/tools/preproc/string_parser.cpp b/tools/preproc/string_parser.cpp index dd5196a4..00533434 100644 --- a/tools/preproc/string_parser.cpp +++ b/tools/preproc/string_parser.cpp @@ -44,6 +44,7 @@ std::string StringParser::ReadCharOrEscape() if (sequence.length() == 0) RaiseError("no mapping exists for double quote"); + m_pos++; return sequence; } else if (m_buffer[m_pos] == '\\') @@ -53,6 +54,7 @@ std::string StringParser::ReadCharOrEscape() if (sequence.length() == 0) RaiseError("no mapping exists for backslash"); + m_pos++; return sequence; } }