From 2314ea67b9a9bdfbfd3d92e75ac1ec13a75a3276 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Tue, 9 Jul 2024 18:59:02 +0000 Subject: [PATCH] Use triple quotes in TOML.print when string contains newline (#55084) closes #55083 Shouldu this also check for `\r`? --------- Co-authored-by: Alex Arslan (cherry picked from commit e732706bbcff002860d88aee64a8113311529252) --- stdlib/TOML/src/print.jl | 5 +++-- stdlib/TOML/test/print.jl | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/stdlib/TOML/src/print.jl b/stdlib/TOML/src/print.jl index 168a7f63c6b5b..63f65b017d393 100644 --- a/stdlib/TOML/src/print.jl +++ b/stdlib/TOML/src/print.jl @@ -101,9 +101,10 @@ function printvalue(f::MbyFunc, io::IO, value::TOMLValue, sorted::Bool) value isa AbstractFloat ? Base.print(io, isnan(value) ? "nan" : isinf(value) ? string(value > 0 ? "+" : "-", "inf") : Float64(value)) : # TOML specifies IEEE 754 binary64 for float - value isa AbstractString ? (Base.print(io, "\""); + value isa AbstractString ? (qmark = Base.contains(value, "\n") ? "\"\"\"" : "\""; + Base.print(io, qmark); print_toml_escaped(io, value); - Base.print(io, "\"")) : + Base.print(io, qmark)) : value isa AbstractDict ? print_inline_table(f, io, value, sorted) : error("internal error in TOML printing, unhandled value") end diff --git a/stdlib/TOML/test/print.jl b/stdlib/TOML/test/print.jl index 79a87b9f0f13f..8fba1b1c1df10 100644 --- a/stdlib/TOML/test/print.jl +++ b/stdlib/TOML/test/print.jl @@ -195,3 +195,13 @@ LocalPkg = {path = "LocalPkg"} """ @test toml_str(d; sorted=true, inline_tables) == s @test roundtrip(s) + +# multiline strings (#55083) +s = """ +a = \"\"\"lorem ipsum + + + +alpha\"\"\" +""" +@test roundtrip(s)