mirror of
https://github.com/GreenComfyTea/MHR-Overlay.git
synced 2026-01-25 12:58:06 -08:00
Change table tostring function to a better one
This commit is contained in:
@@ -68,18 +68,96 @@ function table_helpers.merge(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function table_helpers.tostring(table_)
|
function table_helpers.tostring(table_)
|
||||||
if type(table_) == "table" then
|
if type(table_) == "number" or type(table_) == "boolean" or type(table_) == "string" then
|
||||||
local s = "{ \n";
|
|
||||||
for k, v in pairs(table_) do
|
|
||||||
if type(k) ~= "number" then
|
|
||||||
k = "\"" .. k .. "\"";
|
|
||||||
end
|
|
||||||
s = s .. "\t[" .. k .. "] = " .. table_helpers.tostring(v) .. ",\n";
|
|
||||||
end
|
|
||||||
return s .. "} \n";
|
|
||||||
else
|
|
||||||
return tostring(table_);
|
return tostring(table_);
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if table_helpers.is_empty(table_) then
|
||||||
|
return "{}";
|
||||||
|
end
|
||||||
|
|
||||||
|
local cache = {};
|
||||||
|
local stack = {};
|
||||||
|
local output = {};
|
||||||
|
local depth = 1;
|
||||||
|
local output_str = "{\n";
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local size = 0;
|
||||||
|
for k,v in pairs(table_) do
|
||||||
|
size = size + 1;
|
||||||
|
end
|
||||||
|
|
||||||
|
local cur_index = 1;
|
||||||
|
for k,v in pairs(table_) do
|
||||||
|
if cache[table_] == nil or cur_index >= cache[table_] then
|
||||||
|
|
||||||
|
if string.find(output_str, "}", output_str:len()) then
|
||||||
|
output_str = output_str .. ",\n";
|
||||||
|
elseif not string.find(output_str, "\n", output_str:len()) then
|
||||||
|
output_str = output_str .. "\n";
|
||||||
|
end
|
||||||
|
|
||||||
|
-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
|
||||||
|
table.insert(output,output_str);
|
||||||
|
output_str = "";
|
||||||
|
|
||||||
|
local key;
|
||||||
|
if type(k) == "number" or type(k) == "boolean" then
|
||||||
|
key = "[" .. tostring(k) .. "]";
|
||||||
|
else
|
||||||
|
key = "['" .. tostring(k) .. "']";
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(v) == "number" or type(v) == "boolean" then
|
||||||
|
output_str = output_str .. string.rep('\t', depth) .. key .. " = "..tostring(v);
|
||||||
|
elseif type(v) == "table" then
|
||||||
|
output_str = output_str .. string.rep('\t', depth) .. key .. " = {\n";
|
||||||
|
table.insert(stack, table_);
|
||||||
|
table.insert(stack, v);
|
||||||
|
cache[table_] = cur_index + 1;
|
||||||
|
break;
|
||||||
|
else
|
||||||
|
output_str = output_str .. string.rep('\t', depth) .. key .. " = '" .. tostring(v) .. "'";
|
||||||
|
end
|
||||||
|
|
||||||
|
if cur_index == size then
|
||||||
|
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}";
|
||||||
|
else
|
||||||
|
output_str = output_str .. ",";
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- close the table
|
||||||
|
if cur_index == size then
|
||||||
|
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}";
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
cur_index = cur_index + 1;
|
||||||
|
end
|
||||||
|
|
||||||
|
if size == 0 then
|
||||||
|
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}";
|
||||||
|
end
|
||||||
|
|
||||||
|
if #stack > 0 then
|
||||||
|
table_ = stack[#stack];
|
||||||
|
stack[#stack] = nil;
|
||||||
|
depth = cache[table_] == nil and depth + 1 or depth - 1;
|
||||||
|
else
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
|
||||||
|
table.insert(output, output_str);
|
||||||
|
output_str = table.concat(output);
|
||||||
|
|
||||||
|
return output_str;
|
||||||
|
end
|
||||||
|
|
||||||
|
function table_helpers.tostringln(table_)
|
||||||
|
return "\n" .. table_helpers.tostring(table_);
|
||||||
end
|
end
|
||||||
|
|
||||||
function table_helpers.is_empty(table_)
|
function table_helpers.is_empty(table_)
|
||||||
|
|||||||
Reference in New Issue
Block a user