From 68f18fcb8e0ec3103591c06df9bfd73cbbbd4a51 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 8 Aug 2023 17:25:05 +0800 Subject: [PATCH] scanner: fix string interpolation with nested string interpolation in inner quotes (fix #19081) (#19085) --- vlib/v/scanner/scanner.v | 2 +- ...ing_interpolation_with_inner_quotes_test.v | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/string_interpolation_with_inner_quotes_test.v diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 67540cb0c2..18f35a764d 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -648,7 +648,7 @@ fn (mut s Scanner) text_scan() token.Token { } // End of $var, start next string if s.is_inter_end { - if s.text[s.pos] == s.quote { + if s.text[s.pos] == s.quote || (s.text[s.pos] == s.inter_quote && s.is_enclosed_inter) { s.is_inter_end = false return s.new_token(.string, '', 1) } diff --git a/vlib/v/tests/string_interpolation_with_inner_quotes_test.v b/vlib/v/tests/string_interpolation_with_inner_quotes_test.v new file mode 100644 index 0000000000..866bc0e2a7 --- /dev/null +++ b/vlib/v/tests/string_interpolation_with_inner_quotes_test.v @@ -0,0 +1,32 @@ +fn f(x int, s string) string { + return 'label ${s}: ${x}' +} + +// vfmt off +fn test_string_interp_with_inner_quotes() { + x := 'hi' + println('abc ${f(123, 'def')} xyz') + assert 'abc ${f(123, 'def')} xyz' == 'abc label def: 123 xyz' + + println('abc ${f(123, "def")} xyz') + assert 'abc ${f(123, "def")} xyz' == 'abc label def: 123 xyz' + + println("abc ${f(123, 'def')} xyz") + assert "abc ${f(123, 'def')} xyz" == 'abc label def: 123 xyz' + + println("abc ${f(123, "def")} xyz") + assert "abc ${f(123, "def")} xyz" == 'abc label def: 123 xyz' + + println("abc ${f(123, "$x $x")} xyz") + assert "abc ${f(123, "$x $x")} xyz" == 'abc label hi hi: 123 xyz' + + println('abc ${f(123, '$x $x')} xyz') + assert 'abc ${f(123, '$x $x')} xyz' == 'abc label hi hi: 123 xyz' + + println('abc ${f(123, "$x $x")} xyz') + assert 'abc ${f(123, "$x $x")} xyz' == 'abc label hi hi: 123 xyz' + + println("abc ${f(123, '$x $x')} xyz") + assert "abc ${f(123, '$x $x')} xyz" == 'abc label hi hi: 123 xyz' +} +// vfmt on