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

JuliaLang / julia / #37799

06 Jun 2024 03:00AM UTC coverage: 87.483% (+4.3%) from 83.152%
#37799

push

local

web-flow
Fix assertion/crash when optimizing function with dead basic block (#54690)

AllocOpt probably needs to handle that in other places more smartly but
this seems to at least stop it crashing. Fixes issue found in
https://github.com/JuliaLang/julia/pull/54604#issuecomment-2136320508 by
@topolarity.

76991 of 88007 relevant lines covered (87.48%)

16066183.62 hits per line

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

76.35
/stdlib/Printf/src/Printf.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2
"""
3
The `Printf` module provides formatted output functions similar to the C standard library's `printf`. It allows formatted printing to an output stream or to a string.
4
"""
5
module Printf
6

7
using Base.Ryu
8

9
export @printf, @sprintf
10

11
public format, Format
12

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

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

26
"""
27
Typed representation of a format specifier.
28

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

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

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

58
Base.show(io::IO, f::Spec) = print(io, string(f))
×
59

60
floatfmt(s::Spec{T}) where {T} =
11✔
61
    Spec{Val{'f'}}(s.leftalign, s.plus, s.space, s.zero, s.hash, s.width, 0, s.dynamic_width, s.dynamic_precision)
62
ptrfmt(s::Spec{T}, x) where {T} =
13✔
63
    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)
64

65
"""
66
    Printf.Format(format_str)
67

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

70
The input `format_str` can include any valid format specifier character and modifiers.
71

72
A `Format` object can be passed to `Printf.format(f::Format, args...)` to produce a
73
formatted string, or `Printf.format(io::IO, f::Format, args...)` to print the
74
formatted string directly to `io`.
75

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

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

95
# what number base should be used for a given format specifier?
96
base(T) = T <: HexBases ? 16 : T <: Val{'o'} ? 8 : 10
1,879✔
97
char(::Type{Val{c}}) where {c} = c
252✔
98

99
struct InvalidFormatStringError <: Exception
100
    message::String
12✔
101
    format::String
102
    start_color::Int
103
    end_color::Int
104
end
105

106
function Base.showerror(io::IO, err::InvalidFormatStringError)
×
107
    io_has_color = get(io, :color, false)::Bool
×
108

109
    println(io, "InvalidFormatStringError: ", err.message)
×
110
    print(io, "    \"", @view(err.format[begin:prevind(err.format, err.start_color)]))
×
111
    invalid_text = @view err.format[err.start_color:err.end_color]
×
112

113
    printstyled(io, invalid_text, color=:red)
×
114

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

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

127
# parse format string
128
function Format(f::AbstractString)
1,615✔
129
    bytes = codeunits(f)
1,615✔
130
    len = length(bytes)
1,615✔
131
    pos = 1
1,615✔
132
    numarguments = 0
1,615✔
133

134
    b = 0x00
1,615✔
135
    local last_percent_pos
136

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

277
macro format_str(str)
278
    Format(str)
279
end
280

281
const hex = b"0123456789abcdef"
282
const HEX = b"0123456789ABCDEF"
283

284
# write out a single arg according to format options
285
# char
286
@inline function writechar(buf, pos, c)
287
    u = bswap(reinterpret(UInt32, c))
421✔
288
    while true
458✔
289
        buf[pos] = u % UInt8
458✔
290
        pos += 1
458✔
291
        (u >>= 8) == 0 && break
458✔
292
    end
37✔
293
    return pos
421✔
294
end
295

296

297
@inline function rmdynamic(spec::Spec{T}, args, argp) where {T}
298
    zero, width, precision = spec.zero, spec.width, spec.precision
3,408✔
299
    if spec.dynamic_width
3,408✔
300
        width = args[argp]
516✔
301
        argp += 1
516✔
302
    end
303
    if spec.dynamic_precision
3,408✔
304
        precision = args[argp]
362✔
305
        if zero && T <: Ints && precision > 0
362✔
306
            zero = false
30✔
307
        end
308
        argp += 1
362✔
309
    end
310
    (Spec{T}(spec.leftalign, spec.plus, spec.space, zero, spec.hash, width, precision, false, false), argp)
3,468✔
311
end
312

313
@inline function fmt(buf, pos, args, argp, spec::Spec{T}) where {T}
2✔
314
    spec, argp = rmdynamic(spec, args, argp)
1,734✔
315
    (fmt(buf, pos, args[argp], spec), argp+1)
3,046✔
316
end
317

318
@inline function fmt(buf, pos, arg, spec::Spec{T}) where {T <: Chars}
3✔
319
    leftalign, width = spec.leftalign, spec.width
39✔
320
    c = Char(first(arg))
40✔
321
    w = textwidth(c)
78✔
322
    if !leftalign && width > w
39✔
323
        for _ = 1:(width - w)
10✔
324
            buf[pos] = UInt8(' ')
70✔
325
            pos += 1
70✔
326
        end
130✔
327
    end
328
    pos = writechar(buf, pos, c)
51✔
329
    if leftalign && width > w
39✔
330
        for _ = 1:(width - w)
9✔
331
            buf[pos] = UInt8(' ')
120✔
332
            pos += 1
120✔
333
        end
231✔
334
    end
335
    return pos
39✔
336
end
337

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

379
# integers
380
toint(x) = x
1,877✔
381
toint(x::Rational) = Integer(x)
2✔
382

383
fmt(buf, pos, arg::AbstractFloat, spec::Spec{T}) where {T <: Ints} =
11✔
384
    fmt(buf, pos, arg, floatfmt(spec))
385

386
@inline function fmt(buf, pos, arg, spec::Spec{T}) where {T <: Ints}
387
    leftalign, plus, space, zero, hash, width, prec =
946✔
388
        spec.leftalign, spec.plus, spec.space, spec.zero, spec.hash, spec.width, spec.precision
389
    bs = base(T)
946✔
390
    arg2 = toint(arg)
946✔
391
    n = i = ndigits(arg2, base=bs, pad=1)
946✔
392
    neg = arg2 < 0
946✔
393
    x = arg2 isa Base.BitSigned ? unsigned(abs(arg2)) : abs(arg2)
946✔
394
    arglen = n + (neg || (plus | space)) +
1,660✔
395
        (T == Val{'o'} && hash ? 1 : 0) +
396
        (T == Val{'x'} && hash ? 2 : 0) + (T == Val{'X'} && hash ? 2 : 0)
397
    arglen2 = arglen < width && prec > 0 ? arglen + min(max(0, prec - n), width - arglen) : arglen
946✔
398
    if !leftalign && !zero && arglen2 < width
946✔
399
        # pad left w/ spaces
400
        for _ = 1:(width - arglen2)
334✔
401
            buf[pos] = UInt8(' ')
2,901✔
402
            pos += 1
2,901✔
403
        end
5,468✔
404
    end
405
    if neg
946✔
406
        buf[pos] = UInt8('-'); pos += 1
232✔
407
    elseif plus # plus overrides space
714✔
408
        buf[pos] = UInt8('+'); pos += 1
85✔
409
    elseif space
629✔
410
        buf[pos] = UInt8(' '); pos += 1
69✔
411
    end
412
    if T == Val{'o'} && hash
946✔
413
        buf[pos] = UInt8('0')
48✔
414
        pos += 1
48✔
415
    elseif T == Val{'x'} && hash
898✔
416
        buf[pos] = UInt8('0')
67✔
417
        buf[pos + 1] = UInt8('x')
67✔
418
        pos += 2
67✔
419
    elseif T == Val{'X'} && hash
831✔
420
        buf[pos] = UInt8('0')
36✔
421
        buf[pos + 1] = UInt8('X')
36✔
422
        pos += 2
36✔
423
    end
424
    if zero && arglen2 < width
946✔
425
        for _ = 1:(width - arglen2)
133✔
426
            buf[pos] = UInt8('0')
890✔
427
            pos += 1
890✔
428
        end
1,647✔
429
    elseif n < prec
813✔
430
        for _ = 1:(prec - n)
191✔
431
            buf[pos] = UInt8('0')
1,747✔
432
            pos += 1
1,747✔
433
        end
1,747✔
434
    elseif arglen < arglen2
622✔
435
        for _ = 1:(arglen2 - arglen)
×
436
            buf[pos] = UInt8('0')
×
437
            pos += 1
×
438
        end
×
439
    end
440
    while i > 0
4,783✔
441
        @inbounds buf[pos + i - 1] = bs == 16 ?
3,837✔
442
            (T == Val{'x'} ? hex[(x & 0x0f) + 1] : HEX[(x & 0x0f) + 1]) :
443
            (48 + (bs == 8 ? (x & 0x07) : rem(x, 10)))
444
        if bs == 8
3,837✔
445
            x >>= 3
619✔
446
        elseif bs == 16
3,218✔
447
            x >>= 4
1,407✔
448
        else
449
            x = oftype(x, div(x, 10))
1,811✔
450
        end
451
        i -= 1
3,837✔
452
    end
3,837✔
453
    pos += n
946✔
454
    if leftalign && arglen2 < width
946✔
455
        # pad right
456
        for _ = 1:(width - arglen2)
185✔
457
            buf[pos] = UInt8(' ')
1,323✔
458
            pos += 1
1,323✔
459
        end
2,461✔
460
    end
461
    return pos
946✔
462
end
463

464
# floats
465
"""
466
    Printf.tofloat(x)
467

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

473
```julia
474
Printf.tofloat(x::MyCustomType) = convert_my_custom_type_to_float(x)
475
```
476

477
For arbitrary precision numerics, you might extend the method like:
478

479
```julia
480
Printf.tofloat(x::MyArbitraryPrecisionType) = BigFloat(x)
481
```
482

483
!!! compat "Julia 1.6"
484
    This function requires Julia 1.6 or later.
485
"""
486
tofloat(x) = Float64(x)
58✔
487
tofloat(x::Base.IEEEFloat) = x
485✔
488
tofloat(x::BigFloat) = x
57✔
489

490
_snprintf(ptr, siz, str, arg) =
50✔
491
    @ccall "libmpfr".mpfr_snprintf(ptr::Ptr{UInt8}, siz::Csize_t, str::Ptr{UInt8};
492
                                   arg::Ref{BigFloat})::Cint
493

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

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

678
# pointers
679
fmt(buf, pos, arg, spec::Spec{Pointer}) = fmt(buf, pos, UInt64(arg), ptrfmt(spec, arg))
13✔
680

681
# position counters
682
function fmt(buf, pos, arg::Ref{<:Integer}, ::Spec{PositionCounter})
683
    arg[] = pos - 1
7✔
684
    pos
7✔
685
end
686

687
# old Printf compat
688
function fix_dec end
689
function ini_dec end
690

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

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

815
const UNROLL_UPTO = 16
816
# if you have your own buffer + pos, write formatted args directly to it
817
@inline function format(buf::Vector{UInt8}, pos::Integer, f::Format, args...)
9✔
818
    # write out first substring
819
    escapechar = false
1,616✔
820
    for i in f.substringranges[1]
1,649✔
821
        b = f.str[i]
135✔
822
        if !escapechar
135✔
823
            buf[pos] = b
129✔
824
            pos += 1
129✔
825
            escapechar = b === UInt8('%')
129✔
826
        else
827
            escapechar = false
6✔
828
        end
829
    end
237✔
830
    # for each format, write out arg and next substring
831
    # unroll up to 16 formats
832
    N = length(f.formats)
1,616✔
833
    argp = 1
1,616✔
834
    Base.@nexprs 16 i -> begin
835
        if N >= i
3,310✔
836
            pos, argp = fmt(buf, pos, args, argp, f.formats[i])
3,040✔
837
            for j in f.substringranges[i + 1]
1,787✔
838
                b = f.str[j]
107✔
839
                if !escapechar
107✔
840
                    buf[pos] = b
106✔
841
                    pos += 1
106✔
842
                    escapechar = b === UInt8('%')
106✔
843
                else
844
                    escapechar = false
1✔
845
                end
846
            end
124✔
847
        end
848
    end
849
    if N > 16
1,615✔
850
        for i = 17:length(f.formats)
3✔
851
            pos, argp = fmt(buf, pos, args, argp, f.formats[i])
6✔
852
            for j in f.substringranges[i + 1]
9✔
853
                b = f.str[j]
3✔
854
                if !escapechar
3✔
855
                    buf[pos] = b
3✔
856
                    pos += 1
3✔
857
                    escapechar = b === UInt8('%')
3✔
858
                else
859
                    escapechar = false
×
860
                end
861
            end
3✔
862
        end
9✔
863
    end
864
    return pos
1,615✔
865
end
866

867
@inline function plength(f::Spec{T}, args, argp) where {T}
868
    f, argp = rmdynamic(f, args, argp)
1,734✔
869
    (plength(f, args[argp]), argp+1)
1,828✔
870
end
871

872
function plength(f::Spec{T}, x) where {T <: Chars}
8✔
873
    c = Char(first(x))
40✔
874
    w = textwidth(c)
78✔
875
    return max(f.width, w) + (ncodeunits(c) - w)
39✔
876
end
877

878
plength(f::Spec{Pointer}, x) = max(f.width, 2 * sizeof(x) + 2)
13✔
879

880
function plength(f::Spec{T}, x) where {T <: Strings}
881
    str = string(x)
112✔
882
    sw = textwidth(str)
112✔
883
    p = f.precision == -1 ? (sw + (f.hash ? (x isa Symbol ? 1 : 2) : 0)) : f.precision
161✔
884
    return max(f.width, p) + (sizeof(str) - sw)
112✔
885
end
886

887
function plength(f::Spec{T}, x) where {T <: Ints}
×
888
    x2 = toint(x)
933✔
889
    return max(
933✔
890
        f.width,
891
        f.precision + ndigits(x2, base=base(T), pad=1) + MAX_FMT_CHARS_WIDTH
892
    )
893
end
894

895
plength(f::Spec{T}, x::AbstractFloat) where {T <: Ints} =
11✔
896
    max(f.width, f.hash + MAX_INTEGER_PART_WIDTH + 0 + MAX_FMT_CHARS_WIDTH)
897
plength(f::Spec{T}, x) where {T <: Floats} =
589✔
898
    max(f.width, f.hash + MAX_INTEGER_PART_WIDTH + f.precision + MAX_FMT_CHARS_WIDTH)
899
plength(::Spec{PositionCounter}, x) = 0
×
900

901
@inline function computelen(substringranges, formats, args)
2✔
902
    len = sum(length, substringranges)
1,616✔
903
    N = length(formats)
1,616✔
904
    # unroll up to 16 formats
905
    argp = 1
1,616✔
906
    Base.@nexprs 16 i -> begin
907
        if N >= i
3,311✔
908
            l, argp = plength(formats[i], args, argp)
1,850✔
909
            len += l
1,698✔
910
        end
911
    end
912
    if N > 16
1,616✔
913
        for i = 17:length(formats)
3✔
914
            l, argp = plength(formats[i], args, argp)
6✔
915
            len += l
6✔
916
        end
9✔
917
    end
918
    return len
1,616✔
919
end
920

921
@noinline argmismatch(a, b) =
1✔
922
    throw(ArgumentError("Number of format specifiers and number of provided args differ: $a != $b"))
923

924
"""
925
    Printf.format(f::Printf.Format, args...) => String
926
    Printf.format(io::IO, f::Printf.Format, args...)
927

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

934
function format(io::IO, f::Format, args...) # => Nothing
4✔
935
    f.numarguments == length(args) || argmismatch(f.numarguments, length(args))
4✔
936
    buf = Base.StringVector(computelen(f.substringranges, f.formats, args))
8✔
937
    pos = format(buf, 1, f, args...)
4✔
938
    write(io, resize!(buf, pos - 1))
4✔
939
    return
4✔
940
end
941

942
function format(f::Format, args...) # => String
1,613✔
943
    f.numarguments == length(args) || argmismatch(f.numarguments, length(args))
1,614✔
944
    buf = Base.StringVector(computelen(f.substringranges, f.formats, args))
3,216✔
945
    pos = format(buf, 1, f, args...)
1,657✔
946
    return String(resize!(buf, pos - 1))
1,611✔
947
end
948

949
"""
950
    @printf([io::IO], "%Fmt", args...)
951

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

955
# Examples
956
```jldoctest
957
julia> @printf "Hello %s" "world"
958
Hello world
959

960
julia> @printf "Scientific notation %e" 1.234
961
Scientific notation 1.234000e+00
962

963
julia> @printf "Scientific notation three digits %.3e" 1.23456
964
Scientific notation three digits 1.235e+00
965

966
julia> @printf "Decimal two digits %.2f" 1.23456
967
Decimal two digits 1.23
968

969
julia> @printf "Padded to length 5 %5i" 123
970
Padded to length 5   123
971

972
julia> @printf "Padded with zeros to length 6 %06i" 123
973
Padded with zeros to length 6 000123
974

975
julia> @printf "Use shorter of decimal or scientific %g %g" 1.23 12300000.0
976
Use shorter of decimal or scientific 1.23 1.23e+07
977

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

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

990
# Examples
991
```jldoctest
992
julia> @printf("%f %F %f %F", Inf, Inf, NaN, NaN)
993
Inf Inf NaN NaN
994

995
julia> @printf "%.0f %.1f %f" 0.5 0.025 -0.0078125
996
0 0.0 -0.007812
997
```
998

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

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

1022
"""
1023
    @sprintf("%Fmt", args...)
1024

1025
Return [`@printf`](@ref) formatted output as string.
1026

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

1039
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