I have a function as a PoC which does not work although it works very well from the commandline lua formatNumber.lua.
What’s happenning here, please? Tests (just add to the end of the file and run it with Lua interpreter)…
Ok, unless you followed these instructions Living on the edge (builds) you’re on a regular release, which is pretty old. Please try out edge builds. I fixed a lot there.
Is there any way for me to debug the Lua run somehow (I started with cca a week ago)? By the way, if the small function variadicTest() above works as expected (which it seems it does), the problem doesn’t seem to be connected to the variadicity itself or, at least, it’s just started to look like it isn’t…
@zef Is there any chance you looked into this issue, please? I’ve written some more variadic functions for misc. things (for example I use markdown.parseMarkdown() along with custom table function to traverse the AST and produce abbreviated preview for page listings skipping code blocks etc., and some of the utility table/tree functions happened to be variadic in order to be universal). I suppose it’s been not fixed yet. (?)
Ok, I just looked at this and I’m not sure where this code comes from exactly, but it seems to use Lua features that are not standard as far as I can find. I updated the code as follows:
local function formatNumber(format, ...)
local args = { ... }
local argind = 1
local res = ''
local i = 1
while i <= string.len(format) do
local chr = string.sub(format, i, i)
if chr == '%' then
i = i + 1
local padchr = '0'
local padcnt = 0
if string.match(string.sub(format, i, i), '[^%dN]') then
padchr = string.sub(format, i, i)
i = i + 1
end
local cntstr = ''
while string.match(string.sub(format, i, i), '%d') do
cntstr = cntstr .. string.sub(format, i, i)
i = i + 1
end
if cntstr ~= '' then
padcnt = tonumber(cntstr)
end
if string.sub(format, i, i) == 'N' then
if argind <= #args then
local num = tostring(args[argind])
local neg = false
if string.sub(num, 1, 1) == '-' then
neg = true
num = string.sub(num, 2)
end
if padcnt == 0 and padchr ~= '0' then
padcnt = 2
end
local pad = string.rep(padchr,
math.max(0, padcnt - #num - (neg and 1 or 0)))
if neg and string.match(padchr, '%d') then
res = res .. '-' .. pad .. num
else
res = res .. pad .. (neg and '-' or '') .. num
end
argind = argind + 1
i = i + 1
else
error('Not enough arguments!')
end
else
res = res .. '%' .. string.sub(format, i, i)
i = i + 1
end
else
res = res .. chr
i = i + 1
end
end
if argind <= #args then
error('Too many arguments!')
end
return res
end
Although I don’t know exactly what the formats are supposed to output, at least it seems to render strings now.
Ok, implemented this now (just pushed). Your function should work as is (without my modifications). At least if the pattern match library is properly implemented at the moment, which I’m not 100% convinced of.
`[\n]` works:
${string.gsub(table.concat({"Start of string","Start of newline"},"\n"),"[\n]Start","\nSTART").values[1]}
`^` works:
${string.gsub(table.concat({"Start of string","Start of newline"},"\n"),"^Start","\nSTART").values[1]}
`[^]` does not work:
${string.gsub(table.concat({"Start of string","Start of newline"},"\n"),"[^]Start","\nSTART").values[1]}
`[\n^]` does not work:
${string.gsub(table.concat({"Start of string","Start of newline"},"\n"),"[^\n]Start","\nSTART").values[1]}
`[abc]` works: ${string.gsub("abc","[abc]","A").values[1]}
Honestly I largely let Claude (AI) generate this pattern matching stuff. I’ll have to give it another shot, feeding it more docs and tests to work with.