Add gmatch implementation
This commit is contained in:
@@ -143,11 +143,38 @@ public class StrLib {
|
|||||||
* as this would prevent the iteration.
|
* as this would prevent the iteration.
|
||||||
*/
|
*/
|
||||||
static void gmatch( VM vm ) {
|
static void gmatch( VM vm ) {
|
||||||
LString s = vm.getArgAsLuaString(0);
|
vm.setResult( new GMatchAux(vm) );
|
||||||
LString pattern = vm.getArgAsLuaString(1);
|
|
||||||
vm.setResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class GMatchAux extends LFunction {
|
||||||
|
private final LString src,pat;
|
||||||
|
private final int srclen;
|
||||||
|
private final MatchState ms;
|
||||||
|
private int soffset;
|
||||||
|
public GMatchAux(VM vm) {
|
||||||
|
this.src = vm.getArgAsLuaString(0);
|
||||||
|
this.pat = vm.getArgAsLuaString(1);
|
||||||
|
this.srclen = src.length();
|
||||||
|
this.ms = new MatchState(vm, src, pat);
|
||||||
|
this.soffset = 0;
|
||||||
|
}
|
||||||
|
public boolean luaStackCall(VM vm) {
|
||||||
|
vm.setResult();
|
||||||
|
for ( ; soffset<srclen; soffset++ ) {
|
||||||
|
int res = ms.match(soffset, 0);
|
||||||
|
if ( res >=0 ) {
|
||||||
|
int soff = soffset;
|
||||||
|
soffset = res;
|
||||||
|
ms.push_captures( true, soff, res );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vm.push( LNil.NIL );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.gsub (s, pattern, repl [, n])
|
* string.gsub (s, pattern, repl [, n])
|
||||||
* Returns a copy of s in which all (or the first n, if given) occurrences of the
|
* Returns a copy of s in which all (or the first n, if given) occurrences of the
|
||||||
|
|||||||
Reference in New Issue
Block a user