-- lua扩展 -- table扩展 -- 返回table大小 table.size = function(t) local count = 0 for _ in pairs(t) do count = count + 1 end return count end --返回table的最大value table.maxn = function(t) local maxn = nil for _, v in pairs(t) do if nil == maxn then maxn = v end if maxn < v then maxn = v end end return maxn end --返回table的最小value table.minn = function(t) local min = nil for _, v in pairs(t) do if nil == min then min = v end if min > v then min = v end end return min end -- 判断table是否为空 table.empty = function(t) return not next(t) end -- 返回table索引列表 table.indices = function(t) local result = {} for k, v in pairs(t) do table.insert(result, k) end return result end -- 返回table值列表 table.values = function(t) local result = {} for k, v in pairs(t) do table.insert(result, v) end return result end -- 浅拷贝 table.clone = function(t, nometa) local result = {} if not nometa then setmetatable(result, getmetatable(t)) end for k, v in pairs (t) do result[k] = v end return result end -- 深拷贝 table.copy = function(t, nometa) local result = {} if not nometa then setmetatable(result, getmetatable(t)) end for k, v in pairs(t) do if type(v) == "table" then result[k] = table.copy(v, nometa) else result[k] = v end end return result end table.merge = function(dest, src) for k, v in pairs(src) do dest[k] = v end end table.random = function(tbl) local len = #tbl if len == 1 then return tbl end for i = 1, len do local ranOne = math.random(1, len+1-i) tbl[ranOne], tbl[len+1-i] = tbl[len+1-i],tbl[ranOne] end return tbl end table.contain = function(t, val) for _, v in pairs(t) do if v == val then return true end end return false end table.count = function(t, val) local cnt = 0 for _, v in pairs(t) do if v == val then cnt = cnt + 1 end end return cnt end table.sum = function(t) local s = 0 for _, v in pairs(t) do s = s + v end return s end table.sub = function(t, start, count) local st = {} for i = 1, count do table.insert(st, t[start+i-1]) end return st end table.findIdx = function(t, val) for k, v in ipairs(t) do if v == val then return k end end return -1 end table.removeVal = function(t, val) for k, v in ipairs(t) do if v == val then return table.remove(t, k) end end end --用val填充table table.fill = function(t, val, count) if not count then count = #t end for i = 1, count do t[i] = val end return t end table.shuffle = function(t) for i = #t, 2, -1 do local j = math.random(i) t[i], t[j] = t[j], t[i] end return t end -- string扩展 -- 下标运算 do local mt = getmetatable("") local _index = mt.__index mt.__index = function (s, ...) local k = ... if "number" == type(k) then return _index.sub(s, k, k) else return _index[k] end end end string.split = function(s, delim) local split = {} local pattern = "[^" .. delim .. "]+" string.gsub(s, pattern, function(v) table.insert(split, v) end) return split end string.ltrim = function(s, c) local pattern = "^" .. (c or "%s") .. "+" return (string.gsub(s, pattern, "")) end string.rtrim = function(s, c) local pattern = (c or "%s") .. "+" .. "$" return (string.gsub(s, pattern, "")) end string.trim = function(s, c) return string.rtrim(string.ltrim(s, c), c) end string.split_to_number = function(s, delim) local split = {} local pattern = "[^" .. delim .. "]+" string.gsub(s, pattern, function(v) table.insert(split, tonumber(v)) end) return split end --safe floor(针对math.floor传入nil或非数字字符串会报错) math.sfloor = function (value) value = tonumber(value) if value ~= nil then return math.floor(value) end return nil end --金币保留两位小数 math.round_coin = function (value) return math.floor(value*100+0.5)/100 end local function dump(obj) local getIndent, quoteStr, wrapKey, wrapVal, dumpObj getIndent = function(level) return string.rep("\t", level) end quoteStr = function(str) return '"' .. string.gsub(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 dumpObj = function(obj, level) if type(obj) ~= "table" then return wrapVal(obj) end level = level + 1 local tokens = {} tokens[#tokens + 1] = "{" for k, v in pairs(obj) do tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. "," end tokens[#tokens + 1] = getIndent(level - 1) .. "}" return table.concat(tokens, "\n") end return dumpObj(obj, 0) end do local _tostring = tostring tostring = function(v) if type(v) == 'table' then return dump(v) else return _tostring(v) end end end -- math扩展 do local _floor = math.floor math.floor = function(n, p) if p and p ~= 0 then local e = 10 ^ p return _floor(n * e) / e else return _floor(n) end end end math.round = function(n, p) local e = 10 ^ (p or 0) return math.floor(n * e + 0.5) / e end -- lua面向对象扩展 local _class={} function class(super) local class_type={} class_type.ctor=false class_type.super=super class_type.new=function(...) local obj={} do local create create = function(c,...) if c.super then create(c.super,...) end if c.ctor then c.ctor(obj,...) end end create(class_type,...) end setmetatable(obj,{ __index=_class[class_type] }) return obj end local vtbl={} _class[class_type]=vtbl setmetatable(class_type,{__newindex= function(t,k,v) vtbl[k]=v end }) if super then setmetatable(vtbl,{__index= function(t,k) local ret=_class[super][k] vtbl[k]=ret return ret end }) end return class_type end
标签:function,return,string,val,lua,table,end,local From: https://www.cnblogs.com/barrysgy/p/18209033