mherkender / lua.js

An ECMAscript framework to compile and run Lua code, allowing Lua to run in a browser or in Flash
http://blog.brokenfunction.com/
600 stars 73 forks source link

string.match .find .gmatch table.concat partial implementations #6

Closed ghoulsblade closed 12 years ago

ghoulsblade commented 12 years ago

update: string + table functions

ghoulsblade commented 12 years ago

update : also did a limited string.match implementation not fully identical, but passes a few tests of simple cases:

local path = "/Library/Application Support" CheckTrue("match01",string.match(path,"/Library/Application Support"))
CheckTrue("match02",string.match("12ABab99",'^[0123456789abcdefABCDEF]+$'))
local a,b = string.match("myfile.txt","(.+)%.(.-)$") CheckArr("match03",{a,b},{"myfile","txt"})
CheckArr("match04",{string.match("mymusic.mp3","%.[mo][pg][3g]$")},{".mp3"})

(all 4 tests passed successfully with the suggested implementation) with the following test-utils:

function CheckArr (title,arr1,arr2)
    for k,v in pairs(arr2) do 
        print((arr1[k] == v) and "OK" or "ERROR",title,"#"..tostring(k),arr1[k],v)
    end
end
function CheckBool (title,testme,shouldbe)
    testme = not (not testme)
    shouldbe = not (not shouldbe)
    print( (testme == shouldbe) and "OK" or "ERROR" , title, testme, shouldbe)
end
function CheckTrue (title,testme) CheckBool(title,testme,true) end
ghoulsblade commented 12 years ago

update : also did a limited string.find implementation not fully identical, but passes a few tests of simple cases: (all 4 tests passed successfully with the suggested implementation)

local path = "/Library/Application Support" 
local p0,p1,a,b = string.find(path,"/Library/Application Support") CheckArr("find01",{p0,p1,a,b},{1,28,nil,nil}) 
local p0,p1,a,b = string.find("12ABab99",'^[0123456789abcdefABCDEF]+$') CheckArr("find02",{p0,p1,a,b},{1,8,nil,nil}) 
local p0,p1,a,b = string.find("myfile.txt","(.+)%.(.-)$") CheckArr("find03",{p0,p1,a,b},{1,10,"myfile","txt"}) 
local p0,p1,a,b = string.find("mymusic.mp3","%.[mo][pg][3g]$") CheckArr("find04",{p0,p1,a,b},{8,11,nil,nil}) 
ghoulsblade commented 12 years ago

tests (all 4 successful) :

TestIterator("gmatch11",{string.gmatch("hello world from Lua", "%w+")},{{"hello"},{"world"},{"from"},{"Lua"}})
TestIterator("gmatch12",{string.gmatch("hello world from Lua", "%a+")},{{"hello"},{"world"},{"from"},{"Lua"}})
TestIterator("gmatch13",{string.gmatch("from=world, to=Lua", "(%w+)=(%w+)")},{{"from","world"},{"to","Lua"}})
TestIterator("gmatch14",{string.gmatch("12abCD", ".")},{{"1"},{"2"},{"a"},{"b"},{"C"},{"D"}})

test utils:

function TestIterator (title,arrinit,arrgoal)
    local res = {}
    local restxt = {}
    for a,b,c,d,e,f,g in unpack(arrinit) do table.insert(res,{a,b,c,d,e,f,g}) end
    for k,v in ipairs(arrgoal) do 
        for k2,v2 in ipairs(v) do 
            local x = res[k] and res[k][k2]
            print((x == v2) and "OK" or "ERROR",title,k,k2,x,v2)
        end
    end
end
ghoulsblade commented 12 years ago

table.concat tests (all successful)

function CheckEqual (title,v1,v2) print((v1 == v2) and "OK" or "ERROR",title,v1,v2) end
CheckEqual("concat01",table.concat({"ab",3,"cd",10},","),"ab,3,cd,10")
CheckEqual("concat02",table.concat({"ab",3,"cd",10,m=42},","),"ab,3,cd,10")
CheckEqual("concat03",table.concat({1,2,3,4,5,6,7,8,9},",",3,5),"3,4,5")
CheckEqual("concat04",table.concat({}, ";"),"")
ghoulsblade commented 12 years ago

understandable =) "translating" lua patterns to js regexp is probably impossible, so i guess the only correct way to implement it would be to port the original lua sourcecode part for this to js, using char-arrays instead of memory access or something like that.