From b788d6f81f2524fc0c3531ce55e807af4dd652db Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Sat, 10 Aug 2019 11:08:14 -0600 Subject: [PATCH] Adjustments for printf life --- base/ryu/Ryu.jl | 4 ++-- base/ryu/exp.jl | 60 +++++++++++++++++++++++++++++++++++++---------- base/ryu/fixed.jl | 46 +++++++++++++++++++++++++++--------- 3 files changed, 85 insertions(+), 25 deletions(-) diff --git a/base/ryu/Ryu.jl b/base/ryu/Ryu.jl index 79c9167f6a5d61..d343959468620f 100644 --- a/base/ryu/Ryu.jl +++ b/base/ryu/Ryu.jl @@ -17,13 +17,13 @@ end function writefixed(x::T, precision) where {T <: Base.IEEEFloat} buf = Vector{UInt8}(undef, precision + shortestdigits(T)) - pos = writefixed(x, precision, buf, 1) + pos = writefixed(buf, 1, x, false, false, false, precision) return unsafe_string(pointer(buf), pos-1) end function writeexp(x::T, precision) where {T <: Base.IEEEFloat} buf = Vector{UInt8}(undef, precision + shortestdigits(T)) - pos = writeexp(x, precision, buf, 1) + pos = writeexp(buf, 1, x, false, false, false, precision) return unsafe_string(pointer(buf), pos-1) end diff --git a/base/ryu/exp.jl b/base/ryu/exp.jl index 1697f86d50a960..b5f7ec9a3375f9 100644 --- a/base/ryu/exp.jl +++ b/base/ryu/exp.jl @@ -1,4 +1,6 @@ -@inline function writeexp(v::T, precision, buf, pos) where {T <: Base.IEEEFloat} +@inline function writeexp(buf, pos, v::T, + plus=false, space=false, hash=false, + precision=-1, expchar=UInt8('e'), decchar=UInt8('.')) where {T <: Base.IEEEFloat} x = Float64(v) neg = signbit(x) # special cases @@ -6,18 +8,27 @@ if neg buf[pos] = UInt8('-') pos += 1 + elseif plus + buf[pos] = UInt8('+') + pos += 1 + elseif space + buf[pos] = UInt8(' ') + pos += 1 end buf[pos] = UInt8('0') pos += 1 if precision > 0 - buf[pos] = UInt8('.') + buf[pos] = decchar pos += 1 for _ = 1:precision buf[pos] = UInt8('0') pos += 1 end + elseif hash + buf[pos] = decchar + pos += 1 end - buf[pos] = UInt8('e') + buf[pos] = expchar buf[pos + 1] = UInt8('+') buf[pos + 2] = UInt8('0') buf[pos + 3] = UInt8('0') @@ -30,11 +41,18 @@ elseif !isfinite(x) if neg buf[pos] = UInt8('-') + pos += 1 + elseif plus + buf[pos] = UInt8('+') + pos += 1 + elseif space + buf[pos] = UInt8(' ') + pos += 1 end - buf[pos + neg] = UInt8('I') - buf[pos + neg + 1] = UInt8('n') - buf[pos + neg + 2] = UInt8('f') - return pos + neg + 3 + buf[pos] = UInt8('I') + buf[pos + 1] = UInt8('n') + buf[pos + 2] = UInt8('f') + return pos + 3 end bits = Core.bitcast(UInt64, x) @@ -53,6 +71,12 @@ if neg buf[pos] = UInt8('-') pos += 1 + elseif plus + buf[pos] = UInt8('+') + pos += 1 + elseif space + buf[pos] = UInt8(' ') + pos += 1 end digits = 0 printedDigits = 0 @@ -81,10 +105,14 @@ break end if precision > 1 - pos = append_d_digits(availableDigits, digits, buf, pos) + pos = append_d_digits(availableDigits, digits, buf, pos, decchar) else buf[pos] = UInt8('0') + digits pos += 1 + if hash + buf[pos] = decchar + pos += 1 + end end printedDigits = availableDigits availableDigits = 0 @@ -118,10 +146,14 @@ break end if precision > 1 - pos = append_d_digits(availableDigits, digits, buf, pos) + pos = append_d_digits(availableDigits, digits, buf, pos, decchar) else buf[pos] = UInt8('0') + digits pos += 1 + if hash + buf[pos] = decchar + pos += 1 + end end printedDigits = availableDigits availableDigits = 0 @@ -165,10 +197,14 @@ end else if precision > 1 - pos = append_d_digits(maximum, digits, buf, pos) + pos = append_d_digits(maximum, digits, buf, pos, decchar) else buf[pos] = UInt8('0') + digits pos += 1 + if hash + buf[pos] = decchar + pos += 1 + end end end if roundUp != 0 @@ -181,7 +217,7 @@ break end c = roundPos > 0 ? buf[roundPos] : 0x00 - if c == UInt8('.') + if c == decchar continue elseif c == UInt8('9') buf[roundPos] = UInt8('0') @@ -196,7 +232,7 @@ end end end - buf[pos] = UInt8('e') + buf[pos] = expchar pos += 1 if e < 0 buf[pos] = UInt8('-') diff --git a/base/ryu/fixed.jl b/base/ryu/fixed.jl index c7189a0cbabab7..54038069ee2312 100644 --- a/base/ryu/fixed.jl +++ b/base/ryu/fixed.jl @@ -1,4 +1,6 @@ -@inline function writefixed(v::T, precision, buf, pos, trimtrailingzeros=false) where {T <: Base.IEEEFloat} +@inline function writefixed(buf, pos, v::T, + plus=false, space=false, hash=false, + precision=-1, decchar=UInt8('.'), trimtrailingzeros=false) where {T <: Base.IEEEFloat} x = Float64(v) neg = signbit(x) # special cases @@ -6,11 +8,17 @@ if neg buf[pos] = UInt8('-') pos += 1 + elseif plus + buf[pos] = UInt8('+') + pos += 1 + elseif space + buf[pos] = UInt8(' ') + pos += 1 end buf[pos] = UInt8('0') pos += 1 if precision > 0 - buf[pos] = UInt8('.') + buf[pos] = decchar pos += 1 if trimtrailingzeros precision = 1 @@ -19,6 +27,9 @@ buf[pos] = UInt8('0') pos += 1 end + elseif hash + buf[pos] = decchar + pos += 1 end return pos elseif isnan(x) @@ -29,11 +40,18 @@ elseif !isfinite(x) if neg buf[pos] = UInt8('-') + pos += 1 + elseif plus + buf[pos] = UInt8('+') + pos += 1 + elseif space + buf[pos] = UInt8(' ') + pos += 1 end - buf[pos + neg] = UInt8('I') - buf[pos + neg + 1] = UInt8('n') - buf[pos + neg + 2] = UInt8('f') - return pos + neg + 3 + buf[pos] = UInt8('I') + buf[pos + 1] = UInt8('n') + buf[pos + 2] = UInt8('f') + return pos + 3 end bits = Core.bitcast(UInt64, x) @@ -51,6 +69,12 @@ if neg buf[pos] = UInt8('-') pos += 1 + elseif plus + buf[pos] = UInt8('+') + pos += 1 + elseif space + buf[pos] = UInt8(' ') + pos += 1 end if e2 >= -52 idx = e2 < 0 ? 0 : indexforexp(e2) @@ -75,8 +99,8 @@ buf[pos] = UInt8('0') pos += 1 end - if precision > 0 - buf[pos] = UInt8('.') + if precision > 0 || hash + buf[pos] = decchar pos += 1 end if e2 < 0 @@ -144,14 +168,14 @@ buf[roundPos + 1] = UInt8('1') if dotPos > 1 buf[dotPos] = UInt8('0') - buf[dotPos + 1] = UInt8('.') + buf[dotPos + 1] = decchar end buf[pos] = UInt8('0') pos += 1 break end c = roundPos > 0 ? buf[roundPos] : 0x00 - if c == UInt8('.') + if c == decchar dotPos = roundPos continue elseif c == UInt8('9') @@ -177,7 +201,7 @@ while buf[pos - 1] == UInt8('0') pos -= 1 end - if buf[pos - 1] == UInt8('.') + if buf[pos - 1] == decchar pos += 1 end end