From 3b612899bf8b319525ba95f0869ed68fa12ba5bb Mon Sep 17 00:00:00 2001 From: Larpon Date: Wed, 17 Nov 2021 22:48:29 +0100 Subject: [PATCH] toml: eat first nl if present in multiline strings (#12496) --- vlib/toml/parser/parser.v | 7 +++++++ vlib/toml/tests/burntsushi.toml-test_test.v | 3 ++- vlib/toml/tests/strings_test.v | 10 +++++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/vlib/toml/parser/parser.v b/vlib/toml/parser/parser.v index e1ff7e1398..efd1ec0656 100644 --- a/vlib/toml/parser/parser.v +++ b/vlib/toml/parser/parser.v @@ -1127,6 +1127,13 @@ pub fn (mut p Parser) quoted() ast.Quoted { mut lit := p.tok.lit[1..p.tok.lit.len - 1] if is_multiline { lit = p.tok.lit[3..p.tok.lit.len - 3] + // From https://toml.io/en/v1.0.0#string + // "Multi-line literal strings [...] A newline immediately following the opening + // delimiter will be trimmed. All other content between the delimiters + // is interpreted as-is without modification." + if lit.len > 0 && lit[0] == `\n` { + lit = lit[1..] + } } return ast.Quoted{ text: lit diff --git a/vlib/toml/tests/burntsushi.toml-test_test.v b/vlib/toml/tests/burntsushi.toml-test_test.v index 986d559478..8680e395e1 100644 --- a/vlib/toml/tests/burntsushi.toml-test_test.v +++ b/vlib/toml/tests/burntsushi.toml-test_test.v @@ -18,7 +18,8 @@ const ( valid_value_exceptions = [ // String - 'string/raw-multiline.toml', // This test is not correct. Our parser *correctly* includes the newline at the start of the raw multiline. + 'string/double-quote-escape.toml', + 'string/unicode-escape.toml', 'string/escapes.toml', 'string/escape-tricky.toml', 'string/multiline.toml', diff --git a/vlib/toml/tests/strings_test.v b/vlib/toml/tests/strings_test.v index 8e816ad869..961c6e9cc1 100644 --- a/vlib/toml/tests/strings_test.v +++ b/vlib/toml/tests/strings_test.v @@ -40,9 +40,9 @@ fn test_multiline_strings() { value = toml_doc.value('multi2') assert value.string() == 'one\ntwo' value = toml_doc.value('multi3') - assert value.string() == '\none\ntwo\nthree' + assert value.string() == 'one\ntwo\nthree' value = toml_doc.value('multi4') - assert value.string() == '\none\ntwo\nthree\nfour\n' + assert value.string() == 'one\ntwo\nthree\nfour\n' toml_doc = toml.parse(toml_multiline_text_2) or { panic(err) } value = toml_doc.value('multi1') @@ -50,9 +50,9 @@ fn test_multiline_strings() { value = toml_doc.value('multi2') assert value.string() == 'one\ntwo' value = toml_doc.value('multi3') - assert value.string() == '\none\ntwo\nthree' + assert value.string() == 'one\ntwo\nthree' value = toml_doc.value('multi4') - assert value.string() == '\none\ntwo\nthree\nfour\n' + assert value.string() == 'one\ntwo\nthree\nfour\n' toml_file := os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) + @@ -91,7 +91,7 @@ fn test_literal_strings() { // See `.gitattributes` in the project root for the rule in action. // These lines would look like this on Windows: // assert toml_doc.value('ml_lit1').string() == '\r\n\\' - assert toml_doc.value('ml_lit1').string() == '\n\\' + assert toml_doc.value('ml_lit1').string() == '\\' assert toml_doc.value('ml_lit2').string() == '\\\n\\' assert toml_doc.value('ml_lit3').string() == '\\\ntricky\\\n' }