Arthur Reutenauer wrote:
local optimized_string = formatted_string:gsub('^(%d*\.%d-)(0*)$', '%1')
Actually, I just realized that the primitive tostring does exactly the same transformation (probably in a much safer way), so we can use it in the much more complete code:
====
-- Thank you, Lua-users wiki :-) -- http://lua-users.org/wiki/VarargTheSecondClassCitizen Issue #7
-- The first two functions below implement a map over a "..." list local function map_rec(f, n, a, ...) if n > 0 then return f(a), map_rec(f, n-1, ...) end end
local function map(f, ...) return map_rec(f, select('#', ...), ...) end
-- Our "better formatting" function function string.better_format(form, ...) -- Every number-kind-of-format -> %s form = form:gsub('%%[0-9]-%.?[0-9]-f', '%%s') form = form:gsub('%%d', '%%s') form = form:gsub('%%i', '%%s') -- Then make every argument in the list into a string return(string.format(form, map(tostring, ...))) end
-- Now Lua will happily strip the trailing zeroes print(string.better_format("%f %f m", 94.486000, 30.000)) print(string.better_format("%5f %.7f l", .577, 2.7828)) print(string.better_format("%5f %.7f %d l", 3.14000, 2.7828, 3))
this is a bit weird case ... on the one hand you specify %5f and such but that's ignored i.e. becomes %s so why not use %s in the first place then so, you could have said print(string.format("%s %s m", 94.486000, 30.000)) print(string.format("%s %s l", .577, 2.7828)) print(string.format("%s %s %s l", 3.14000, 2.7828, 3)) which is then way faster anyway, originally i used %s but when taco and i played with the converter and did some performance tests we found out that %f is faster (unless > 6 digits specified) a more general speed improvement is to set the pdf compression to 3 i may add a stripper (more general one) once i reimplement the backend (all backend stuff in mkiv is temporary) Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------