Lua 常用函数

--++--

function addOne(Num)

    return add(Num, nil)

end

--++Num--

function add(Num, NumAdd)

    NumAdd = NumAdd or 1

    Num = Num + NumAdd

    return Num

end


--创建对象回调的闭包

function handler(obj, method)

    return function(...)

        return method(obj, ...)

    end

end


--将一个table打包输出

function mydump(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

--例子:"2014-09-07 08:23:05"转换成1410049385

function DateTime_To_UnixStamp(date_time)

    local time_table = myfunctions.split_datetime_str(date_time)

    if time_table ~= nil then

        return os.time{year=time_table[1], month=time_table[2], day=time_table[3], hour=time_table[4], min=time_table[5], sec=time_table[6]}

    end

    return 0

end

--例子:1410049385转换成"2014-09-07 08:23:05"

function UnixStamp_To_DateTime(unix_stamp)

    local year = os.date("%Y", unix_stamp)

    local month = os.date("%m", unix_stamp)

    local day = os.date("%d", unix_stamp)

    local hour = os.date("%H", unix_stamp)

    local min = os.date("%M", unix_stamp)

    local sec = os.date("%S", unix_stamp)

    return string.format("%02d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec)

end

function UnixStamp_To_Time(unix_stamp)

    local month = os.date("%m", unix_stamp)

    local day = os.date("%d", unix_stamp)

    local hour = os.date("%H", unix_stamp)

    local min = os.date("%M", unix_stamp)

    return string.format("%02d-%02d %02d:%02d", month, day, hour, min)

end

-- 获取一个时间戳的凌晨

function transform_dawn_time(unix_stamp)

    if unix_stamp < 86400 then return 0 end

    local set_year = os.date("%Y", unix_stamp)

    local set_month = os.date("%m", unix_stamp)

    local set_day = os.date("%d", unix_stamp)

    local set_hour = 0

    local set_min = 0

    local set_sec = 0

    return os.time{year=set_year, month=set_month, day=set_day, hour=set_hour, min=set_min, sec=set_sec}

end

function UnixStamp_To_BattleTime(unix_stamp)

    local day = math.floor(unix_stamp / 86400)

    local hour = math.floor((unix_stamp % 86400) / 3600)

    local min = math.floor((unix_stamp % 3600) / 60)

    if day > 0 then

        return day.."天前"

    elseif hour > 0 then

        return hour.."小时前"

    elseif min > 0 then

        return min.."分钟前"

    else

        return "一分钟前"

    end

end

--

function check_part_five_clock(time1, time2)

    local cur_tag = math.max(0, time1 - 18000)

    local new_tag = math.max(0, time2 - 18000)

    return (transform_dawn_time(cur_tag) ~= transform_dawn_time(new_tag))

end

function string_split(inputstr, sep)

    if inputstr == nil then

        return {}

    end

    if sep == nil then

        sep = "%s"

    end

    local t={}

    local i=1

    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do

        t[i] = str

        i = i + 1

    end

    return t

end

--根据字符串两边剪切,例子:[(1;2;3),(4;5;6)]剪切出{"(1;2;3)", "(4;5;6)"}

function split_string_by_side(origin, in_start, in_end)            --split_string_by_side(origin, "%" .. start_text, "%" .. end_text)

    local back_list = {}

    local temp = origin

    local start_pos, _ = string.find(temp, in_start)

    local end_pos, _ = string.find(temp, in_end)

    while start_pos and end_pos do

        if start_pos > end_pos then

            temp = string.sub(temp, end_pos + 1)

            start_pos = string.find(temp, in_start)

            end_pos = string.find(temp, in_end)

        else

            local get = string.sub(temp, start_pos, end_pos)

            table.insert(back_list, get)

            temp = string.sub(temp, end_pos + 1)

            start_pos = string.find(temp, in_start)

            end_pos = string.find(temp, in_end)

        end

    end

    return back_list

end

--根据字符串中间剪切,例子:(1;2;3)剪切出{"1", "2", "3"}

function split_string_by_middle(origin, out_start, out_end, middle)  --split_string_by_middle(origin, "%" .. start_text, "%" .. end_text, middle)

    if not origin or not middle then

        return {}

    end

    local temp = origin

    local start_pos = string.find(temp, out_start)

    local end_pos = string.find(temp, out_end)

    while start_pos and end_pos do

        if start_pos > end_pos then

            temp = string.sub(temp, end_pos + 1)

            start_pos = string.find(temp, out_start)

            end_pos = string.find(temp, out_end)

        else

            local get = string.sub(temp, start_pos + 1, end_pos - 1)

            return string_split(get, middle)

        end       

    end

    return {}

end

function Parse_Expression(expression)

    expression = expression or "0"

    local script = "return " .. expression

    return loadstring(script)()

end

--从列表中根据权重选出N个,可以重复, 例子:{{80,1001,1,2},{90,1002,2,3},{75,1003,2,3}},选出{{20,30,40,90}},权重位置可以指定

function random_list_by_weight_same(origin_list, count, weight_pos)

    local temp_list = clone(origin_list)

    weight_pos = weight_pos or 1

    count = count or 1

    local back_list = {}

    local sum_weight = 0

    local sum_count = 0

    for k,v in pairs(temp_list) do

        sum_weight = sum_weight + (v[weight_pos] or 0)

        sum_count = sum_count + 1

    end

    if sum_weight > 0 then

        for index = 1, math.min(count, sum_count) do

            local random_seed = math.random(1, sum_weight)

            local random_sum = 0

            for k,v in pairs(temp_list) do

                random_sum = random_sum + (v[weight_pos] or 0)

                if random_seed <= random_sum then

                    table.insert(back_list, v)

                    break

                end

            end

        end

    end

    return back_list

end

--从列表中根据权重选出N个,不重复, 例子:{{10,20,30,50},{20,30,40,40},{20,30,40,90}},选出{{20,30,40,90}},权重位置可以指定

function random_list_by_weight_different(origin_list, count, weight_pos)

    local temp_list = clone(origin_list)

    weight_pos = weight_pos or 1

    count = count or 1

    local back_list = {}

    local sum_count = 0

    for k,v in pairs(temp_list) do

        sum_count = sum_count + 1

    end

    for index = 1, math.min(count, sum_count) do

        local cur_sum_weight = 0

        for k,v in pairs(temp_list) do

            cur_sum_weight = cur_sum_weight + (v[weight_pos] or 0)

        end

        local random_seed = math.random(1, cur_sum_weight)

        local random_sum = 0

        for k,v in pairs(temp_list) do

            random_sum = random_sum + (v[weight_pos] or 0)

            if random_seed <= random_sum then

                table.insert(back_list, v)

                temp_list[k] = nil

                break

            end

        end       

    end

    return back_list

end

function string_trim(s)

    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))

end

--分割字符串,如将"20,40,50",分割成{20,40,50}

function split_str(szFullString, szSeparator)

    local nFindStartIndex = 1

    local nSplitIndex = 1

    local nSplitArray = {}

    while true do

        local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)

        if not nFindLastIndex then

            nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))

            break

        end

        nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)

        nFindStartIndex = nFindLastIndex + string.len(szSeparator)

        nSplitIndex = nSplitIndex + 1

    end

    return nSplitArray

end

function myRandom(dict)

    local len = 0

    for k, v in pairs(dict) do

        len = len + 1

    end

    math.randomseed(os.time())

    local seed = math.random(len)

    local cur_count = 0

    for k, v in pairs(dict) do

        cur_count = cur_count + 1

        if cur_count == seed then

            return k, v

        end

    end

    return nil, nil

end

function calculateFormSign(tab)

local key_test ={}

for i in pairs(tab) do

    table.insert(key_test,i)

end

local str=""

table.sort(key_test)

local form = WWWForm.New()

for i,v in pairs(key_test) do

    form:AddField(v,tab[v])

    str=string.format("%s%s=%s",str,v,tab[v])

end

    local Form=LuaHelper.OnWWW(form,str)

    return Form

end

--输入限制,排除掉("";,{}()=''return<>)

function input_constraints(str)

    return str

end

function utfstrlen(str)

    local len = #str

    local left = len

    local cnt = 0

    local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}

    while left ~= 0 do

        local tmp = string.byte(str, -left)

        local i = #arr

        while arr[i] do

            if tmp >= arr[i] then

                left = left - i

                break

            end

            i = i - 1

        end

        cnt = cnt + 1

    end

    return cnt

end

function name_limit(name)

    if not name then

        return -1

    end

    name = string_trim(name)

    if name == "" then

        return -2

    end

    if utfstrlen(name) > 10 then

        return -3

    end

    local start_text = string.sub(name, 1, 2)

    local end_text = string.sub(name, 3)

    if string.upper(start_text) == "UR" and end_text and end_text ~= "" then

        local temp = string.match(end_text, "%x+")

        if temp and temp ~= "" and string.upper(end_text) == string.upper(temp) then

            return -4

        end

    end

    return 0

end

function get_table_count(table)

    local count = 0

    for k, v in pairs(table) do

        count = count + 1

    end

    return count

end

-- 单位:秒

function get_count_down_str(countdown)

    countdown = math.max(0, countdown)

    -- 上限99小时

    local hour = math.floor((countdown % 356400) / 3600)

    local min = math.floor((countdown % 3600) / 60)

    local sec = math.floor(countdown % 60)


    if sec ~= 0 then

        sec = string.format("%0d秒", sec)

    else

        sec = ""

    end

    if min ~= 0 then

        min = string.format("%0d分", min)

    else

        min = ""

    end

    if hour ~= 0 then

        hour = string.format("%0d小时", hour)

        return hour..min

    else

        return min..sec

    end

    return "0秒"

end

-- 单位:秒

function get_online_str(countdown)

    countdown = math.max(0, countdown)

    local day = math.floor((countdown / 3600 / 24))

    local hour = math.floor(((countdown % 86400) / 3600))

    local min = math.floor(((countdown % 3600) / 60))

    if tonumber(day) > 0 then

        return string.format("%0d天%0d时前", day, hour)

    elseif tonumber(hour) > 0 then

        return string.format("%0d时%0d分前", hour, min)

    elseif tonumber(min) > 0 then

        return string.format("%0d分前", min)

    else

        return "1分钟前"

    end

end

-- 单位:秒

function get_box_need_diamond(countdown)

    return math.ceil(math.ceil(math.max(0, countdown) / 60 / 15) * 1.5)

end

function merger_table(table1, table2)

    if table1 == nil then

        return table2

    end

    if table2 == nil then

        return table1

    end

    for key, sub in pairs(table2) do

        table1[key] = sub

    end

    return table1

end

function get_desc_str(desc, value_list, value_add_list, time_list, time_add_list, level)

    local value_dict = {}

    for key, value in pairs(value_list) do

        if value and tonumber(value) ~= 0 then

            table.insert(value_dict, math.abs(value) + math.abs(value_add_list[key] or 0) * math.max(0, (level - 1)))

        end

    end

    local time_dict = {}

    for key, value in pairs(time_list) do

        if value and tonumber(value) ~= 0 then

            table.insert(time_dict, math.abs(value) + math.abs(time_add_list[key] or 0) * math.max(0, (level - 1)))

        end

    end

    if next(value_dict)  then

        desc = string.gsub(desc, "V%d+", function(key) return value_dict[tonumber(string.match(key, "%d+"))] or "" end)

    end

    if next(time_dict) then

        desc = string.gsub(desc, "T%d+", function(key) return time_dict[tonumber(string.match(key, "%d+"))] or "" end)

    end

    return desc

end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,877评论 0 38
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,487评论 0 13
  • lua库函数 这些函数都是Lua编程语言的一部分, 点击这里了解更多. assert(value) - 检查一个值...
    曾令伟阅读 3,948评论 0 1
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,497评论 0 17
  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,391评论 0 2