implement fake sprintf

this spends extra tokens to handle "invalid format character" to catch using a % where %% is intended. this is designed to grow with further format chars if needed; I have ideas for not-exactly-POSIX %d, %x, %h, %!, %#, %c, and possibly others but there is absolutely no reason to spend tokens on these things until we need them. (that said, existing debugging output might benefit from some of these other formats, but that debug code is commented out, so maybe nevert.)
This commit is contained in:
Kistaro Windrider 2023-01-01 12:33:16 -08:00
parent aefdfeb99c
commit b0bca27239
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -38,6 +38,29 @@ function cycle(tbl,period)
return tbl[t()%period*#tbl\period+1] return tbl[t()%period*#tbl\period+1]
end end
-- fake sprintf function
-- %% for literal "%"
-- %v for param
function sprintf(fmt, ...)
local out, i = "", 1
for s in all(split(fmt,"%")) do
local m = s[1]
if m == "%" then
out ..= "%"
else
local p = select(i,...)
i+=1
if m == "v" then
out ..= select(i,...)
else
out ..= "(?!:"..m..":"..tostr(p)..")"
end
out ..=sub(s,2)
end
return out
end
mnames={} mnames={}
function names(root) function names(root)
local n=mnames[root] local n=mnames[root]