From f893b8d7e9efaa58975ea860673d866c21fa3c7e Mon Sep 17 00:00:00 2001 From: Manodasan Wignarajah Date: Fri, 29 Apr 2022 17:22:49 -0700 Subject: [PATCH] Fix escaping of embedded quotes. (#1182) --- .../TestComponentCSharp/TestComponentCSharp.idl | 2 +- src/cswinrt/code_writers.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Tests/TestComponentCSharp/TestComponentCSharp.idl b/src/Tests/TestComponentCSharp/TestComponentCSharp.idl index 776f19937..2f44d0827 100644 --- a/src/Tests/TestComponentCSharp/TestComponentCSharp.idl +++ b/src/Tests/TestComponentCSharp/TestComponentCSharp.idl @@ -542,7 +542,7 @@ And this is another one" void f(); } - [attr_string("a string with embedded quotes, like the string \"quotes\"")] + [attr_string("a string with embedded escape characters, like the string \"quotes\" and string \\\"quotes\\\" and string \'char\'")] runtimeclass EmbeddedQuotesTest { void f(); diff --git a/src/cswinrt/code_writers.h b/src/cswinrt/code_writers.h index ddeca8199..06d225a10 100644 --- a/src/cswinrt/code_writers.h +++ b/src/cswinrt/code_writers.h @@ -1502,10 +1502,24 @@ remove => %; }, [&](std::string_view type_name) { + bool previous_char_escape = false; std::string sanitized_type_name; sanitized_type_name.reserve(type_name.length() * 2); for (const auto& c : type_name) { + if (c == '\\' && !previous_char_escape) + { + previous_char_escape = true; + continue; + } + + // We only handle the following escape characters for now. + if (previous_char_escape && c != '\\' && c != '\'' && c != '"') + { + sanitized_type_name += '\\'; + } + previous_char_escape = false; + sanitized_type_name += c; if (c == '"') {