Skip to content

Instantly share code, notes, and snippets.

@SkyyySi
Last active September 1, 2022 19:46
Show Gist options
  • Save SkyyySi/01363cc7acde36123750e377d501b4e6 to your computer and use it in GitHub Desktop.
Save SkyyySi/01363cc7acde36123750e377d501b4e6 to your computer and use it in GitHub Desktop.
A nice string conversion module for tables

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

--- Pro tip: You can also use this as the actual string conversion used
--- by a table using metaprogramming, like this:
--[[
local table_to_string = require("table_to_string")
local mytable, mt = {}, {}
mt.__tostring = table_to_string
setmetatable(mytable, mt)
--]]
---@param s string
---@return string, integer
local function string_escape(s)
return s:gsub("\\", [[\\]])
:gsub("\a", [[\a]])
:gsub("\b", [[\b]])
:gsub("\f", [[\f]])
:gsub("\n", [[\n]])
:gsub("\r", [[\r]])
:gsub("\t", [[\t]])
:gsub("\v", [[\v]])
:gsub("\"", [[\"]])
:gsub("\'", [[\']])
end
---@param str string
---@param n number
---@return string
local function string_multiply(str, n)
if n <= 0 then
return ""
end
local outs = ""
local floor = math.floor(n)
local point = n - floor
for i = 0, n do
outs = outs..str
end
if point > 0 then
local len = #str * floor
outs = outs..str:sub(1, math.floor(len))
end
return outs
end
---@param t table
---@param indent? string
---@param depth? integer
---@return string
local function table_to_string(t, indent, depth)
indent = indent or " "
depth = depth or 0
local bracket_indent = string_multiply(indent, depth)
local full_indent = bracket_indent..indent
local outs = bracket_indent.."{\n"
for k, v in pairs(t) do
local tv = type(v)
local tk = type(k)
if tv == "table" then
outs = outs..table_to_string(v, indent, depth + 1).."\n"
else
if tk == "string" then
k = '"'..string_escape(k)..'"'
end
if tv == "string" then
v = '"'..string_escape(v)..'"'
end
if tk == "function" or tk == "thread" or tk == "userdata" then
k = '[['..tostring(k)..']]'
end
if tv == "function" or tv == "thread" or tv == "userdata" then
v = '[['..tostring(v)..']]'
end
outs = ("%s%s[%s] = %s,\n"):format(outs, full_indent, k, v)
end
end
return outs..bracket_indent.."}"
end
return table_to_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment