/
Printf.jl
69 lines (56 loc) · 2.3 KB
/
Printf.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# This file is a part of Julia. License is MIT: https://julialang.org/license
module Printf
# the macro implementations here exactly mirrors the
# macros left in base/printf.jl, and uses the utility there
export @printf, @sprintf
using Base.Printf: _printf, is_str_expr, fix_dec, DIGITS, DIGITSs, print_fixed, print_fixed_width, decode_dec, decode_hex,
ini_hex, ini_HEX, print_exp_a, decode_0ct, decode_HEX, ini_dec, print_exp_e,
decode_oct, _limit, SmallNumber
using Unicode: textwidth
"""
@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(args...)
isempty(args) && throw(ArgumentError("@printf: called with no arguments"))
if isa(args[1], AbstractString) || is_str_expr(args[1])
_printf("@printf", :stdout, args[1], args[2:end])
else
(length(args) >= 2 && (isa(args[2], AbstractString) || is_str_expr(args[2]))) ||
throw(ArgumentError("@printf: first or second argument must be a format string"))
_printf("@printf", esc(args[1]), args[2], args[3:end])
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(args...)
isempty(args) && throw(ArgumentError("@sprintf: called with zero arguments"))
isa(args[1], AbstractString) || is_str_expr(args[1]) ||
throw(ArgumentError("@sprintf: first argument must be a format string"))
letexpr = _printf("@sprintf", :(IOBuffer()), args[1], args[2:end])
push!(letexpr.args[2].args, :(String(take!(out))))
letexpr
end
end # module