From 14f3c950be6e09245454f4f4198c32a94ab1162f Mon Sep 17 00:00:00 2001 From: shove Date: Tue, 9 Jan 2024 08:07:42 +0800 Subject: [PATCH] cgen: fix escape of non-ascii characters in string interpolation (fix #20432) (#20437) --- vlib/v/gen/c/str_intp.v | 2 +- vlib/v/tests/string_interpolation_test.v | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/str_intp.v b/vlib/v/gen/c/str_intp.v index a9d24edfabf6dc..f097433ed60674 100644 --- a/vlib/v/gen/c/str_intp.v +++ b/vlib/v/gen/c/str_intp.v @@ -265,7 +265,7 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { g.write(' str_intp(${node.vals.len}, ') g.write('_MOV((StrIntpData[]){') for i, val in node.vals { - mut escaped_val := util.smart_quote(val, false) + mut escaped_val := cescape_nonascii(util.smart_quote(val, false)) escaped_val = escaped_val.replace('\0', '\\0') if escaped_val.len > 0 { diff --git a/vlib/v/tests/string_interpolation_test.v b/vlib/v/tests/string_interpolation_test.v index d18cea346938f7..c46239909293f3 100644 --- a/vlib/v/tests/string_interpolation_test.v +++ b/vlib/v/tests/string_interpolation_test.v @@ -226,3 +226,10 @@ fn test_contains_zero_characters() { assert str.len == 7 assert str == 'abc\0abc' } + +// for issue: 20432 +// non-ascii escape was lost +fn test_interpo_non_ascii_characters() { + hello := '你好' + assert '${hello},世界!' == '你好,世界!' +}