mherkender/lua.js

partially implemented string pattern functions (find,gmatch,gsub,match)

ghoulsblade opened this issue · 3 comments

https://github.com/ghoulsblade/love-webplayer/blob/master/js/lua-parser-utils.js
include this file to add support for :

string.find
string.gmatch
string.gsub
string.match

my implementation of them is only partial, it covers a lot of common usecases, but it isn't 100% compatible to native lua behavior.

I added support for table.concat. There's a bug in your version (when sep is null in a javascript join, it is replaced with ", " not "" as in lua's table.concat), so it ended up a little different.

I'm not quite ready to add partial support for the string functions. I should probably hammer out a real version at some point.

no hurry, it wasn't meant as pull request, i just wanted to put it here so people running into them can find a workaround easier =)

I've been busy on some functions to convert a pattern into a regular expression. Here is the code I have so far:

rT  = [];
rT["a"]="w";
rT["A"]="W";
rT["l"]="[a-z]";
rT["L"]="[^a-z]";
rT["u"]="[A-Z]";
rT["U"]="[^A-Z]";
rT["p"]="[\\.\\!\\?\\,\\;\\:\\'\\]";
rT["P"]="[^\\.\\!\\?\\,\\;\\:\\'\\]";
rT["s"]="[\\s\\f\\n\\t\\r\\v]";
rT["S"]="[\\s\\f\\n\\t\\r\\v]";
rT["w"]="[a-Z0-9]";
rT["W"]="[^a-Z0-9]";
rT["x"]="[a-fA-F0-9]";
rT["X"]="[^a-fA-F0-9]";
rT["z"]="\\0";
rT["Z"]="[^\\0]";

function toReg(match){
    var pcmd    = match[1];

    if (rT[pcmd])
        return "\\" + rT[pcmd];
    else if (pcmd == "b"){
        var a   = match[2];
        var b   = match[3];
        return "(" +a+ "[^" +a+b+ "]*" +b+ ")";
    }
    else
        return "\\" + pcmd;
}

saveRegExp  = /([\\\{\}\!])/g
luaPattern  = /%b(.{2})|%(.)/g

function patToRegExp(pattern){
    //Replace certain characters that could be interpreter wrongly by the JS RegExp engine
    var regexp  = pattern.replace(saveRegExp, "\\$1")
    regexp  = regexp.replace(luaPattern, toReg)
    return regexp;
}




a   = "The Game+yuo";
pattern = "([*%/%-+()<>=%[%]])";
regg    = patToRegExp(pattern)
console.log(regg);
reg = new RegExp(regg, "g");
console.log(a.replace(reg, "-$1-"));

There are still some bugs, but maybe you can use the idea as a starting point :)
Currently I have no time at all to work on it.