模块:TWRVersionCheck

来自TeamMoeg官方WIKI
跳到导航 跳到搜索

WikiCompareVersion ver1 ver2 1


local p = {}
function p.split (inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={}
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        table.insert(t, str)
    end
    return t
end
function p.parseNumberVersion(inputstr)
    local rt=p.split(inputstr,"\\.")
    for i, v in ipairs( rt ) do
        rt[i]=tonumber(v);
    end
    return rt
end
function p.isNum(inputstr)
    if inputstr==nil then
        return 0
    end
    local mst=string.sub(inputstr,1,1)
    local lv=string.byte(mst)-48
    if lv>=0 and lv<=9 then
        return 1
    end
    return 0
end
function p.parseSubVersion(inputstr)
    local ext=0
    local ver=""
    local t={}
    for i=1,string.len(inputstr) do
        local mst=string.sub(inputstr,i,i)
        local lv=string.byte(mst)-48
        if lv>=0 and lv<=9 then
            ext=i
            break
        end
        ver=ver..mst
    end
    t.ver=p.parseNumberVersion(string.sub(inputstr,ext))
    t.type=ver
    return t
end
function p.parseFullVersion(inputstr)
    local ss=p.split (inputstr,"-")
    local t={}
    local subs={}
    t.ver=p.parseNumberVersion(ss[1])
    for i=2,#ss do
        subs[i-1]=p.parseSubVersion(ss[i])
    end
    t.sub=subs
    return t
end
function p.compareNumberVersion(t1,t2)
    local len=math.min(#t1,#t2)
    for i=1,len do
        if t1[i]>t2[i] then
            return 1
        elseif t1[i]<t2[i] then
            return -1
        end
    end
    return 0 
end
p.types={}
p.types.pre=-1
p.types.rc=0
p.types.stable=3
p.types.hf=1
p.types[""]=2
function p.compareSubVersion(t1,t2)
    local p1=p.types[t1.type]
    local p2=p.types[t2.type]
    if p1>p2 then
        return 1
    elseif p1<p2 then
        return -1
    end
    return p.compareNumberVersion(t1.ver,t2.ver)
end
function p.compareFullVersion(t1,t2)
    local mv=p.compareNumberVersion(t1.ver,t2.ver)
    if mv~=0 then
        return mv
    end
    if #t1.sub==0 then
        if #t2.sub~=0 then
            if t2.sub[1].type~="pre" then
                return -1
            end
            return 1
        end
        return 0
    end
    if #t2.sub==0 then
        if t1.sub[1].type=="pre" then
            return -1
        end
        return 1
    end
    local len=math.min(#t1.sub,#t2.sub)
    for i=1,len do
        local mv2=p.compareSubVersion(t1.sub[i],t2.sub[i])
        if mv2~=0 then
            return mv2
        end
    end
    if #t2.sub>#t1.sub then
        if t2.sub[#t1.sub+1].type=="pre" then
            return 1
        end
        return -1
    elseif #t2.sub<#t1.sub then
        if t1.sub[#t2.sub+1].type=="pre" then
            return -1
        end
        return 1
    end
    return 0
end
function p.compareVersion(s1,s2)
    if p.isNum(s1)~=1 then
        return -1
    elseif p.isNum(s2)~=1 then
        return 1
    end
    return p.compareFullVersion(p.parseFullVersion(s1),p.parseFullVersion(s2))
end
function p.WikiCompareVersion(frame)
    if p.compareVersion(frame.args.ver1,frame.args.ver2)>=0 then
        return 1
    end
end
return p;