From eebc82d83a493880109465566e064e7f8e4bef91 Mon Sep 17 00:00:00 2001 From: shove Date: Tue, 11 Oct 2022 02:19:34 +0800 Subject: [PATCH] fmt: fix wrong processing of quotes, when formatting string literals (fix #16017) (#16018) --- vlib/v/fmt/fmt.v | 26 ++++++++++------------ vlib/v/fmt/tests/string_quotes_expected.vv | 10 +++++++++ vlib/v/fmt/tests/string_quotes_input.vv | 10 +++++++++ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 30646a81d..1f1a6c2b8 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2621,18 +2621,14 @@ pub fn (mut f Fmt) char_literal(node ast.CharLiteral) { } pub fn (mut f Fmt) string_literal(node ast.StringLiteral) { - use_double_quote := node.val.contains("'") && !node.val.contains('"') + quote := if node.val.contains("'") && !node.val.contains('"') { '"' } else { "'" } if node.is_raw { f.write('r') } else if node.language == ast.Language.c { f.write('c') } if node.is_raw { - if use_double_quote { - f.write('"$node.val"') - } else { - f.write("'$node.val'") - } + f.write('$quote$node.val$quote') } else { unescaped_val := node.val.replace('$fmt.bs$fmt.bs', '\x01').replace_each([ "$fmt.bs'", @@ -2640,13 +2636,8 @@ pub fn (mut f Fmt) string_literal(node ast.StringLiteral) { '$fmt.bs"', '"', ]) - if use_double_quote { - s := unescaped_val.replace_each(['\x01', '$fmt.bs$fmt.bs', '"', '$fmt.bs"']) - f.write('"$s"') - } else { - s := unescaped_val.replace_each(['\x01', '$fmt.bs$fmt.bs', "'", "$fmt.bs'"]) - f.write("'$s'") - } + s := unescaped_val.replace_each(['\x01', '$fmt.bs$fmt.bs', quote, '$fmt.bs$quote']) + f.write('$quote$s$quote') } } @@ -2673,7 +2664,14 @@ pub fn (mut f Fmt) string_inter_literal(node ast.StringInterLiteral) { // work too different for the various exprs that are interpolated f.write(quote) for i, val in node.vals { - f.write(val) + unescaped_val := val.replace('$fmt.bs$fmt.bs', '\x01').replace_each([ + "$fmt.bs'", + "'", + '$fmt.bs"', + '"', + ]) + s := unescaped_val.replace_each(['\x01', '$fmt.bs$fmt.bs', quote, '$fmt.bs$quote']) + f.write('$s') if i >= node.exprs.len { break } diff --git a/vlib/v/fmt/tests/string_quotes_expected.vv b/vlib/v/fmt/tests/string_quotes_expected.vv index 5f614909b..ade6d8448 100644 --- a/vlib/v/fmt/tests/string_quotes_expected.vv +++ b/vlib/v/fmt/tests/string_quotes_expected.vv @@ -8,3 +8,13 @@ fn main() { println('I\'m out of idea "_"') println('Definitely out ":\'("') } + +arr := ['a', 'b'] +_ := "message\\n>${arr.join('\\n>')}" +_ := "message\\n>${arr.join('\\n>')}" +_ := 'message\\n>' +_ := 'message\\n>' +_ := 'values: ${arr.join('\\n')}' +_ := 'values: ${arr.join('\\n')}' +_ := 'values: ${arr.join('\\n')}' +_ := 'values: ${arr.join('\\n')}' diff --git a/vlib/v/fmt/tests/string_quotes_input.vv b/vlib/v/fmt/tests/string_quotes_input.vv index c8cedb52a..dc70bbc94 100644 --- a/vlib/v/fmt/tests/string_quotes_input.vv +++ b/vlib/v/fmt/tests/string_quotes_input.vv @@ -8,3 +8,13 @@ fn main() { println('I\'m out of idea "_"') println("Definitely out \":'(\"") } + +arr := ['a', 'b'] +_ := 'message\\n>${arr.join('\\n>')}' +_ := "message\\n>${arr.join('\\n>')}" +_ := 'message\\n>' +_ := "message\\n>" +_ := 'values: ${arr.join('\\n')}' +_ := "values: ${arr.join('\\n')}" +_ := 'values: ${arr.join("\\n")}' +_ := "values: ${arr.join("\\n")}" -- 2.39.5