Last active
December 16, 2015 20:29
-
-
Save MondayPM/5492836 to your computer and use it in GitHub Desktop.
Matcher for haxe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It doesn't infinite loop on empty string matches. It just moves the position forward by 1.