Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

118
LINES

< > BotCompany Repo | #153 // tstr.lua (lua-nucleo)

Lua code

--------------------------------------------------------------------------------
--- Visualization of non-recursive tables.
-- @module lua-nucleo.tstr
-- This file is a part of lua-nucleo library
-- @copyright lua-nucleo authors (see file `COPYRIGHT` for the license)
--------------------------------------------------------------------------------

local pairs, ipairs, type, tostring = pairs, ipairs, type, tostring
local table_concat = table.concat
local string_match, string_format = string.match, string.format

--local lua51_keywords = import 'lua-nucleo/language.lua' { 'lua51_keywords' }
--local serialize_number = import 'lua-nucleo/string.lua' { 'serialize_number' }
local lua51_keywords = get('#155').lua51_keywords
local serialize_number = get('#156').serialize_number

local tstr, tstr_cat
do
  local function impl(t, cat, visited)
    local t_type = type(t)
    if t_type == "table" then
      if not visited[t] then
        visited[t] = true

        cat("{")

        -- Serialize numeric indices

        local next_i = 0
        for i, v in ipairs(t) do -- TODO: Lua 5.2 warning. Rewrite.
          if i > 1 then -- TODO: Move condition out of the loop
            cat(",")
          end
          impl(v, cat, visited)
          next_i = i
        end
        next_i = next_i + 1

        -- Serialize hash part
        -- Skipping comma only at first element if there is no numeric part.
        local need_comma = (next_i > 1)
        for k, v in pairs(t) do
          local k_type = type(k)
          if k_type == "string" then
            if need_comma then
              cat(",")
            end
            need_comma = true

            -- TODO: Need "%q" analogue, which would put quotes
            -- only if string does not match regexp below
            if not lua51_keywords[k] and string_match(k, "^[%a_][%a%d_]*$") then
              cat(k) cat("=")
            else
              cat(string_format("[%q]=", k))
            end
            impl(v, cat, visited)
          else
            if
              k_type ~= "number" or -- non-string non-number
              k >= next_i or k < 1 or -- integer key in hash part of the table
              k % 1 ~= 0 -- non-integer key
            then
              if need_comma then
                cat(",")
              end
              need_comma = true

              cat("[")
              impl(k, cat, visited)
              cat("]=")
              impl(v, cat, visited)
            end
          end
        end

        cat("}")

        visited[t] = nil
      else
        -- Note this loses information on recursive tables
        cat('"table (recursive)"')
      end
    elseif t_type == "number" then
      cat(serialize_number(t))
    elseif t_type == "boolean" then
      cat(tostring(t))
    elseif t == nil then
      cat("nil")
    else
      -- Note this converts non-serializable types to strings
      cat(string_format("%q", tostring(t)))
    end
  end

  -- This function is optimized for logging.
  -- Unless you are serializing "simple" tables,
  -- do not use this function for serialization.
  -- This function intentionally loses information on nested recursive tables
  -- and on non-serializable types like functions, threads and userdata.

  tstr_cat = function(cat, t)
    impl(t, cat, {})
  end

  tstr = function(t)
    local buf = {}
    local cat = function(v) buf[#buf + 1] = v end
    impl(t, cat, {})
    return table_concat(buf)
  end
end

return
{
  tstr = tstr;
  tstr_cat = tstr_cat;
}

Author comment

Prints tables in one line.

Not working in TinyBrain as-is. Try serpent (#158).

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 3622 [visualize]

Snippet ID: #153
Snippet name: tstr.lua (lua-nucleo)
Eternal ID of this version: #153/1
Text MD5: 2d2d687b3c03ad36062046c1b52701ab
Author: stefan
Category: table serializers
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-01-23 02:51:22
Source code size: 3622 bytes / 118 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 948 / 216
Referenced in: [show references]