From 3494c48e7425b36823266279b43dcde5fff2a1fa Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sun, 1 Jan 2023 12:33:16 -0800 Subject: [PATCH] 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.) --- chameleonic.p8 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/chameleonic.p8 b/chameleonic.p8 index bc9028a..2522e0c 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -38,6 +38,29 @@ function cycle(tbl,period) return tbl[t()%period*#tbl\period+1] 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={} function names(root) local n=mnames[root]