From 367f03be4a44061ce9662fb773f808e71b36090a Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sun, 1 Jan 2023 12:50:35 -0800 Subject: [PATCH] fix section before first "%" oops, need to special-case the first part of the split list. amusingly, I don't need to special-case zero-length format strings because that will skip the entire loop and output "", which seems correct. (I am also not special-casing zero-length segments because `s[1] == nil` will go into the error handler and that seems fine. --- chameleonic.p8 | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index ea33d55..51427dd 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -44,23 +44,29 @@ end -- %! for tostring(param) -- which dumps tables function fmt(f, ...) - local out, i = "", 1 + local out, i = "", 0 for s in all(split(f,"%")) do - local m = s[1] - if m == "~" then - out ..= "%" + if i == 0 then + -- before first format directive + out ..= s + i = 1 else - local p = select(i,...) - i+=1 - if m == "v" then - out ..= p - elseif m == "!" then - out ..= tostring(p) + local m = s[1] + if m == "~" then + out ..= "%" else - out ..= "(?!:"..m..":"..tostring(p)..")" + local p = select(i,...) + i+=1 + if m == "v" then + out ..= p + elseif m == "!" then + out ..= tostring(p) + else + out ..= "(?!:"..m..":"..tostring(p)..")" + end end + out ..=sub(s,2) end - out ..=sub(s,2) end return out end