parsav  Diff

Differences From Artifact [c868f8cd22]:

To Artifact [31fe76fcd7]:


    20     20   	if val then return os.execute(cmd) else
    21     21   		local fd = io.popen(cmd,'r')
    22     22   		local t = fd:read('*a')
    23     23   		return chomp(t), fd:close()
    24     24   	end
    25     25   end
    26     26   
    27         -local function dump(v,pfx,cyc)
           27  +local function copy(a)
           28  +	local new = {}
           29  +	for k,v in pairs(a) do new[k] = v end
           30  +	return new
           31  +end
           32  +
           33  +local function cat(a,b)
           34  +	a = copy(a)
           35  +	local ofs = #a
           36  +	for k,v in pairs(b) do
           37  +		if type(k) == 'number' then
           38  +			a[k+ofs] = v
           39  +		else a[k] = v end
           40  +	end
           41  +	return a
           42  +end
           43  +
           44  +local function search(tbl,pred,lst,path)
           45  +	lst = lst or {} path = path or {}
           46  +	if type(pred) ~= 'function' then
           47  +		local val = pred
           48  +		pred = function(a,k)
           49  +			if type(a) == 'table' and a ~= val then return end
           50  +			return a == val
           51  +		end
           52  +	end
           53  +	for k,v in pairs(tbl) do
           54  +		local res = pred(v,k)
           55  +		local np = cat(path, {tbl})
           56  +		if res == true then
           57  +			table.insert(lst, {
           58  +				key = k;
           59  +				value = v;
           60  +				parent = tbl;
           61  +				path = np;
           62  +			})
           63  +		elseif res == nil then
           64  +			search(v,pred,lst,np)
           65  +		end
           66  +	end
           67  +	return lst
           68  +end
           69  +
           70  +local function dump(v,pfx,cyc,ismeta)
    28     71   	pfx = pfx or ''
    29     72   	cyc = cyc or {}
    30     73   	local np = pfx .. '  '
    31     74   
    32     75   	if type(v) == 'table' then
    33     76   		if cyc[v] then return '<...>' else cyc[v] = true end
    34     77   	end
................................................................................
    37     80   		return string.format('%q', v)
    38     81   	elseif type(v) == 'table' then
    39     82   		local str = ''
    40     83   		for k,v in pairs(v) do
    41     84   			local tkey, tval = dump(k,np,cyc), dump(v,np,cyc)
    42     85   			str = str .. string.format('%s[%s] = %s\n', np, tkey,tval)
    43     86   		end
    44         -		return '{\n' .. str .. pfx .. '}\n'
           87  +		local meta = ''
           88  +		if getmetatable(v) then
           89  +			meta = dump(getmetatable(v),pfx,cyc,true) .. '::'
           90  +		end
           91  +		if ismeta then
           92  +			return string.format('%s<|\n%s%s|>',meta,str,pfx)
           93  +		else
           94  +			return meta..'{\n' .. str .. pfx .. '}\n'
           95  +		end
    45     96   	else
    46     97   		return string.format('%s', v)
    47     98   	end
    48     99   end
          100  +
    49    101   local ping = function(path)
    50    102   	local f = io.open(path)
    51    103   	if f then f:close() return true end
    52    104   	return false
    53    105   end
    54    106   local tobool = function(s)
    55    107   	if s == true then return true
................................................................................
    68    120   	local seed = 1 for i = 1, 8 do
    69    121   		seed = seed * string.byte(string.sub(ent,i,i))
    70    122   	end
    71    123   	math.randomseed(seed)
    72    124   else math.randomseed(os.time()) end
    73    125   
    74    126   return {
    75         -	exec = exec;
    76    127   	dump = dump;
    77         -	ping = ping;
    78         -	chomp = chomp;
    79         -	map = map;
    80         -	tobool = tobool;
          128  +	exec = exec, ping = ping;
          129  +	map = map, copy = copy, cat = cat, search = search;
          130  +	chomp = chomp, tobool = tobool;
    81    131   	find = function(lst,pred)
    82    132   		for k,v in pairs(lst) do
    83    133   			local test = pred(v,k) 
    84    134   			if test then return test end
    85    135   		end
    86    136   		return nil
    87    137   	end;
................................................................................
    94    144   			s = s .. string.char(ofs) 
    95    145   		end
    96    146   		return s
    97    147   	end;
    98    148   	append = function(a,b)
    99    149   		for _, v in pairs(b) do a[#a+1] = v end
   100    150   	end;
   101         -	has = function(haystack,needle,eq)
   102         -		eq = eq or function(a,b) return a == b end
          151  +	has = function(haystack,pred)
          152  +		if type(pred) ~= 'function' then
          153  +			local val = pred
          154  +			pred = function(a) return a == val end
          155  +		end
   103    156   		for k,v in pairs(haystack) do
   104         -			if eq(needle,v) then return k end
          157  +			if pred(v,k) then return k,v end
   105    158   		end
   106    159   	end;
   107    160   	keys = function(ary)
   108    161   		local kt = {}
   109    162   		for k,v in pairs(ary) do kt[#kt+1] = k end
   110    163   		return kt
   111    164   	end;