Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ghoulsblade opened this issue Apr 27, 2012 · 3 comments

Comments

@ghoulsblade
Copy link
Contributor

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.

@mherkender
Copy link
Owner

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.

@ghoulsblade
Copy link
Contributor Author

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 =)

@jimbauwens
Copy link
Contributor

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.

Repository owner deleted a comment from RealLinen Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants