LIVM/faxm/string.lua

36 lines
1.1 KiB
Lua
Raw Permalink Normal View History

2022-04-09 00:20:48 +03:00
return {
source = function(source)
2022-04-09 11:18:01 +03:00
local tl = {}
2022-04-09 00:20:48 +03:00
while source:match[[|"[^"]+"]] do
local s = source:match[[|"[^"]+"]]
local ss = s
s = s:sub(3, #s - 1)
local r = {}
for i = 0, math.floor(#s / 8) do
local t = s:sub(i * 8 + 1, (i + 1) * 8)
while #t < 8 do t = t .. "\0" end
t = {string.byte(t, 1, -1)}
local rt = {}
for i = 1, 8 do rt[9 - i] = t[i] end
for i = 1, 8 do rt[i] = string.format("%02x", rt[i]) end
if table.concat(rt, "") ~= "0000000000000000" then
r[#r + 1] = "|x" .. table.concat(rt, "")
end
end
2022-04-09 11:18:01 +03:00
if #(r[#r]:gsub("000", "")) == #(r[#r]) then
r[#r + 1] = "|x0000000000000000"
end
2022-04-09 00:20:48 +03:00
local function regexEscape(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
end
2022-04-09 11:18:01 +03:00
local tt = table.concat(r, ", ")
tl[tt] = #r
source = source:gsub(regexEscape(ss), tt)
2022-04-09 00:20:48 +03:00
end
2022-04-09 11:18:01 +03:00
source = source:gsub("(string%s+(%w+)%s?:(%s+)([|xa-f0-9, ]+)[^;])", function(found)
found = "object" .. found:sub(7)
return found .. " ? length=|" .. tostring(tl[found:gsub("object%s+%w+%s?:(%s+)", "")] * 8)
end)
2022-04-09 00:20:48 +03:00
return source
end
}