首页 > 其他分享 >lua的table, string, class常用扩展

lua的table, string, class常用扩展

时间:2024-05-23 17:29:54浏览次数:17  
标签:function return string val lua table end local

-- 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

相关文章

  • C++ 的 mutable 引出的一系列思考
    阅读多线程实战第六章第二节时,看到mutable关键词的使用,突然忘记它的含义=>  https://github.com/xiaoweiChen/CPP-Concurrency-In-Action-2ed-2019/blob/master/content/chapter6/6.2-chinese.md 进而引申到mutable的使用=> C++的mutable关键字......
  • iptables防火墙SNAT策略和DNAT策略
    目录1.SNAT策略及应用(1)SNAT原理与应用:(2)SNAT转换(1)前提条件:(2)实现方法:2.DNAT策略及应用(1)DNAT原理与应用:(2)DNAT转换(1)前提条件:(2)实现方法:1.SNAT策略及应用(1)SNAT原理与应用:SNAT应用环境:局域网主机共享单个公网IP地址接入Internet(私有IP不能在Internet中正常路由)SNAT原理:修改数据包的......
  • LeetCode Greatest Common Divisor of Strings All In One
    LeetCodeGreatestCommonDivisorofStringsAllInOneLeetCode1071errorsfunctiongcdOfStrings(str1:string,str2:string):string{letresult=``;lettemp=[];if(str1.length>str2.length){letreg=newRegExp(str2,'g'......
  • String Record
    T1.P5840算法:ACAM+BIT+树链剖分自然地,我们会对\(s_i\)建ACAM,然后建出一颗fail树。此时我们考虑集合内加入一个新的字符串。每一个匹配到的点我们都会给从这个点一直到fail数的根节点上的的每一个点\(+1\),但是每一个点只会加一遍。然后对于这棵树上的一个节点,他对最后......
  • Angular Material 17+ 高级教程 – CDK Table
    前言CDKTable是AngularMaterial对<table>的抽象(无styles)封装。无styles的table有什么好封装的呢?CDKTable最重要的3个功能是:以column概念来做管理动态输出columnstickycolumn都不算什么大功能,但如果我们要自己实现这些功能,确实也挺费劲的,所......
  • el-table
     toggleRowExpansionAll(data,isExpansion){data.forEach((item)=>{this.$refs.tableCheck.toggleRowExpansion(item,isExpansion);if(item.children!==undefined&&item.children!==null){this.toggleRowEx......
  • [ES2024] Simplify array immutable changes with the new array.with method
    Thenew Array.with methodgivesyouanimmutablesyntaxforchangingvaluesofanarrayataspecifiedindex.Sometimes .map willbemoreefficient.So,inthislessonwe'llcomparebothmethodswhilereplacinganobjectataspecificindex. varto......
  • antd 的 ProTable 通过rowClassName 设置行的字体颜色时,固定列fixed不生效的问题
    1、其他列是已经生效了,但是固定列是没有生效的 constrowClassName=(record)=>{returntableTreeSearchKey.includes(record.key)?'selected-row':'';};<ProTable ...... rowClassName={rowClassName}> 2、分析原因:固定列的子组件也有color属性,覆盖......
  • Lua常用C Api接口
    Lua5.4是Lua语言的一个版本,它提供了许多CAPI函数,这些函数允许开发者在C语言中嵌入和扩展Lua的功能。以下是一些常用的Lua5.4CAPI函数,以及它们的简单示例:lua_newstate-创建一个新的Lua状态。lua_State*L=lua_newstate(0,0);//创建一个新的Lua状态......
  • lua语言特性及用途
    Lua是一个小巧的脚本语言,其设计目的是为了通过灵活嵌入应用程序中从而为应用程序提供灵活的扩展和定制功能。Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。Lua并没有提供强大的库,这是由它的定位决定的。Lua脚本可以很容易的被C/C++代码调用,也可以反过来调用C/C......