Skip to content

Instantly share code, notes, and snippets.

@MondayPM
Last active December 16, 2015 20:29
Show Gist options
  • Select an option

  • Save MondayPM/5492836 to your computer and use it in GitHub Desktop.

Select an option

Save MondayPM/5492836 to your computer and use it in GitHub Desktop.
Matcher for haxe
package;
class Matcher {
public var s:String;
public var reg:EReg;
public var pos:Int;
public function new(s:String,reg:EReg) {
this.s=s;
this.reg=reg;
pos=0;
}
public function find():Bool {
if(pos==-1) {
return false;
}
var cur:String=null;
if(pos==0) {
cur=s;
} else {
cur=s.substr(this.pos);
}
if(!reg.match(cur)) {
pos=-1;
return false;
} else {
var pos:{pos:Int,len:Int}=reg.matchedPos();
if(pos.len==0) {
this.pos++;
} else {
this.pos+=pos.pos+pos.len;
}
if(this.pos==s.length) {
this.pos=-1;
}
return true;
}
}
}
@MondayPM

MondayPM commented May 1, 2013

Copy link
Copy Markdown
Author

It doesn't infinite loop on empty string matches. It just moves the position forward by 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment