Drakonas

textutils (OpenComputers)

Apr 22nd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. local textutils = {}
  2.  
  3. local g_tLuaKeywords = {
  4.     [ "and" ] = true,
  5.     [ "break" ] = true,
  6.     [ "do" ] = true,
  7.     [ "else" ] = true,
  8.     [ "elseif" ] = true,
  9.     [ "end" ] = true,
  10.     [ "false" ] = true,
  11.     [ "for" ] = true,
  12.     [ "function" ] = true,
  13.     [ "if" ] = true,
  14.     [ "in" ] = true,
  15.     [ "local" ] = true,
  16.     [ "nil" ] = true,
  17.     [ "not" ] = true,
  18.     [ "or" ] = true,
  19.     [ "repeat" ] = true,
  20.     [ "return" ] = true,
  21.     [ "then" ] = true,
  22.     [ "true" ] = true,
  23.     [ "until" ] = true,
  24.     [ "while" ] = true,
  25. }
  26.  
  27. local function serializeImpl( t, tTracking, sIndent )
  28.     local sType = type(t)
  29.     if sType == "table" then
  30.         if tTracking[t] ~= nil then
  31.             error( "Cannot serialize table with recursive entries", 0 )
  32.         end
  33.         tTracking[t] = true
  34.  
  35.         if next(t) == nil then
  36.             -- Empty tables are simple
  37.             return "{}"
  38.         else
  39.             -- Other tables take more work
  40.             local sResult = "{\n"
  41.             local sSubIndent = sIndent .. "  "
  42.             local tSeen = {}
  43.             for k,v in ipairs(t) do
  44.                 tSeen[k] = true
  45.                 sResult = sResult .. sSubIndent .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  46.             end
  47.             for k,v in pairs(t) do
  48.                 if not tSeen[k] then
  49.                     local sEntry
  50.                     if type(k) == "string" and not g_tLuaKeywords[k] and string.match( k, "^[%a_][%a%d_]*$" ) then
  51.                         sEntry = k .. " = " .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  52.                     else
  53.                         sEntry = "[ " .. serializeImpl( k, tTracking, sSubIndent ) .. " ] = " .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  54.                     end
  55.                     sResult = sResult .. sSubIndent .. sEntry
  56.                 end
  57.             end
  58.             sResult = sResult .. sIndent .. "}"
  59.             return sResult
  60.         end
  61.        
  62.     elseif sType == "string" then
  63.         return string.format( "%q", t )
  64.    
  65.     elseif sType == "number" or sType == "boolean" or sType == "nil" then
  66.         return tostring(t)
  67.        
  68.     else
  69.         error( "Cannot serialize type "..sType, 0 )
  70.        
  71.     end
  72. end
  73.  
  74. EMPTY_ARRAY = {}
  75.  
  76. local function serializeJSONImpl( t, tTracking )
  77.     local sType = type(t)
  78.     if t == EMPTY_ARRAY then
  79.         return "[]"
  80.  
  81.     elseif sType == "table" then
  82.         if tTracking[t] ~= nil then
  83.             error( "Cannot serialize table with recursive entries", 0 )
  84.         end
  85.         tTracking[t] = true
  86.  
  87.         if next(t) == nil then
  88.             -- Empty tables are simple
  89.             return "{}"
  90.         else
  91.             -- Other tables take more work
  92.             local sObjectResult = "{"
  93.             local sArrayResult = "["
  94.             local nObjectSize = 0
  95.             local nArraySize = 0
  96.             for k,v in pairs(t) do
  97.                 if type(k) == "string" then
  98.                     local sEntry = serializeJSONImpl( k, tTracking ) .. ":" .. serializeJSONImpl( v, tTracking )
  99.                     if nObjectSize == 0 then
  100.                         sObjectResult = sObjectResult .. sEntry
  101.                     else
  102.                         sObjectResult = sObjectResult .. "," .. sEntry
  103.                     end
  104.                     nObjectSize = nObjectSize + 1
  105.                 end
  106.             end
  107.             for n,v in ipairs(t) do
  108.                 local sEntry = serializeJSONImpl( v, tTracking )
  109.                 if nArraySize == 0 then
  110.                     sArrayResult = sArrayResult .. sEntry
  111.                 else
  112.                     sArrayResult = sArrayResult .. "," .. sEntry
  113.                 end
  114.                 nArraySize = nArraySize + 1
  115.             end
  116.             sObjectResult = sObjectResult .. "}"
  117.             sArrayResult = sArrayResult .. "]"
  118.             if nObjectSize > 0 or nArraySize == 0 then
  119.                 return sObjectResult
  120.             else
  121.                 return sArrayResult
  122.             end
  123.         end
  124.  
  125.     elseif sType == "string" then
  126.         return string.format( "%q", t )
  127.  
  128.     elseif sType == "number" or sType == "boolean" then
  129.         return tostring(t)
  130.  
  131.     else
  132.         error( "Cannot serialize type "..sType, 0 )
  133.  
  134.     end
  135. end
  136.  
  137. function textutils.serialize( t )
  138.     local tTracking = {}
  139.     return serializeImpl( t, tTracking, "" )
  140. end
  141.  
  142. function textutils.unserialize( s )
  143.     local func = load( "return "..s, "unserialize" )
  144.     if func then
  145.         local ok, result = pcall( func )
  146.         if ok then
  147.             return result
  148.         end
  149.     end
  150.     return nil
  151. end
  152.  
  153. function textutils.serializeJSON( t )
  154.     local tTracking = {}
  155.     return serializeJSONImpl( t, tTracking )
  156. end
  157.  
  158. function textutils.urlEncode( str )
  159.     if str then
  160.         str = string.gsub(str, "\n", "\r\n")
  161.         str = string.gsub(str, "([^%w ])", function(c)
  162.             return string.format("%%%02X", string.byte(c))
  163.         end )
  164.         str = string.gsub(str, " ", "+")
  165.     end
  166.     return str    
  167. end
  168.  
  169. -- GB versions
  170. serialise = serialize
  171. unserialise = unserialize
  172. serialiseJSON = serializeJSON
  173.  
  174. return textutils
Add Comment
Please, Sign In to add comment