Advertisement
draugath

Untitled

Jan 28th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.80 KB | None | 0 0
  1. function test.stringify(v, nobinary, dump, count)
  2. --Returns the string value of the parameter passed to it.
  3. --Works with nspickle() to display the contents of tables that contain binary data
  4. --Setting nobinary to true will strip binary data from the string form of tables.
  5.     local fc = test.fgcolor['white']
  6.     local function nspickle(tbl, nobinary, count)
  7.         local msg = ''
  8.         local indent = ''; if (dump) then indent = '\n'..string.rep('\t', count-1) end
  9.         local idx = ''
  10.         local n = 0
  11.        
  12.         for i, v in ipairs(tbl) do
  13.             local tv = type(v)
  14.             if (not (nobinary and (tv == 'function' or tv == 'userdata' or tv == 'thread'))) then
  15.                 if (dump) then idx = '['..tostring(i)..']=' else idx = '' end
  16.                 msg = msg..indent..idx..test.stringify(v, nobinary, dump, count)..','
  17.                 n = i
  18.             else
  19.                 break
  20.             end
  21.         end
  22.         msg = msg:gsub(',$', ';')
  23.         for k, v in pairs(tbl) do
  24.             local tk = type(k)
  25.             local tv = type(v)
  26.             if (tk ~= 'number' or (tk == 'number' and (k < 1 or k > n))) then
  27.                 if (tk == 'number') then
  28.                     k = '['..k..']'
  29.                 elseif (tk == 'string' and k:match('[^%w_]')) then
  30.                     k = '["'..k..'"]'
  31.                 end
  32.                 if (not (nobinary and (tv == 'function' or tv == 'userdata' or tv == 'thread'))) then
  33.                     msg = msg..indent..tostring(k)..'='..test.stringify(v, nobinary, dump, count)..','
  34.                 end
  35.             end
  36.         end
  37.         msg = msg:gsub('[,;]$', '')
  38.         return msg
  39.     end
  40.  
  41.     local t = type(v)
  42.     if (t == 'table') then
  43.         count = (count or 0) + 1
  44.         local pre, post = '{', '}'
  45.         if (dump) then
  46.             --pre = string.rep('\t', count - 2)..'{'
  47.             post = '\n'..string.rep('\t', count-2)..'}'
  48.         end
  49.         v = pre..nspickle(v, nobinary, count)..post
  50.         count = count - 1
  51.         if (count == 0) then v = v:gsub('^{(.*)}$', '%1') end
  52.     elseif (t == 'string') then v = '"'..v..'"'
  53.     else v = tostring(v)
  54.     end
  55.     return v
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement