Skip to content

Commit afaea39

Browse files
committed
Fix bug with precision > length of string
1 parent e3c72b0 commit afaea39

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ keywords = ["Strings", "Formatting"]
2323
license = "MIT"
2424
name = "Format"
2525
uuid = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
26-
version = "1.2.1"
26+
version = "1.2.2"
2727

2828
[deps]
2929
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

src/printf.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ end
235235
function _fmt(buf, pos, spec::FmtSpec{FmtStr}, arg)
236236
altf, width, prec = spec.altf, spec.width, spec.prec
237237
str = altf && (arg isa Symbol || arg isa AbstractString) ? repr(arg) : string(arg)
238-
op = p = prec == -1 ? length(str) : prec
238+
slen = length(str)
239+
op = p = prec == -1 ? slen : min(slen, prec)
239240
# Make sure there is enough room in buffer
240241
nlen = max(width, p)
241242
buflen = sizeof(buf) - pos

test/cformat.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ function runtime_float()
7676
cfmt( "%10.4f", _erfinv( rand() ) )
7777
end
7878
end
79+
function runtime_float_spec()
80+
set_seed!( 10 )
81+
fs = Format.FmtSpec("%10.4f")
82+
for i in 1:200000
83+
cfmt( fs, _erfinv( rand() ) )
84+
end
85+
end
7986
function runtime_float_bypass()
8087
f = generate_formatter( "%10.4f" )
8188
set_seed!( 10 )
@@ -87,9 +94,11 @@ end
8794
println()
8895
println( "float64 @sprintf speed")
8996
@time native_float()
90-
println( "float64 sprintf speed")
97+
println( "float64 cfmt speed")
9198
@time runtime_float()
92-
println( "float64 sprintf speed, bypass repeated lookup")
99+
println( "float64 cfmt spec speed")
100+
@time runtime_float_spec()
101+
println( "float64 cfmt speed, bypass repeated lookup")
93102
@time runtime_float_bypass()
94103

95104
@testset "test commas..." begin
@@ -103,6 +112,10 @@ println( "float64 sprintf speed, bypass repeated lookup")
103112
@test cfmt( "%'s", 1234567.0 ) == "1.234567e6"
104113
end
105114

115+
@testset "Test bug introduced by stdlib/Printf rewrite" begin
116+
@test cfmt( "%4.2s", "a" ) == " a"
117+
end
118+
106119
@testset "test format..." begin
107120
@test format( 10 ) == "10"
108121
@test format( 10.0 ) == "10"
@@ -165,7 +178,9 @@ end
165178
# same with unspecified width
166179
@test format( 12345678, commas=true, parens=true )== " 12,345,678 "
167180
@test format( -12345678, commas=true, parens=true )== "(12,345,678)"
181+
end
168182

183+
@testset "autoscale" begin
169184
@test format( 1.2e9, autoscale = :metric ) == "1.2G"
170185
@test format( 1.2e6, autoscale = :metric ) == "1.2M"
171186
@test format( 1.2e3, autoscale = :metric ) == "1.2k"
@@ -181,7 +196,9 @@ end
181196
@test format( 0x100000, autoscale = :binary ) == "1Mi"
182197
@test format( 0x800, autoscale = :binary ) == "2Ki"
183198
@test format( 0x400, autoscale = :binary ) == "1Ki"
199+
end
184200

201+
@testset "suffix" begin
185202
@test format( 100.00, precision=2, suffix="%" ) == "100.00%"
186203
@test format( 100, precision=2, suffix="%" ) == "100%"
187204
@test format( 100, precision=2, suffix="%", conversion="f" ) == "100.00%"

0 commit comments

Comments
 (0)