Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local val_to_str, key_to_str, tabletostring
- local char, byte, gsub, match, sub, find =
- string.char, string.byte, string.gsub, string.match, string.sub, string.find
- local insert, concat = table.insert, table.concat
- function val_to_str ( v )
- if "string" == type( v ) then
- v = gsub( v, "\n", "\\n" )
- if match( gsub(v,"[^'\"]",""), '^"+$' ) then
- return "'" .. v .. "'"
- end
- return '"' .. gsub(v,'"', '\\"' ) .. '"'
- else
- return "table" == type( v ) and tabletostring( v ) or
- tostring( v )
- end
- end
- function key_to_str ( k )
- if "string" == type( k ) and match( k, "^[_%a][_%a%d]*$" ) then
- return k
- else
- return "[" .. val_to_str( k ) .. "]"
- end
- end
- function tabletostring( tbl )
- local result, done = {}, {}
- for k, v in ipairs( tbl ) do
- insert( result, val_to_str( v ) )
- done[ k ] = true
- end
- for k, v in pairs( tbl ) do
- if not done[ k ] then
- insert( result,
- key_to_str( k ) .. "=" .. val_to_str( v ) )
- end
- end
- return "{" .. concat( result, "," ) .. "}"
- end
- if _ARGS == '' then return 'error.' end
- local tab = _ENV[_ARGS]
- local bool = false
- if _ARGS == '_ENV' then tab = _ENV; bool = true end
- if type(tab) ~= 'table' then return 'error.' end
- return (bool and '_ENV' or _ARGS) .. ' = ' .. tabletostring(tab)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement