Skip to content

Commit

Permalink
Additional fix + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Oct 6, 2020
1 parent 467ae38 commit d2f65db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,19 @@ function Format(f::AbstractString)
len = length(bytes)
pos = 1
b = 0x00
while true
while pos <= len
b = bytes[pos]
pos += 1
(pos > len || (b == UInt8('%') && pos <= len && bytes[pos] != UInt8('%'))) && break
if b == UInt8('%')
pos > len && throw(ArgumentError("invalid format string: '$f'"))
if bytes[pos] == UInt8('%')
# escaped '%'
b = bytes[pos]
pos += 1
else
break
end
end
end
strs = [1:pos - 1 - (b == UInt8('%'))]
fmts = []
Expand Down
6 changes: 6 additions & 0 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ end
@testset "basics" begin

@test Printf.@sprintf("%%") == "%"
@test Printf.@sprintf("1%%") == "1%"
@test Printf.@sprintf("%%1") == "%1"
@test Printf.@sprintf("1%%2") == "1%2"
@test Printf.@sprintf("1%%%d", 2) == "1%2"
@test Printf.@sprintf("1%%2%%3") == "1%2%3"
@test Printf.@sprintf("GAP[%%]") == "GAP[%]"
@test Printf.@sprintf("hey there") == "hey there"
@test_throws ArgumentError Printf.Format("")
@test_throws ArgumentError Printf.Format("%+")
Expand Down

0 comments on commit d2f65db

Please sign in to comment.