Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow triple quoted strings as format strings for [@]printf #5847

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,28 @@ end

_is_str_expr(ex) =
isa(ex,Expr) && ex.head==:macrocall && isa(ex.args[1],Symbol) &&
(ex.args[1] == :str || endswith(string(ex.args[1]),"_str"))
(ex.args[1] == :str || endswith(string(ex.args[1]),"str"))

#TODO: remove when triple quotes parses to a string and not a @mstr macrocall
# and the following line fails with an exception
:("""hi""").head
function _convert_mstr_macro(ex)
!isa(ex, Expr) && return ex
if ex.head == :macrocall &&
ex.args[1] == symbol("@mstr") &&
length(ex.args) == 2 &&
isa(ex.args[2], String)
return ex.args[2]
end
ex
end

macro printf(args...)
if length(args) == 0
error("@printf: called with zero arguments")
end
#TODO: remove when @mstr macrocall gets removed
args = map(_convert_mstr_macro, args)
if !isa(args[1],String) && !(length(args) > 1 && isa(args[2],String))
if _is_str_expr(args[1]) || length(args) > 1 && _is_str_expr(args[2])
error("format must be a plain static string (no interpolation or prefix)")
Expand Down