Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Basic implementation of (s)printf using @(s)printf macros. #15

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
39 changes: 39 additions & 0 deletions src/cformat.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
###########
#
# Implementation of printf() and sprintf() that uses generated functions
# and the existing @printf and @sprintf macros.
#
# Original work by Tim Holy. Minor edits by Daniel Carrera.
#
immutable FormatString{S} end

FormatString(str::AbstractString) = FormatString{symbol(str)}

@generated function Base.print{format}(::Type{FormatString{format}}, args...)
meta = Expr(:meta, :inline)
fmt = string(format)
allargs = [:(args[$d]) for d = 1:length(args)]
quote
@printf($fmt, $(allargs...))
end
end
@generated function Base.sprint{format}(::Type{FormatString{format}}, args...)
meta = Expr(:meta, :inline)
fmt = string(format)
allargs = [:(args[$d]) for d = 1:length(args)]
quote
@sprintf($fmt, $(allargs...))
end
end

#
# USAGE: printf("%6d %7.2f", 220/7, 22/7)
#
function printf(s::AbstractString, args...)
print(FormatString(s), args...)
end
function sprintf(s::AbstractString, args...)
print(FormatString(s), args...)
end
###########

formatters = Dict{ ASCIIString, Function }()

function sprintf1( fmt::ASCIIString, x )
Expand Down