Skip to content

Commit 8560318

Browse files
Loosen type restriction on Ryu.writeshortest (#57172)
`Ryu.writeshortest` is documented to have a method that "allows passing in a byte buffer", just like `Ryu.writefixed` and `Ryu.writeexp`, but unlike those functions `writeshortest` is type constrained to `::Vector{UInt8}`. This PR loosens that to `::AbstractVector{UInt8}`, to allow the "byte buffer" to e.g. be a `Memory` rather than a `Vector`. I've added tests and updated the docstrings for all three functions to ensure that they're not just restricted to `Vector{UInt8}`. This change was prompted by our private codebase hitting `MethodError: no method matching writeshortest(::Memory{UInt8}, ::Int64, ::Float64, ...)` when trying it out with Julia v1.12.0-DEV. (cc @quinnj -- i think you added this method originally, but i couldn't see any reason why e.g. Memory shouldn't be allowed now we have it)
1 parent eff8ba4 commit 8560318

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

base/ryu/Ryu.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ neededdigits(::Type{Float16}) = 9 + 5 + 9
1919

2020
"""
2121
Ryu.writeshortest(x, plus=false, space=false, hash=true, precision=-1, expchar=UInt8('e'), padexp=false, decchar=UInt8('.'), typed=false, compact=false)
22-
Ryu.writeshortest(buf::Vector{UInt8}, pos::Int, x, args...)
22+
Ryu.writeshortest(buf::AbstractVector{UInt8}, pos::Int, x, args...)
2323
2424
Convert a float value `x` into its "shortest" decimal string, which can be parsed back to the same value.
2525
This function allows achieving the `%g` printf format.
@@ -53,7 +53,7 @@ end
5353

5454
"""
5555
Ryu.writefixed(x, precision, plus=false, space=false, hash=false, decchar=UInt8('.'), trimtrailingzeros=false)
56-
Ryu.writefixed(buf::Vector{UInt8}, pos::Int, x, args...)
56+
Ryu.writefixed(buf::AbstractVector{UInt8}, pos::Int, x, args...)
5757
5858
Convert a float value `x` into a "fixed" size decimal string of the provided precision.
5959
This function allows achieving the `%f` printf format.
@@ -81,7 +81,7 @@ end
8181

8282
"""
8383
Ryu.writeexp(x, precision, plus=false, space=false, hash=false, expchar=UInt8('e'), decchar=UInt8('.'), trimtrailingzeros=false)
84-
Ryu.writeexp(buf::Vector{UInt8}, pos::Int, x, args...)
84+
Ryu.writeexp(buf::AbstractVector{UInt8}, pos::Int, x, args...)
8585
8686
Convert a float value `x` into a scientific notation decimal string.
8787
This function allows achieving the `%e` printf format.

base/ryu/shortest.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ integer. If a `maxsignif` argument is provided, then `b < maxsignif`.
224224
return b, e10
225225
end
226226

227-
function writeshortest(buf::Vector{UInt8}, pos, x::T,
227+
function writeshortest(buf::AbstractVector{UInt8}, pos, x::T,
228228
plus=false, space=false, hash=true,
229229
precision=-1, expchar=UInt8('e'), padexp=false, decchar=UInt8('.'),
230230
typed=false, compact=false) where {T}

test/ryu.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,23 @@ end
370370

371371
end # Float16
372372

373+
@testset "writeshortest(::AbstractVector, pos, ...)" begin
374+
@testset for Vec in (Vector{UInt8}, Memory{UInt8})
375+
buf = Vec(undef, 4)
376+
@test Ryu.writeshortest(buf, 1, -0.0) == 5
377+
@test String(buf) == "-0.0"
378+
379+
buf = Vec(undef, 100)
380+
xx = 4.7223665f21
381+
expected = "4.7223665e21"
382+
start_pos = 42
383+
nwritten = length(expected)
384+
end_pos = start_pos + nwritten
385+
@test Ryu.writeshortest(buf, start_pos, xx) == end_pos
386+
@test String(buf[start_pos:end_pos-1]) == expected
387+
end
388+
end
389+
373390
@testset "Ryu.writefixed" begin
374391
@testset "Basic" begin
375392
@test Ryu.writefixed(todouble(false, 1234, 99999), 0) ==
@@ -563,6 +580,23 @@ end # Float16
563580
@test Ryu.writefixed(-100.0+eps(-100.0), 0, false, false, true, UInt8('.'), false) == "-100."
564581
@test Ryu.writefixed(100.0-eps(100.0), 1, false, false, true, UInt8('.'), false) == "100.0"
565582
@test Ryu.writefixed(-100.0+eps(-100.0), 1, false, false, true, UInt8('.'), false) == "-100.0"
583+
584+
@testset "writefixed(::AbstractVector, pos, ...)" begin
585+
@testset for Vec in (Vector{UInt8}, Memory{UInt8})
586+
buf = Vec(undef, 6)
587+
@test Ryu.writefixed(buf, 1, 0.0, 4) == 7
588+
@test String(buf) == "0.0000"
589+
590+
buf = Vec(undef, 100)
591+
xx = 1729.142857142857
592+
prec = 8
593+
start_pos = 42
594+
nwritten = 4 + 1 + prec
595+
end_pos = start_pos + nwritten
596+
@test Ryu.writefixed(buf, start_pos, xx, prec) == end_pos
597+
@test String(buf[start_pos:end_pos-1]) == "1729.14285714"
598+
end
599+
end
566600
end # fixed
567601

568602
@testset "Ryu.writeexp" begin
@@ -761,6 +795,23 @@ end
761795
@test Ryu.writeexp(2.0, 1, false, false, false, UInt8('e'), UInt8('.'), true) == "2e+00"
762796
end
763797

798+
@testset "writeexp(::AbstractVector, pos, ...)" begin
799+
@testset for Vec in (Vector{UInt8}, Memory{UInt8})
800+
buf = Vec(undef, 10)
801+
@test Ryu.writeexp(buf, 1, 0.0, 4) == 11
802+
@test String(buf) == "0.0000e+00"
803+
804+
buf = Vec(undef, 100)
805+
xx = 1729.142857142857
806+
prec = 8
807+
start_pos = 42
808+
nwritten = 1 + 1 + prec + 4
809+
end_pos = start_pos + nwritten
810+
@test Ryu.writeexp(buf, start_pos, xx, prec) == end_pos
811+
@test String(buf[start_pos:end_pos-1]) == "1.72914286e+03"
812+
end
813+
end
814+
764815
end # exp
765816

766817
@testset "compact" begin

0 commit comments

Comments
 (0)