--[[抽取脚本工具类]] common={} --根据field获取结果对象 function common.getFieldObjects(result,field) for key, val in pairs(result) do if key==field then return val end end return nil end --抽取field新增结果赋值object(field,code,ruletext,extfrom,field,value,type,matchtype) function common.setFieldObjects(result,field,object) local nofield=true for key, val in pairs(result) do if key==field then nofield=false table.insert(val, object) end end if nofield then result[field]={object} end return result end --输出 function printf(obj) print(dump(obj) ) end function dump(obj) local getIndent, quoteStr, wrapKey, wrapVal, isArray, dumpObj getIndent = function(level) return string.rep("\t", level) end quoteStr = function(str) str = string.gsub(str, "[%c\\\"]", { ["\t"] = "\\t", ["\r"] = "\\r", ["\n"] = "\\n", ["\""] = "\\\"", ["\\"] = "\\\\", }) return '"' .. str .. '"' end wrapKey = function(val) if type(val) == "number" then return "[" .. val .. "]" elseif type(val) == "string" then return "[" .. quoteStr(val) .. "]" else return "[" .. tostring(val) .. "]" end end wrapVal = function(val, level) if type(val) == "table" then return dumpObj(val, level) elseif type(val) == "number" then return val elseif type(val) == "string" then return quoteStr(val) else return tostring(val) end end local isArray = function(arr) local count = 0 for k, v in pairs(arr) do count = count + 1 end for i = 1, count do if arr[i] == nil then return false end end return true, count end dumpObj = function(obj, level) if type(obj) ~= "table" then return wrapVal(obj) end level = level + 1 local tokens = {} tokens[#tokens + 1] = "{" local ret, count = isArray(obj) if ret then for i = 1, count do tokens[#tokens + 1] = getIndent(level) .. wrapVal(obj[i], level) .. "," end else for k, v in pairs(obj) do tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. "," end end tokens[#tokens + 1] = getIndent(level - 1) .. "}" return table.concat(tokens, "\n") end return dumpObj(obj, 0) end --通用方法结束 return common;