Skip to content

Commit

Permalink
Encourage type inference to try harder in printf(args::Tuple)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenhinkeller committed Jul 18, 2022
1 parent 5516729 commit 0214afc
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 21 deletions.
158 changes: 140 additions & 18 deletions src/printformats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ julia> printf(c"%f\n", 1/3)
0
```
"""
@inline printf(n::T) where T <: Union{Number, Ptr} = printf(printfmt(T), n)

@inline function printf(n::T) where T <: Union{Number, Ptr}
printf(printfmt(T), n)
return zero(Int32)
end
# Print a vector
"""
```julia
Expand Down Expand Up @@ -73,7 +75,7 @@ julia> printf(rand(5,5))
end

# Print a tuple
@inline function printf(v::NTuple{N, T} where N) where T <: Union{Number, Ptr, StaticString}
@inline function printf(v::NTuple{N, T} where N) where T <: Union{Number, Ptr}
fmt = printfmt(T)
p = pointer(fmt)
putchar(0x28) # open paren
Expand Down Expand Up @@ -106,7 +108,7 @@ end
# Top-level formats, single numbers
@inline function printf(fp::Ptr{FILE}, n::T) where T <: Union{Number, Ptr}
printf(fp, printfmt(T), n)
newline(fp)
return zero(Int32)
end


Expand All @@ -122,7 +124,7 @@ end
end

# Print a tuple
@inline function printf(fp::Ptr{FILE}, v::NTuple{N, T} where N) where T <: Union{Number, Ptr, StaticString}
@inline function printf(fp::Ptr{FILE}, v::NTuple{N, T} where N) where T <: Union{Number, Ptr}
fmt = printfmt(T)
p = pointer(fmt)
putchar(fp, 0x28) # open paren
Expand Down Expand Up @@ -150,35 +152,155 @@ end
return zero(Int32)
end

## -- Printing a long string of things
## -- Printing a long string of things -- encourage type inference to work a bit harder

"""
```julia
printf([fp::Ptr{FILE}], args...)
printf([fp::Ptr{FILE}], things::Tuple)
```
Print any number of arguments, optionally to a file specified by `fp`.
Print any number of things, optionally to a file specified by `fp`.
## Examples
```julia
julia> printf(c"Sphinx ", c"of ", c"black ", c"quartz, ", c"judge ", c"my ", c"vow!\n")
julia> printf((c"Sphinx ", c"of ", c"black ", c"quartz, ", c"judge ", c"my ", c"vow!\n"))
Sphinx of black quartz, judge my vow!
0
julia> printf(c"The value of x is currently ", x, c"\n")
julia> x = 1
1
julia> printf((c"The value of x is currently ", x, c"\n"))
The value of x is currently 1
0
```
"""
@inline function printf(args...)
for arg in args
printf(arg)
end
@inline function printf(args::Tuple{T1, T2}) where {T1, T2}
printf(args[1])
printf(args[2])
return zero(Int32)
end
@inline function printf(fp::Ptr{FILE}, args...)
for arg in args
printf(fp, arg)
end
@inline function printf(args::Tuple{T1, T2, T3}) where {T1, T2, T3}
printf(args[1])
printf(args[2])
printf(args[3])
return zero(Int32)
end
@inline function printf(args::Tuple{T1, T2, T3, T4}) where {T1, T2, T3, T4}
printf(args[1])
printf(args[2])
printf(args[3])
printf(args[4])
return zero(Int32)
end
@inline function printf(args::Tuple{T1, T2, T3, T4}) where {T1, T2, T3, T4}
printf(args[1])
printf(args[2])
printf(args[3])
printf(args[4])
return zero(Int32)
end
@inline function printf(args::Tuple{T1, T2, T3, T4, T5}) where {T1, T2, T3, T4, T5}
printf(args[1])
printf(args[2])
printf(args[3])
printf(args[4])
printf(args[5])
return zero(Int32)
end
@inline function printf(args::Tuple{T1, T2, T3, T4, T5, T6}) where {T1, T2, T3, T4, T5, T6}
printf(args[1])
printf(args[2])
printf(args[3])
printf(args[4])
printf(args[5])
printf(args[6])
printf(args[7])
return zero(Int32)
end
@inline function printf(args::Tuple{T1, T2, T3, T4, T5, T6, T7}) where {T1, T2, T3, T4, T5, T6, T7}
printf(args[1])
printf(args[2])
printf(args[3])
printf(args[4])
printf(args[5])
printf(args[6])
printf(args[7])
return zero(Int32)
end
@inline function printf(args::Tuple{T1, T2, T3, T4, T5, T6, T7, T8}) where {T1, T2, T3, T4, T5, T6, T7, T8}
printf(args[1])
printf(args[2])
printf(args[3])
printf(args[4])
printf(args[5])
printf(args[6])
printf(args[7])
printf(args[8])
return zero(Int32)
end
# Print to file
@inline function printf(fp::Ptr{FILE}, args::Tuple{T1, T2}) where {T1, T2}
printf(fp, args[1])
printf(fp, args[2])
return zero(Int32)
end
@inline function printf(fp::Ptr{FILE}, args::Tuple{T1, T2, T3}) where {T1, T2, T3}
printf(fp, args[1])
printf(fp, args[2])
printf(fp, args[3])
return zero(Int32)
end
@inline function printf(fp::Ptr{FILE}, args::Tuple{T1, T2, T3, T4}) where {T1, T2, T3, T4}
printf(fp, args[1])
printf(fp, args[2])
printf(fp, args[3])
printf(fp, args[4])
return zero(Int32)
end
@inline function printf(fp::Ptr{FILE}, args::Tuple{T1, T2, T3, T4}) where {T1, T2, T3, T4}
printf(fp, args[1])
printf(fp, args[2])
printf(fp, args[3])
printf(fp, args[4])
return zero(Int32)
end
@inline function printf(fp::Ptr{FILE}, args::Tuple{T1, T2, T3, T4, T5}) where {T1, T2, T3, T4, T5}
printf(fp, args[1])
printf(fp, args[2])
printf(fp, args[3])
printf(fp, args[4])
printf(fp, args[5])
return zero(Int32)
end
@inline function printf(fp::Ptr{FILE}, args::Tuple{T1, T2, T3, T4, T5, T6}) where {T1, T2, T3, T4, T5, T6}
printf(fp, args[1])
printf(fp, args[2])
printf(fp, args[3])
printf(fp, args[4])
printf(fp, args[5])
printf(fp, args[6])
printf(fp, args[7])
return zero(Int32)
end
@inline function printf(fp::Ptr{FILE}, args::Tuple{T1, T2, T3, T4, T5, T6, T7}) where {T1, T2, T3, T4, T5, T6, T7}
printf(fp, args[1])
printf(fp, args[2])
printf(fp, args[3])
printf(fp, args[4])
printf(fp, args[5])
printf(fp, args[6])
printf(fp, args[7])
return zero(Int32)
end
@inline function printf(fp::Ptr{FILE}, args::Tuple{T1, T2, T3, T4, T5, T6, T7, T8}) where {T1, T2, T3, T4, T5, T6, T7, T8}
printf(fp, args[1])
printf(fp, args[2])
printf(fp, args[3])
printf(fp, args[4])
printf(fp, args[5])
printf(fp, args[6])
printf(fp, args[7])
printf(fp, args[8])
return zero(Int32)
end

Expand Down
7 changes: 4 additions & 3 deletions test/testllvmio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
@test printf(0x00000001) == 0
@test printf(0x0000000000000001) == 0
@test printf(Ptr{UInt64}(0)) == 0
@test printf(c"\n", 1, c": ", c"Sphinx ", c"of ", c"black ", c"quartz, ", c"judge ", c"my ", c"vow!\n") == 0
@test printf((c"Sphinx ", c"of ", c"black ", c"quartz, ", c"judge ", c"my ", c"vow!\n")) == 0
@test printf((c"The value of x is currently ", 1.0, c"\n")) == 0

## -- low-level printing to file

Expand All @@ -49,8 +50,8 @@
@test printf(fp, 0x00000001) == 0
@test printf(fp, 0x0000000000000001) == 0
@test printf(fp, Ptr{UInt64}(0)) == 0
@test printf(fp, c"\n", 1, c": ", c"Sphinx ", c"of ", c"black ", c"quartz, ", c"judge ", c"my ", c"vow!\n") == 0

@test printf(fp, (c"Sphinx ", c"of ", c"black ", c"quartz, ", c"judge ", c"my ", c"vow!\n")) == 0
@test printf(fp, (c"The value of x is currently ", 1.0, c"\n")) == 0

## -- High-level printing

Expand Down

2 comments on commit 0214afc

@brenhinkeller
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

  • Add @externptr and @externload
  • Add convienience method to printf for printing a tuple of various arguments

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/64437

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.3 -m "<description of version>" 0214afcb9aeefc08c8be3be68b8409eabb3c3d40
git push origin v0.5.3

Please sign in to comment.