Skip to content

Commit

Permalink
Use triple quotes in TOML.print when string contains newline (#55084)
Browse files Browse the repository at this point in the history
closes #55083 

Shouldu this also check for `\r`?

---------

Co-authored-by: Alex Arslan <ararslan@comcast.net>
  • Loading branch information
palday and ararslan authored Jul 9, 2024
1 parent d7609d8 commit e732706
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions stdlib/TOML/src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions stdlib/TOML/test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e732706

Please sign in to comment.