diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 30646a81d5..1f1a6c2b80 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 5f614909b2..ade6d8448b 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 c8cedb52ad..dc70bbc94b 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")}"