• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37630

23 Sep 2023 06:01PM UTC coverage: 85.958% (-0.5%) from 86.504%
#37630

push

local

web-flow
Fix error in comment [NFC] (#51181)

72248 of 84050 relevant lines covered (85.96%)

11837257.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.33
/stdlib/Printf/src/Printf.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
module Printf
4

5
using Base.Ryu
6

7
export @printf, @sprintf
8

9
# format specifier categories
10
const Ints = Union{Val{'d'}, Val{'i'}, Val{'u'}, Val{'x'}, Val{'X'}, Val{'o'}}
11
const Floats = Union{Val{'e'}, Val{'E'}, Val{'f'}, Val{'F'}, Val{'g'}, Val{'G'}, Val{'a'}, Val{'A'}}
12
const Chars = Union{Val{'c'}, Val{'C'}}
13
const Strings = Union{Val{'s'}, Val{'S'}}
14
const Pointer = Val{'p'}
15
const HexBases = Union{Val{'x'}, Val{'X'}, Val{'a'}, Val{'A'}}
16
const PositionCounter = Val{'n'}
17

18
const MAX_FRACTIONAL_PART_WIDTH = 17  # max significant decimals + 1: `ceil(Int, log10(1 / eps(Float64))) + 1`
19
const MAX_INTEGER_PART_WIDTH = 309  # max exponent: `ceil(Int, log10(prevfloat(typemax(Float64))))`
20
const MAX_FMT_CHARS_WIDTH = 5  # hash | sign +/- | decimal dot | exponent e/E | exponent sign
21

22
"""
23
Typed representation of a format specifier.
24

25
`T` is a `Val{'_'}`, where `_` is a valid format specifier character.
26

27
Fields are the various modifiers allowed for various format specifiers.
28
"""
29
struct Spec{T} # T => %type => Val{'type'}
30
    leftalign::Bool
2,379✔
31
    plus::Bool
32
    space::Bool
33
    zero::Bool
34
    hash::Bool
35
    width::Int
36
    precision::Int
37
    dynamic_width::Bool
38
    dynamic_precision::Bool
39
end
40

41
# recreate the format specifier string from a typed Spec
42
Base.string(f::Spec{T}; modifier::String="") where {T} =
×
43
    string("%",
44
           f.leftalign ? "-" : "",
45
           f.plus ? "+" : "",
46
           f.space ? " " : "",
47
           f.zero ? "0" : "",
48
           f.hash ? "#" : "",
49
           f.dynamic_width ? "*" : (f.width > 0 ? f.width : ""),
50
           f.dynamic_precision ? ".*" : (f.precision == 0 ? ".0" : (f.precision > 0 ? ".$(f.precision)" : "")),
51
           modifier,
52
           char(T))
53

54
Base.show(io::IO, f::Spec) = print(io, string(f))
×
55

56
floatfmt(s::Spec{T}) where {T} =
×
57
    Spec{Val{'f'}}(s.leftalign, s.plus, s.space, s.zero, s.hash, s.width, 0, s.dynamic_width, s.dynamic_precision)
58
ptrfmt(s::Spec{T}, x) where {T} =
×
59
    Spec{Val{'x'}}(s.leftalign, s.plus, s.space, s.zero, true, s.width, sizeof(x) == 8 ? 16 : 8, s.dynamic_width, s.dynamic_precision)
60

61
"""
62
    Printf.Format(format_str)
63

64
Create a C printf-compatible format object that can be used for formatting values.
65

66
The input `format_str` can include any valid format specifier character and modifiers.
67

68
A `Format` object can be passed to `Printf.format(f::Format, args...)` to produce a
69
formatted string, or `Printf.format(io::IO, f::Format, args...)` to print the
70
formatted string directly to `io`.
71

72
For convenience, the `Printf.format"..."` string macro form can be used for building
73
a `Printf.Format` object at macro-expansion-time.
74

75
!!! compat "Julia 1.6"
76
    `Printf.Format` requires Julia 1.6 or later.
77
"""
78
struct Format{S, T}
79
    str::S # original full format string as CodeUnits
29✔
80
    # keep track of non-format specifier strings to print
81
    # length(substringranges) == length(formats) + 1
82
    # so when printing, we start with printing
83
      # str[substringranges[1]], then formats[1] + args[1]
84
      # then str[substringranges[2]], then formats[2] + args[2]
85
      # and so on, then at the end, str[substringranges[end]]
86
    substringranges::Vector{UnitRange{Int}}
87
    formats::T # Tuple of Specs
88
    numarguments::Int  # required for dynamic format specifiers
89
end
90

91
# what number base should be used for a given format specifier?
92
base(T) = T <: HexBases ? 16 : T <: Val{'o'} ? 8 : 10
322✔
93
char(::Type{Val{c}}) where {c} = c
×
94

95
struct InvalidFormatStringError <: Exception
96
    message::String
97
    format::String
98
    start_color::Int
99
    end_color::Int
100
end
101

102
function Base.showerror(io::IO, err::InvalidFormatStringError)
×
103
    io_has_color = get(io, :color, false)::Bool
×
104

105
    println(io, "InvalidFormatStringError: ", err.message)
×
106
    print(io, "    \"", @view(err.format[begin:prevind(err.format, err.start_color)]))
×
107
    invalid_text = @view err.format[err.start_color:err.end_color]
×
108

109
    printstyled(io, invalid_text, color=:red)
×
110

111
    # +1 is okay, since all format characters are single bytes
112
    println(io, @view(err.format[err.end_color+1:end]), "\"")
×
113

114
    arrow_error = '-'^(length(invalid_text)-1)
×
115
    arrow = "    " * ' '^err.start_color * arrow_error * "^\n"
×
116
    if io_has_color
×
117
        printstyled(io, arrow, color=:red)
×
118
    else
119
        print(io, arrow)
×
120
    end
121
end
122

123
# parse format string
124
function Format(f::AbstractString)
29✔
125
    bytes = codeunits(f)
29✔
126
    len = length(bytes)
29✔
127
    pos = 1
29✔
128
    numarguments = 0
29✔
129

130
    b = 0x00
29✔
131
    local last_percent_pos
×
132

133
    # skip ahead to first format specifier
134
    while pos <= len
311✔
135
        b = bytes[pos]
311✔
136
        pos += 1
311✔
137
        if b == UInt8('%')
311✔
138
            last_percent_pos = pos-1
29✔
139
            pos > len && throw(InvalidFormatStringError("Format specifier is incomplete", f, last_percent_pos, last_percent_pos))
29✔
140
            if bytes[pos] == UInt8('%')
29✔
141
                # escaped '%'
142
                b = bytes[pos]
×
143
                pos += 1
×
144
            else
145
                break
29✔
146
            end
147
        end
148
    end
282✔
149
    strs = [1:pos - 1 - (b == UInt8('%'))]
46✔
150
    fmts = []
29✔
151
    while pos <= len
82✔
152
        b = bytes[pos]
53✔
153
        pos += 1
53✔
154
        # positioned at start of first format str %
155
        # parse flags
156
        leftalign = plus = space = zero = hash = false
53✔
157
        while true
53✔
158
            if b == UInt8('-')
53✔
159
                leftalign = true
×
160
            elseif b == UInt8('+')
53✔
161
                plus = true
×
162
            elseif b == UInt8(' ')
53✔
163
                space = true
×
164
            elseif b == UInt8('0')
53✔
165
                zero = true
×
166
            elseif b == UInt8('#')
53✔
167
                hash = true
×
168
            else
169
                break
53✔
170
            end
171
            pos > len && throw(InvalidFormatStringError("Format specifier is incomplete", f, last_percent_pos, pos-1))
×
172
            b = bytes[pos]
×
173
            pos += 1
×
174
        end
×
175
        if leftalign
53✔
176
            zero = false
×
177
        end
178
        # parse width
179
        width = 0
53✔
180
        dynamic_width = false
53✔
181
        if b == UInt8('*')
53✔
182
            dynamic_width = true
×
183
            numarguments += 1
×
184
            b = bytes[pos]
×
185
            pos += 1
×
186
        else
187
            while b - UInt8('0') < 0x0a
63✔
188
            width = 10 * width + (b - UInt8('0'))
10✔
189
                b = bytes[pos]
10✔
190
                pos += 1
10✔
191
                pos > len && break
10✔
192
            end
10✔
193
        end
194
        # parse precision
195
        precision = 0
53✔
196
        parsedprecdigits = false
53✔
197
        dynamic_precision = false
53✔
198
        if b == UInt8('.')
53✔
199
            pos > len && throw(InvalidFormatStringError("Precision specifier is missing precision", f, last_percent_pos, pos-1))
11✔
200
            parsedprecdigits = true
11✔
201
            b = bytes[pos]
11✔
202
            pos += 1
11✔
203
            if pos <= len
11✔
204
                if b == UInt8('*')
11✔
205
                    dynamic_precision = true
×
206
                    numarguments += 1
×
207
                    b = bytes[pos]
×
208
                    pos += 1
×
209
                else
210
                    precision = 0
11✔
211
                    while b - UInt8('0') < 0x0a
11✔
212
                        precision = 10precision + (b - UInt8('0'))
11✔
213
                        b = bytes[pos]
11✔
214
                        pos += 1
11✔
215
                        pos > len && break
11✔
216
                    end
×
217
                end
218
            end
219
        end
220
        # parse length modifier (ignored)
221
        if b == UInt8('h') || b == UInt8('l')
106✔
222
            prev = b
×
223
            pos > len && throw(InvalidFormatStringError("Length modifier is missing type specifier", f, last_percent_pos, pos-1))
×
224
            b = bytes[pos]
×
225
            pos += 1
×
226
            if b == prev
×
227
                pos > len && throw(InvalidFormatStringError("Length modifier is missing type specifier", f, last_percent_pos, pos-1))
×
228
                b = bytes[pos]
×
229
                pos += 1
×
230
            end
231
        elseif b in b"Ljqtz" # q was a synonym for ll above, see `man 3 printf`. Not to be used.
53✔
232
            pos > len && throw(InvalidFormatStringError("Length modifier is missing type specifier", f, last_percent_pos, pos-1))
42✔
233
            b = bytes[pos]
42✔
234
            pos += 1
42✔
235
        end
236
        # parse type
237
        !(b in b"diouxXDOUeEfFgGaAcCsSpn") && throw(InvalidFormatStringError("'$(Char(b))' is not a valid type specifier", f, last_percent_pos, pos-1))
53✔
238
        type = Val{Char(b)}
53✔
239
        if type <: Ints && precision > 0
53✔
240
            # note - we should also set zero to false if dynamic precison > 0
241
            # this is taken care of in fmt() for Ints
242
            zero = false
×
243
        elseif (type <: Strings || type <: Chars) && !parsedprecdigits
106✔
244
            precision = -1
×
245
        elseif type <: Union{Val{'a'}, Val{'A'}} && !parsedprecdigits
53✔
246
            precision = -1
×
247
        elseif type <: Floats && !parsedprecdigits
53✔
248
            precision = 6
×
249
        end
250
        numarguments += 1
53✔
251
        push!(fmts, Spec{type}(leftalign, plus, space, zero, hash, width, precision, dynamic_width, dynamic_precision))
53✔
252
        start = pos
53✔
253
        while pos <= len
335✔
254
            b = bytes[pos]
306✔
255
            pos += 1
306✔
256
            if b == UInt8('%')
306✔
257
                last_percent_pos = pos-1
24✔
258
                pos > len && throw(InvalidFormatStringError("Format specifier is incomplete", f, last_percent_pos, last_percent_pos))
24✔
259
                if bytes[pos] == UInt8('%')
24✔
260
                    # escaped '%'
261
                    b = bytes[pos]
×
262
                    pos += 1
×
263
                else
264
                    break
24✔
265
                end
266
            end
267
        end
282✔
268
        push!(strs, start:pos - 1 - (b == UInt8('%')))
64✔
269
    end
53✔
270
    return Format(bytes, strs, Tuple(fmts), numarguments)
29✔
271
end
272

273
macro format_str(str)
274
    Format(str)
275
end
276

277
const hex = b"0123456789abcdef"
278
const HEX = b"0123456789ABCDEF"
279

280
# write out a single arg according to format options
281
# char
282
@inline function writechar(buf, pos, c)
×
283
    u = bswap(reinterpret(UInt32, c))
×
284
    while true
×
285
        buf[pos] = u % UInt8
×
286
        pos += 1
×
287
        (u >>= 8) == 0 && break
×
288
    end
×
289
    return pos
×
290
end
291

292

293
@inline function rmdynamic(spec::Spec{T}, args, argp) where {T}
2,326✔
294
    zero, width, precision = spec.zero, spec.width, spec.precision
2,346✔
295
    if spec.dynamic_width
2,346✔
296
        width = args[argp]
×
297
        argp += 1
×
298
    end
299
    if spec.dynamic_precision
2,346✔
300
        precision = args[argp]
×
301
        if zero && T <: Ints && precision > 0
×
302
            zero = false
×
303
        end
304
        argp += 1
×
305
    end
306
    (Spec{T}(spec.leftalign, spec.plus, spec.space, zero, spec.hash, width, precision, false, false), argp)
2,346✔
307
end
308

309
@inline function fmt(buf, pos, args, argp, spec::Spec{T}) where {T}
1,163✔
310
    spec, argp = rmdynamic(spec, args, argp)
1,173✔
311
    (fmt(buf, pos, args[argp], spec), argp+1)
1,173✔
312
end
313

314
@inline function fmt(buf, pos, arg, spec::Spec{T}) where {T <: Chars}
×
315
    leftalign, width = spec.leftalign, spec.width
×
316
    c = Char(first(arg))
×
317
    w = textwidth(c)
×
318
    if !leftalign && width > w
×
319
        for _ = 1:(width - w)
×
320
            buf[pos] = UInt8(' ')
×
321
            pos += 1
×
322
        end
×
323
    end
324
    pos = writechar(buf, pos, c)
×
325
    if leftalign && width > w
×
326
        for _ = 1:(width - w)
×
327
            buf[pos] = UInt8(' ')
×
328
            pos += 1
×
329
        end
×
330
    end
331
    return pos
×
332
end
333

334
# strings
335
@inline function fmt(buf, pos, arg, spec::Spec{T}) where {T <: Strings}
×
336
    leftalign, hash, width, prec = spec.leftalign, spec.hash, spec.width, spec.precision
×
337
    str = string(arg)
×
338
    slen = textwidth(str)::Int + (hash ? arg isa AbstractString ? 2 : 1 : 0)
×
339
    op = p = prec == -1 ? slen : min(slen, prec)
×
340
    if !leftalign && width > p
×
341
        for _ = 1:(width - p)
×
342
            buf[pos] = UInt8(' ')
×
343
            pos += 1
×
344
        end
×
345
    end
346
    if hash
×
347
        if arg isa Symbol
×
348
            buf[pos] = UInt8(':')
×
349
            pos += 1
×
350
            p -= 1
×
351
        elseif arg isa AbstractString
×
352
            buf[pos] = UInt8('"')
×
353
            pos += 1
×
354
            p -= 1
×
355
        end
356
    end
357
    for c in str
×
358
        p -= textwidth(c)
×
359
        p < 0 && break
×
360
        pos = writechar(buf, pos, c)
×
361
    end
×
362
    if hash && arg isa AbstractString && p > 0
×
363
        buf[pos] = UInt8('"')
×
364
        pos += 1
×
365
    end
366
    if leftalign && width > op
×
367
        for _ = 1:(width - op)
×
368
            buf[pos] = UInt8(' ')
×
369
            pos += 1
×
370
        end
×
371
    end
372
    return pos
×
373
end
374

375
# integers
376
toint(x) = x
322✔
377
toint(x::Rational) = Integer(x)
×
378

379
fmt(buf, pos, arg::AbstractFloat, spec::Spec{T}) where {T <: Ints} =
×
380
    fmt(buf, pos, arg, floatfmt(spec))
381

382
@inline function fmt(buf, pos, arg, spec::Spec{T}) where {T <: Ints}
161✔
383
    leftalign, plus, space, zero, hash, width, prec =
161✔
384
        spec.leftalign, spec.plus, spec.space, spec.zero, spec.hash, spec.width, spec.precision
385
    bs = base(T)
161✔
386
    arg2 = toint(arg)
161✔
387
    n = i = ndigits(arg2, base=bs, pad=1)
161✔
388
    neg = arg2 < 0
161✔
389
    x = arg2 isa Base.BitSigned ? unsigned(abs(arg2)) : abs(arg2)
161✔
390
    arglen = n + (neg || (plus | space)) +
319✔
391
        (T == Val{'o'} && hash ? 1 : 0) +
392
        (T == Val{'x'} && hash ? 2 : 0) + (T == Val{'X'} && hash ? 2 : 0)
393
    arglen2 = arglen < width && prec > 0 ? arglen + min(max(0, prec - n), width - arglen) : arglen
161✔
394
    if !leftalign && !zero && arglen2 < width
161✔
395
        # pad left w/ spaces
396
        for _ = 1:(width - arglen2)
×
397
            buf[pos] = UInt8(' ')
×
398
            pos += 1
×
399
        end
×
400
    end
401
    if neg
161✔
402
        buf[pos] = UInt8('-'); pos += 1
6✔
403
    elseif plus # plus overrides space
158✔
404
        buf[pos] = UInt8('+'); pos += 1
8✔
405
    elseif space
154✔
406
        buf[pos] = UInt8(' '); pos += 1
×
407
    end
408
    if T == Val{'o'} && hash
161✔
409
        buf[pos] = UInt8('0')
×
410
        pos += 1
×
411
    elseif T == Val{'x'} && hash
161✔
412
        buf[pos] = UInt8('0')
×
413
        buf[pos + 1] = UInt8('x')
×
414
        pos += 2
×
415
    elseif T == Val{'X'} && hash
161✔
416
        buf[pos] = UInt8('0')
×
417
        buf[pos + 1] = UInt8('X')
×
418
        pos += 2
×
419
    end
420
    if zero && arglen2 < width
161✔
421
        for _ = 1:(width - arglen2)
18✔
422
            buf[pos] = UInt8('0')
11✔
423
            pos += 1
11✔
424
        end
13✔
425
    elseif n < prec
152✔
426
        for _ = 1:(prec - n)
×
427
            buf[pos] = UInt8('0')
×
428
            pos += 1
×
429
        end
×
430
    elseif arglen < arglen2
152✔
431
        for _ = 1:(arglen2 - arglen)
×
432
            buf[pos] = UInt8('0')
×
433
            pos += 1
×
434
        end
×
435
    end
436
    while i > 0
798✔
437
        @inbounds buf[pos + i - 1] = bs == 16 ?
637✔
438
            (T == Val{'x'} ? hex[(x & 0x0f) + 1] : HEX[(x & 0x0f) + 1]) :
439
            (48 + (bs == 8 ? (x & 0x07) : rem(x, 10)))
440
        if bs == 8
637✔
441
            x >>= 3
×
442
        elseif bs == 16
637✔
443
            x >>= 4
×
444
        else
445
            x = oftype(x, div(x, 10))
637✔
446
        end
447
        i -= 1
637✔
448
    end
637✔
449
    pos += n
161✔
450
    if leftalign && arglen2 < width
161✔
451
        # pad right
452
        for _ = 1:(width - arglen2)
×
453
            buf[pos] = UInt8(' ')
×
454
            pos += 1
×
455
        end
×
456
    end
457
    return pos
161✔
458
end
459

460
# floats
461
"""
462
    Printf.tofloat(x)
463

464
Convert an argument to a Base float type for printf formatting.
465
By default, arguments are converted to `Float64` via `Float64(x)`.
466
Custom numeric types that have a conversion to a Base float type
467
that wish to hook into printf formatting can extend this method like:
468

469
```julia
470
Printf.tofloat(x::MyCustomType) = convert_my_custom_type_to_float(x)
471
```
472

473
For arbitrary precision numerics, you might extend the method like:
474

475
```julia
476
Printf.tofloat(x::MyArbitraryPrecisionType) = BigFloat(x)
477
```
478

479
!!! compat "Julia 1.6"
480
    This function requires Julia 1.6 or later.
481
"""
482
tofloat(x) = Float64(x)
×
483
tofloat(x::Base.IEEEFloat) = x
1,002✔
484
tofloat(x::BigFloat) = x
×
485

486
_snprintf(ptr, siz, str, arg) =
×
487
    @ccall "libmpfr".mpfr_snprintf(ptr::Ptr{UInt8}, siz::Csize_t, str::Ptr{UInt8};
×
488
                                   arg::Ref{BigFloat})::Cint
489

490
# Arbitrary constant for a maximum number of bytes we want to output for a BigFloat.
491
# 8KiB seems like a reasonable default. Larger BigFloat representations should probably
492
# use a custom printing routine. Printing values with results larger than this ourselves
493
# seems like a dangerous thing to do.
494
const __BIG_FLOAT_MAX__ = 8192
495

496
@inline function fmt(buf, pos, arg, spec::Spec{T}) where {T <: Floats}
1,002✔
497
    leftalign, plus, space, zero, hash, width, prec =
1,002✔
498
        spec.leftalign, spec.plus, spec.space, spec.zero, spec.hash, spec.width, spec.precision
499
    x = tofloat(arg)
1,002✔
500
    if x isa BigFloat
1,002✔
501
        if isfinite(x)
×
502
            GC.@preserve buf begin
×
503
                siz = length(buf) - pos + 1
×
504
                str = string(spec; modifier="R")
×
505
                required_length = _snprintf(pointer(buf, pos), siz, str, x)
×
506
                if required_length > siz
×
507
                    required_length > __BIG_FLOAT_MAX__ &&
×
508
                        throw(ArgumentError("The given BigFloat requires $required_length bytes to be printed, which is more than the maximum of $__BIG_FLOAT_MAX__ bytes supported."))
509
                    resize!(buf, required_length + 1)
×
510
                    required_length = _snprintf(pointer(buf, pos), required_length + 1, str, x)
×
511
                end
512
                required_length > 0 || throw(ArgumentError("The given BigFloat would produce less than the maximum allowed number of bytes $__BIG_FLOAT_MAX__, but still couldn't be printed fully for an unknown reason."))
×
513
                return pos + required_length
×
514
            end
515
        end
516
        x = Float64(x)
×
517
    end
518
    if T == Val{'e'} || T == Val{'E'}
1,002✔
519
        newpos = Ryu.writeexp(buf, pos, x, prec, plus, space, hash, char(T), UInt8('.'))
×
520
    elseif T == Val{'f'} || T == Val{'F'}
1,002✔
521
        newpos = Ryu.writefixed(buf, pos, x, prec, plus, space, hash, UInt8('.'))
1,012✔
522
    elseif T == Val{'g'} || T == Val{'G'}
×
523
        if isinf(x) || isnan(x)
×
524
            newpos = Ryu.writeshortest(buf, pos, x, plus, space)
×
525
        else
526
            # C11-compliant general format
527
            prec = prec == 0 ? 1 : prec
×
528
            # format the value in scientific notation and parse the exponent part
529
            exp = let p = Ryu.writeexp(buf, pos, x, prec)
×
530
                b1, b2, b3, b4 = buf[p-4], buf[p-3], buf[p-2], buf[p-1]
×
531
                Z = UInt8('0')
×
532
                if b1 == UInt8('e')
×
533
                    # two-digit exponent
534
                    sign = b2 == UInt8('+') ? 1 : -1
×
535
                    exp = 10 * (b3 - Z) + (b4 - Z)
×
536
                else
537
                    # three-digit exponent
538
                    sign = b1 == UInt8('+') ? 1 : -1
×
539
                    exp = 100 * (b2 - Z) + 10 * (b3 - Z) + (b4 - Z)
×
540
                end
541
                flipsign(exp, sign)
×
542
            end
543
            if -4 ≤ exp < prec
×
544
                newpos = Ryu.writefixed(buf, pos, x, prec - (exp + 1), plus, space, hash, UInt8('.'), !hash)
×
545
            else
546
                newpos = Ryu.writeexp(buf, pos, x, prec - 1, plus, space, hash, T == Val{'g'} ? UInt8('e') : UInt8('E'), UInt8('.'), !hash)
×
547
            end
548
        end
549
    elseif T == Val{'a'} || T == Val{'A'}
×
550
        x, neg = x < 0 || x === -Base.zero(x) ? (-x, true) : (x, false)
×
551
        newpos = pos
×
552
        if neg
×
553
            buf[newpos] = UInt8('-')
×
554
            newpos += 1
×
555
        elseif plus
×
556
            buf[newpos] = UInt8('+')
×
557
            newpos += 1
×
558
        elseif space
×
559
            buf[newpos] = UInt8(' ')
×
560
            newpos += 1
×
561
        end
562
        if isnan(x)
×
563
            buf[newpos] = UInt8('N')
×
564
            buf[newpos + 1] = UInt8('a')
×
565
            buf[newpos + 2] = UInt8('N')
×
566
            newpos += 3
×
567
        elseif !isfinite(x)
×
568
            buf[newpos] = UInt8('I')
×
569
            buf[newpos + 1] = UInt8('n')
×
570
            buf[newpos + 2] = UInt8('f')
×
571
            newpos += 3
×
572
        else
573
            buf[newpos] = UInt8('0')
×
574
            newpos += 1
×
575
            buf[newpos] = T <: Val{'a'} ? UInt8('x') : UInt8('X')
×
576
            newpos += 1
×
577
            if arg == 0
×
578
                buf[newpos] = UInt8('0')
×
579
                newpos += 1
×
580
                if prec > 0
×
581
                    buf[newpos] = UInt8('.')
×
582
                    newpos += 1
×
583
                    while prec > 0
×
584
                        buf[newpos] = UInt8('0')
×
585
                        newpos += 1
×
586
                        prec -= 1
×
587
                    end
×
588
                end
589
                buf[newpos] = T <: Val{'a'} ? UInt8('p') : UInt8('P')
×
590
                buf[newpos + 1] = UInt8('+')
×
591
                buf[newpos + 2] = UInt8('0')
×
592
                newpos += 3
×
593
            else
594
                if prec > -1
×
595
                    s, p = frexp(x)
×
596
                    sigbits = 4 * min(prec, 13)
×
597
                    s = 0.25 * round(ldexp(s, 1 + sigbits))
×
598
                    # ensure last 2 exponent bits either 01 or 10
599
                    u = (reinterpret(UInt64, s) & 0x003f_ffff_ffff_ffff) >> (52 - sigbits)
×
600
                    i = n = (sizeof(u) << 1) - (leading_zeros(u) >> 2)
×
601
                else
602
                    s, p = frexp(x)
×
603
                    s *= 2.0
×
604
                    u = (reinterpret(UInt64, s) & 0x001f_ffff_ffff_ffff)
×
605
                    t = (trailing_zeros(u) >> 2)
×
606
                    u >>= (t << 2)
×
607
                    i = n = 14 - t
×
608
                end
609
                frac = u > 9 || hash || prec > 0
×
610
                while i > 1
×
611
                    buf[newpos + i] = T == Val{'a'} ? hex[(u & 0x0f) + 1] : HEX[(u & 0x0f) + 1]
×
612
                    u >>= 4
×
613
                    i -= 1
×
614
                    prec -= 1
×
615
                end
×
616
                if frac
×
617
                    buf[newpos + 1] = UInt8('.')
×
618
                end
619
                buf[newpos] = T == Val{'a'} ? hex[(u & 0x0f) + 1] : HEX[(u & 0x0f) + 1]
×
620
                newpos += n + frac
×
621
                while prec > 0
×
622
                    buf[newpos] = UInt8('0')
×
623
                    newpos += 1
×
624
                    prec -= 1
×
625
                end
×
626
                buf[newpos] = T <: Val{'a'} ? UInt8('p') : UInt8('P')
×
627
                newpos += 1
×
628
                p -= 1
×
629
                buf[newpos] = p < 0 ? UInt8('-') : UInt8('+')
×
630
                p = p < 0 ? -p : p
×
631
                newpos += 1
×
632
                n = i = ndigits(p, base=10, pad=1)
×
633
                while i > 0
×
634
                    buf[newpos + i - 1] = 48 + rem(p, 10)
×
635
                    p = oftype(p, div(p, 10))
×
636
                    i -= 1
×
637
                end
×
638
                newpos += n
×
639
            end
640
        end
641
    end
642
    if newpos - pos < width
1,012✔
643
        # need to pad
644
        if leftalign
609✔
645
            # easy case, just pad spaces after number
646
            for _ = 1:(width - (newpos - pos))
×
647
                buf[newpos] = UInt8(' ')
×
648
                newpos += 1
×
649
            end
×
650
        else
651
            # right aligned
652
            n = width - (newpos - pos)
609✔
653
            if zero
609✔
654
                ex = (arg < 0 || (plus | space)) + (T <: Union{Val{'a'}, Val{'A'}} ? 2 : 0)
×
655
                so = pos + ex
×
656
                len = (newpos - pos) - ex
×
657
                copyto!(buf, so + n, buf, so, len)
×
658
                for i = so:(so + n - 1)
×
659
                    buf[i] = UInt8('0')
×
660
                end
×
661
                newpos += n
×
662
            else
663
                copyto!(buf, pos + n, buf, pos, newpos - pos)
609✔
664
                for i = pos:(pos + n - 1)
1,218✔
665
                    buf[i] = UInt8(' ')
827✔
666
                end
1,045✔
667
                newpos += n
609✔
668
            end
669
        end
670
    end
671
    return newpos
1,012✔
672
end
673

674
# pointers
675
fmt(buf, pos, arg, spec::Spec{Pointer}) = fmt(buf, pos, UInt64(arg), ptrfmt(spec, arg))
×
676

677
# position counters
678
function fmt(buf, pos, arg::Ref{<:Integer}, ::Spec{PositionCounter})
×
679
    arg[] = pos - 1
×
680
    pos
×
681
end
682

683
# old Printf compat
684
function fix_dec end
685
function ini_dec end
686

687
# generic fallback
688
function fmtfallback(buf, pos, arg, spec::Spec{T}) where {T}
×
689
    leftalign, plus, space, zero, hash, width, prec =
×
690
        spec.leftalign, spec.plus, spec.space, spec.zero, spec.hash, spec.width, spec.precision
691
    buf2 = Base.StringVector(
×
692
        MAX_INTEGER_PART_WIDTH + MAX_FRACTIONAL_PART_WIDTH + MAX_FMT_CHARS_WIDTH
693
    )
694
    ise = T <: Union{Val{'e'}, Val{'E'}}
×
695
    isg = T <: Union{Val{'g'}, Val{'G'}}
×
696
    isf = T <: Val{'f'}
×
697
    if isg
×
698
        prec = prec == 0 ? 1 : prec
×
699
        arg = round(arg, sigdigits=prec)
×
700
    end
701
    n, pt, neg = isf ? fix_dec(arg, prec, buf2) : ini_dec(arg, min(prec + ise, length(buf2) - 1), buf2)
×
702
    if isg && !hash
×
703
        while buf2[n] == UInt8('0')
×
704
            n -= 1
×
705
        end
×
706
    end
707
    expform = ise || (isg && !(-4 < pt <= prec))
×
708
    n2 = n + (expform ? 4 : 0) + (prec > 0 || hash) + (neg || (plus | space)) +
×
709
        (isf && pt >= n ? prec + 1 : 0)
710
    if !leftalign && !zero && n2 < width
×
711
        # pad left w/ spaces
712
        for _ = 1:(width - n2)
×
713
            buf[pos] = UInt8(' ')
×
714
            pos += 1
×
715
        end
×
716
    end
717
    if neg
×
718
        buf[pos] = UInt8('-'); pos += 1
×
719
    elseif plus # plus overrides space
×
720
        buf[pos] = UInt8('+'); pos += 1
×
721
    elseif space
×
722
        buf[pos] = UInt8(' '); pos += 1
×
723
    end
724
    if zero && n2 < width
×
725
        for _ = 1:(width - n2)
×
726
            buf[pos] = UInt8('0')
×
727
            pos += 1
×
728
        end
×
729
    end
730
    if expform
×
731
        buf[pos] = buf2[1]
×
732
        pos += 1
×
733
        if n > 1 || hash
×
734
            buf[pos] = UInt8('.')
×
735
            pos += 1
×
736
            for i = 2:n
×
737
                buf[pos] = buf2[i]
×
738
                pos += 1
×
739
            end
×
740
        end
741
        buf[pos] = T <: Val{'e'} || T <: Val{'g'} ? UInt8('e') : UInt8('E')
×
742
        pos += 1
×
743
        exp = pt - 1
×
744
        buf[pos] = exp < 0 ? UInt8('-') : UInt8('+')
×
745
        pos += 1
×
746
        exp = abs(exp)
×
747
        if exp < 10
×
748
            buf[pos] = UInt8('0')
×
749
            buf[pos + 1] = 48 + exp
×
750
            pos += 2
×
751
        else
752
            buf[pos] = 48 + div(exp, 10)
×
753
            buf[pos + 1] = 48 + rem(exp, 10)
×
754
            pos += 2
×
755
        end
756
    elseif pt <= 0
×
757
        buf[pos] = UInt8('0')
×
758
        buf[pos + 1] = UInt8('.')
×
759
        pos += 2
×
760
        while pt < 0
×
761
            buf[pos] = UInt8('0')
×
762
            pos += 1
×
763
            pt += 1
×
764
        end
×
765
        for i = 1:n
×
766
            buf[pos] = buf2[i]
×
767
            pos += 1
×
768
        end
×
769
    elseif pt >= n
×
770
        for i = 1:n
×
771
            buf[pos] = buf2[i]
×
772
            pos += 1
×
773
        end
×
774
        while pt > n
×
775
            buf[pos] = UInt8('0')
×
776
            pos += 1
×
777
            n += 1
×
778
        end
×
779
        if hash || (isf && prec > 0)
×
780
            buf[pos] = UInt8('.')
×
781
            pos += 1
×
782
            while prec > 0
×
783
                buf[pos] = UInt8('0')
×
784
                pos += 1
×
785
                prec -= 1
×
786
            end
×
787
        end
788
    else
789
        for i = 1:pt
×
790
            buf[pos] = buf2[i]
×
791
            pos += 1
×
792
        end
×
793
        buf[pos] = UInt8('.')
×
794
        pos += 1
×
795
        for i = pt+1:n
×
796
            buf[pos] = buf2[i]
×
797
            pos += 1
×
798
        end
×
799
    end
800

801
    if leftalign && n2 < width
×
802
        # pad right
803
        for _ = 1:(width - n2)
×
804
            buf[pos] = UInt8(' ')
×
805
            pos += 1
×
806
        end
×
807
    end
808
    return pos
×
809
end
810

811
const UNROLL_UPTO = 16
812
# if you have your own buffer + pos, write formatted args directly to it
813
@inline function format(buf::Vector{UInt8}, pos::Integer, f::Format, args...)
1,073✔
814
    # write out first substring
815
    escapechar = false
1,063✔
816
    for i in f.substringranges[1]
1,085✔
817
        b = f.str[i]
282✔
818
        if !escapechar
282✔
819
            buf[pos] = b
282✔
820
            pos += 1
282✔
821
            escapechar = b === UInt8('%')
282✔
822
        else
823
            escapechar = false
×
824
        end
825
    end
552✔
826
    # for each format, write out arg and next substring
827
    # unroll up to 16 formats
828
    N = length(f.formats)
1,073✔
829
    argp = 1
1,063✔
830
    Base.@nexprs 16 i -> begin
×
831
        if N >= i
17,018✔
832
            pos, argp = fmt(buf, pos, args, argp, f.formats[i])
1,173✔
833
            for j in f.substringranges[i + 1]
1,327✔
834
                b = f.str[j]
1,258✔
835
                if !escapechar
1,258✔
836
                    buf[pos] = b
1,258✔
837
                    pos += 1
1,258✔
838
                    escapechar = b === UInt8('%')
1,258✔
839
                else
840
                    escapechar = false
×
841
                end
842
            end
2,362✔
843
        end
844
    end
845
    if N > 16
1,063✔
846
        for i = 17:length(f.formats)
×
847
            pos, argp = fmt(buf, pos, args, argp, f.formats[i])
×
848
            for j in f.substringranges[i + 1]
×
849
                b = f.str[j]
×
850
                if !escapechar
×
851
                    buf[pos] = b
×
852
                    pos += 1
×
853
                    escapechar = b === UInt8('%')
×
854
                else
855
                    escapechar = false
×
856
                end
857
            end
×
858
        end
×
859
    end
860
    return pos
1,073✔
861
end
862

863
@inline function plength(f::Spec{T}, args, argp) where {T}
1,163✔
864
    f, argp = rmdynamic(f, args, argp)
1,173✔
865
    (plength(f, args[argp]), argp+1)
1,173✔
866
end
867

868
function plength(f::Spec{T}, x) where {T <: Chars}
×
869
    c = Char(first(x))
×
870
    w = textwidth(c)
×
871
    return max(f.width, w) + (ncodeunits(c) - w)
×
872
end
873

874
plength(f::Spec{Pointer}, x) = max(f.width, 2 * sizeof(x) + 2)
×
875

876
function plength(f::Spec{T}, x) where {T <: Strings}
×
877
    str = string(x)
×
878
    sw = textwidth(str)
×
879
    p = f.precision == -1 ? (sw + (f.hash ? (x isa Symbol ? 1 : 2) : 0)) : f.precision
×
880
    return max(f.width, p) + (sizeof(str) - sw)
×
881
end
882

883
function plength(f::Spec{T}, x) where {T <: Ints}
161✔
884
    x2 = toint(x)
161✔
885
    return max(
161✔
886
        f.width,
887
        f.precision + ndigits(x2, base=base(T), pad=1) + MAX_FMT_CHARS_WIDTH
888
    )
889
end
890

891
plength(f::Spec{T}, x::AbstractFloat) where {T <: Ints} =
×
892
    max(f.width, f.hash + MAX_INTEGER_PART_WIDTH + 0 + MAX_FMT_CHARS_WIDTH)
893
plength(f::Spec{T}, x) where {T <: Floats} =
1,012✔
894
    max(f.width, f.hash + MAX_INTEGER_PART_WIDTH + f.precision + MAX_FMT_CHARS_WIDTH)
895
plength(::Spec{PositionCounter}, x) = 0
×
896

897
@inline function computelen(substringranges, formats, args)
1,063✔
898
    len = sum(length, substringranges)
2,146✔
899
    N = length(formats)
1,063✔
900
    # unroll up to 16 formats
901
    argp = 1
1,063✔
902
    Base.@nexprs 16 i -> begin
×
903
        if N >= i
17,008✔
904
            l, argp = plength(formats[i], args, argp)
1,173✔
905
            len += l
1,173✔
906
        end
907
    end
908
    if N > 16
1,063✔
909
        for i = 17:length(formats)
×
910
            l, argp = plength(formats[i], args, argp)
×
911
            len += l
×
912
        end
×
913
    end
914
    return len
1,073✔
915
end
916

917
@noinline argmismatch(a, b) =
×
918
    throw(ArgumentError("Number of format specifiers and number of provided args differ: $a != $b"))
919

920
"""
921
    Printf.format(f::Printf.Format, args...) => String
922
    Printf.format(io::IO, f::Printf.Format, args...)
923

924
Apply a printf format object `f` to provided `args` and return the formatted string
925
(1st method), or print directly to an `io` object (2nd method). See [`@printf`](@ref)
926
for more details on C `printf` support.
927
"""
928
function format end
929

930
function format(io::IO, f::Format, args...) # => Nothing
16✔
931
    f.numarguments == length(args) || argmismatch(f.numarguments, length(args))
16✔
932
    buf = Base.StringVector(computelen(f.substringranges, f.formats, args))
32✔
933
    pos = format(buf, 1, f, args...)
16✔
934
    write(io, resize!(buf, pos - 1))
32✔
935
    return
16✔
936
end
937

938
function format(f::Format, args...) # => String
1,057✔
939
    f.numarguments == length(args) || argmismatch(f.numarguments, length(args))
1,057✔
940
    buf = Base.StringVector(computelen(f.substringranges, f.formats, args))
1,102✔
941
    pos = format(buf, 1, f, args...)
1,057✔
942
    return String(resize!(buf, pos - 1))
1,057✔
943
end
944

945
"""
946
    @printf([io::IO], "%Fmt", args...)
947

948
Print `args` using C `printf` style format specification string.
949
Optionally, an `IO` may be passed as the first argument to redirect output.
950

951
# Examples
952
```jldoctest
953
julia> @printf "Hello %s" "world"
954
Hello world
955

956
julia> @printf "Scientific notation %e" 1.234
957
Scientific notation 1.234000e+00
958

959
julia> @printf "Scientific notation three digits %.3e" 1.23456
960
Scientific notation three digits 1.235e+00
961

962
julia> @printf "Decimal two digits %.2f" 1.23456
963
Decimal two digits 1.23
964

965
julia> @printf "Padded to length 5 %5i" 123
966
Padded to length 5   123
967

968
julia> @printf "Padded with zeros to length 6 %06i" 123
969
Padded with zeros to length 6 000123
970

971
julia> @printf "Use shorter of decimal or scientific %g %g" 1.23 12300000.0
972
Use shorter of decimal or scientific 1.23 1.23e+07
973

974
julia> @printf "Use dynamic width and precision  %*.*f" 10 2 0.12345
975
Use dynamic width and precision        0.12
976
```
977
For a systematic specification of the format, see [here](https://en.cppreference.com/w/c/io/fprintf).
978
See also [`@sprintf`](@ref) to get the result as a `String` instead of it being printed.
979

980
# Caveats
981
`Inf` and `NaN` are printed consistently as `Inf` and `NaN` for flags `%a`, `%A`,
982
`%e`, `%E`, `%f`, `%F`, `%g`, and `%G`. Furthermore, if a floating point number is
983
equally close to the numeric values of two possible output strings, the output
984
string further away from zero is chosen.
985

986
# Examples
987
```jldoctest
988
julia> @printf("%f %F %f %F", Inf, Inf, NaN, NaN)
989
Inf Inf NaN NaN
990

991
julia> @printf "%.0f %.1f %f" 0.5 0.025 -0.0078125
992
0 0.0 -0.007812
993
```
994

995
!!! compat "Julia 1.8"
996
    Starting in Julia 1.8, `%s` (string) and `%c` (character) widths are computed
997
    using [`textwidth`](@ref), which e.g. ignores zero-width characters
998
    (such as combining characters for diacritical marks) and treats certain
999
    "wide" characters (e.g. emoji) as width `2`.
1000

1001
!!! compat "Julia 1.10"
1002
    Dynamic width specifiers like `%*s` and `%0*.*f` require Julia 1.10.
1003
"""
1004
macro printf(io_or_fmt, args...)
12✔
1005
    if io_or_fmt isa String
12✔
1006
        fmt = Format(io_or_fmt)
×
1007
        return esc(:($Printf.format(stdout, $fmt, $(args...))))
×
1008
    else
1009
        io = io_or_fmt
12✔
1010
        isempty(args) && throw(ArgumentError("No format string provided to `@printf` - use like `@printf [io] <format string> [<args...>]."))
12✔
1011
        fmt_str = first(args)
12✔
1012
        fmt_str isa String || throw(ArgumentError("First argument to `@printf` after `io` must be a format string"))
12✔
1013
        fmt = Format(fmt_str)
12✔
1014
        return esc(:($Printf.format($io, $fmt, $(Base.tail(args)...))))
12✔
1015
    end
1016
end
1017

1018
"""
1019
    @sprintf("%Fmt", args...)
1020

1021
Return [`@printf`](@ref) formatted output as string.
1022

1023
# Examples
1024
```jldoctest
1025
julia> @sprintf "this is a %s %15.1f" "test" 34.567
1026
"this is a test            34.6"
1027
```
1028
"""
1029
macro sprintf(fmt, args...)
17✔
1030
    fmt isa String || throw(ArgumentError("First argument to `@sprintf` must be a format string."))
17✔
1031
    f = Format(fmt)
17✔
1032
    return esc(:($Printf.format($f, $(args...))))
17✔
1033
end
1034

1035
end # module
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc