Skip to content

Commit

Permalink
Rewrite Printf module
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Aug 13, 2019
1 parent 40b17f1 commit 036d510
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
36 changes: 0 additions & 36 deletions base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,29 +541,6 @@ function format(f::Format, args...) # => String
return unsafe_string(pointer(buf), pos-1)
end

"""
@printf([io::IOStream], "%Fmt", args...)
Print `args` using C `printf` style format specification string, with some caveats:
`Inf` and `NaN` are printed consistently as `Inf` and `NaN` for flags `%a`, `%A`,
`%e`, `%E`, `%f`, `%F`, `%g`, and `%G`. Furthermore, if a floating point number is
equally close to the numeric values of two possible output strings, the output
string further away from zero is chosen.
Optionally, an [`IOStream`](@ref)
may be passed as the first argument to redirect output.
See also: [`@sprintf`](@ref)
# Examples
```jldoctest
julia> @printf("%f %F %f %F\\n", Inf, Inf, NaN, NaN)
Inf Inf NaN NaN\n
julia> @printf "%.0f %.1f %f\\n" 0.5 0.025 -0.0078125
1 0.0 -0.007813
```
"""
macro printf(io_or_fmt, args...)
if io_or_fmt isa String
io = stdout
Expand All @@ -577,19 +554,6 @@ macro printf(io_or_fmt, args...)
end
end

"""
@sprintf("%Fmt", args...)
Return `@printf` formatted output as string.
# Examples
```jldoctest
julia> s = @sprintf "this is a %s %15.1f" "test" 34.567;
julia> println(s)
this is a test 34.6
```
"""
macro sprintf(fmt, args...)
f = Format(fmt)
return esc(:(Base.Printf.format($f, $(args...))))
Expand Down
4 changes: 3 additions & 1 deletion contrib/generate_precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ function generate_precompile_statements()
# Extract the precompile statements from stderr
statements = Set{String}()
for statement in eachline(precompile_file_h)
occursin("Main.", statement) && continue
(occursin("Main.", statement) ||
occursin("Printf.", statement) ||
occursin("Base.io_has_tvar_name", statement)) && continue
# check for `#x##s66` style variable names not in quotes
occursin(r"#\w", statement) &&
count(r"(?:#+\w+)+", statement) !=
Expand Down
40 changes: 40 additions & 0 deletions stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,44 @@ module Printf
export @printf, @sprintf
using Base.Printf

"""
@printf([io::IOStream], "%Fmt", args...)
Print `args` using C `printf` style format specification string, with some caveats:
`Inf` and `NaN` are printed consistently as `Inf` and `NaN` for flags `%a`, `%A`,
`%e`, `%E`, `%f`, `%F`, `%g`, and `%G`. Furthermore, if a floating point number is
equally close to the numeric values of two possible output strings, the output
string further away from zero is chosen.
Optionally, an [`IOStream`](@ref)
may be passed as the first argument to redirect output.
See also: [`@sprintf`](@ref)
# Examples
```jldoctest
julia> @printf("%f %F %f %F\\n", Inf, Inf, NaN, NaN)
Inf Inf NaN NaN\n
julia> @printf "%.0f %.1f %f\\n" 0.5 0.025 -0.0078125
1 0.0 -0.007813
```
"""
:(@printf)

"""
@sprintf("%Fmt", args...)
Return `@printf` formatted output as string.
# Examples
```jldoctest
julia> s = @sprintf "this is a %s %15.1f" "test" 34.567;
julia> println(s)
this is a test 34.6
```
"""
:(@sprintf)

end # module

0 comments on commit 036d510

Please sign in to comment.