前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Lua代码片段收集

Lua代码片段收集

作者头像
晚晴幽草轩轩主
发布2018-03-27 12:17:32
9340
发布2018-03-27 12:17:32
举报
文章被收录于专栏:静晴轩

Lua实现闭包

代码语言:javascript
复制
--[[@Func :实现闭包
    @Desc : 当一个函数内部嵌套另一个函数定义时,内部的函数体可以访问外部的函数的局部变量,这种特征我们称作词法定界]]
function fuck()
    local i = 0
    return function()
        i = i + 1
        return i
    end
end
c1 = fuck()
print(c1())
print(c1())

序列化Lua表

代码语言:javascript
复制
-- Desc : 序列化Lua表(Convert Lua-Table To String)
function serialize(t)
truelocal mark={}
truelocal assign={}

truelocal function ser_table(tbl,parent)
truetruemark[tbl]=parent
truetruelocal tmp={}
truetruefor k,v in pairs(tbl) do
truetruetruelocal key= type(k)=="number" and "["..k.."]" or k
truetruetrueif type(v)=="table" then
truetruetruetruelocal dotkey= parent..(type(k)=="number" and key or "."..key)
truetruetruetrueif mark[v] then
truetruetruetruetruetable.insert(assign,dotkey.."="..mark[v])
truetruetruetrueelse
truetruetruetruetruetable.insert(tmp, key.."="..ser_table(v,dotkey))
truetruetruetrueend
truetruetrueelse
truetruetruetruetable.insert(tmp, key.."="..v)
truetruetrueend
truetrueend
truetruereturn "{"..table.concat(tmp,",").."}"
trueend

truereturn "do local ret="..ser_table(t,"ret")..table.concat(assign," ").." return ret end"
end

实现Java字符串的Hash算法

代码语言:javascript
复制
-- 实现Java字符串的Hash算法
hash = function(input)
    input = tostring(input);
    local h = 0
    local len = string.len(input)
    local max = 2147483647
    local min = -2147483648
    local cycle = 4294967296

    for i=1, len do
        h = 31 * h + string.byte(string.sub(input, i, i));
        while h > max do
            h = h - cycle
        end
        while h < min do
            h = h + cycle
        end
    end
    return h
end

树形打印lua table表

代码语言:javascript
复制
--Desc: 树形打印lua table表
local print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local type = type
local pairs = pairs
local tostring = tostring
local next = next

function print_lua_table (lua_table, indent)

    if not lua_table or type(lua_table) ~= "table" then
        return;
    end

    indent = indent or 0
    for k, v in pairs(lua_table) do
        if type(k) == "string" then
            k = string.format("%q", k)
        end
        local szSuffix = ""
        if type(v) == "table" then
            szSuffix = "{"
        end
        local szPrefix = string.rep("    ", indent)
        formatting = szPrefix.."["..k.."]".." = "..szSuffix
        if type(v) == "table" then
            print(formatting)
            print_lua_table(v, indent + 1)
            print(szPrefix.."},")
        else
            local szValue = ""
            if type(v) == "string" then
                szValue = string.format("%q", v)
            else
                szValue = tostring(v)
            end
            print(formatting..szValue..",")
        end
    end
end

具体更加详细的内容可参见树形打印lua table表

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-12-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Lua实现闭包
  • 序列化Lua表
  • 实现Java字符串的Hash算法
  • 树形打印lua table表
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档