Browse Source

[java] Applied patch from r1608; EReg now uses native .replace()/matchSub() function

Caue Waneck 12 years ago
parent
commit
cefe4bf491
2 changed files with 24 additions and 71 deletions
  1. 9 59
      std/java/_std/EReg.hx
  2. 15 12
      std/java/util/regex/Regex.hx

+ 9 - 59
std/java/_std/EReg.hx

@@ -26,7 +26,6 @@ import java.util.regex.Regex;
 	private var pattern:String;
 	private var pattern:String;
 	private var matcher:Matcher;
 	private var matcher:Matcher;
 	private var cur:String;
 	private var cur:String;
-	private var sub:Int;
 	private var isGlobal:Bool;
 	private var isGlobal:Bool;
 
 
 	public function new( r : String, opt : String ) {
 	public function new( r : String, opt : String ) {
@@ -79,7 +78,6 @@ import java.util.regex.Regex;
 	}
 	}
 
 
 	public function match( s : String ) : Bool {
 	public function match( s : String ) : Bool {
-		sub = 0;
 		cur = s;
 		cur = s;
 		matcher = matcher.reset(s);
 		matcher = matcher.reset(s);
 		return matcher.find();
 		return matcher.find();
@@ -95,25 +93,23 @@ import java.util.regex.Regex;
 
 
 	public function matchedLeft() : String
 	public function matchedLeft() : String
 	{
 	{
-		return untyped cur.substring(0, sub + matcher.start());
+		return untyped cur.substring(0, matcher.start());
 	}
 	}
 
 
 	public function matchedRight() : String
 	public function matchedRight() : String
 	{
 	{
-		return untyped cur.substring(sub + matcher.end(), cur.length);
+		return untyped cur.substring(matcher.end(), cur.length);
 	}
 	}
 
 
 	public function matchedPos() : { pos : Int, len : Int } {
 	public function matchedPos() : { pos : Int, len : Int } {
 		var start = matcher.start();
 		var start = matcher.start();
-		return { pos : sub + start, len : matcher.end() - start };
+		return { pos : start, len : matcher.end() - start };
 	}
 	}
 
 
 	public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
 	public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
-		var s2 = (len < 0 ? s.substr(pos) : s.substr(pos, len));
-		sub = pos;
-		matcher = matcher.reset(s2);
+		matcher = matcher.reset(s);
 		cur = s;
 		cur = s;
-		return matcher.find();
+		return matcher.find(pos);
 	}
 	}
 
 
 	public function split( s : String ) : Array<String>
 	public function split( s : String ) : Array<String>
@@ -142,7 +138,7 @@ import java.util.regex.Regex;
 
 
 	inline function start(group:Int)
 	inline function start(group:Int)
 	{
 	{
-		return matcher.start(group) + sub;
+		return matcher.start(group);
 	}
 	}
 
 
 	inline function len(group:Int)
 	inline function len(group:Int)
@@ -152,55 +148,9 @@ import java.util.regex.Regex;
 
 
 	public function replace( s : String, by : String ) : String
 	public function replace( s : String, by : String ) : String
 	{
 	{
-      var b = new StringBuf();
-      var pos = 0;
-      var len = s.length;
-      var a = by.split("$");
-      var first = true;
-      do {
-        if( !matchSub(s,pos,len) )
-          break;
-        var p = matchedPos();
-        if( p.len == 0 && !first ) {
-          if( p.pos == s.length )
-            break;
-          p.pos += 1;
-        }
-        b.addSub(s,pos,p.pos-pos);
-        if( a.length > 0 )
-          b.add(a[0]);
-        var i = 1;
-        while( i < a.length ) {
-          var k = a[i];
-          var c = k.charCodeAt(0);
-          // 1...9
-          if( c >= 49 && c <= 57 ) {
-						try {
-							var ppos = start( c-48 ), plen = this.len( c-48 );
-							b.addSub(s, ppos, plen);
-						}
-						catch(e:Dynamic)
-						{
-							b.add("$");
-							b.add(k);
-						}
-          } else if( c == null ) {
-            b.add("$");
-            i++;
-            var k2 = a[i];
-            if( k2 != null && k2.length > 0 )
-              b.add(k2);
-          } else
-            b.add("$"+k);
-          i++;
-        }
-        var tot = p.pos + p.len - pos;
-        pos += tot;
-        len -= tot;
-        first = false;
-      } while( isGlobal );
-      b.addSub(s,pos,len);
-      return b.toString();
+      matcher.reset(s);
+			by = by.split("$$").join("\\$");
+			return isGlobal ? matcher.replaceAll(by) : matcher.replaceFirst(by);
 	}
 	}
 
 
 	public function map( s : String, f : EReg -> String ) : String {
 	public function map( s : String, f : EReg -> String ) : String {

+ 15 - 12
std/java/util/regex/Regex.hx

@@ -25,10 +25,10 @@ import java.NativeArray;
 extern class Pattern
 extern class Pattern
 {
 {
 	static function compile(regex:String, flags:Int):Pattern;
 	static function compile(regex:String, flags:Int):Pattern;
-	
+
 	function matcher(input:String):Matcher;
 	function matcher(input:String):Matcher;
 	function split(input:String):NativeArray<String>;
 	function split(input:String):NativeArray<String>;
-	
+
 	static var CANON_EQ(default, null):Int;
 	static var CANON_EQ(default, null):Int;
 	static var CASE_INSENSITIVE(default, null):Int;
 	static var CASE_INSENSITIVE(default, null):Int;
 	static var COMMENTS(default, null):Int;
 	static var COMMENTS(default, null):Int;
@@ -43,12 +43,12 @@ extern interface MatchResult
 {
 {
 	@:overload(function(group:Int):Int {})
 	@:overload(function(group:Int):Int {})
 	function end():Int;
 	function end():Int;
-	
+
 	@:overload(function():String {})
 	@:overload(function():String {})
 	function group(group:Int):String;
 	function group(group:Int):String;
-	
+
 	function groupCount():Int;
 	function groupCount():Int;
-	
+
 	@:overload(function(group:Int):Int {})
 	@:overload(function(group:Int):Int {})
 	function start():Int;
 	function start():Int;
 }
 }
@@ -56,21 +56,24 @@ extern interface MatchResult
 extern class Matcher implements MatchResult
 extern class Matcher implements MatchResult
 {
 {
 	function reset(input:String):Matcher;
 	function reset(input:String):Matcher;
-	
+
 	@:overload(function(group:Int):Int {})
 	@:overload(function(group:Int):Int {})
 	function end():Int;
 	function end():Int;
-	
+
 	@:overload(function():String {})
 	@:overload(function():String {})
 	function group(group:Int):String;
 	function group(group:Int):String;
-	
+
 	function groupCount():Int;
 	function groupCount():Int;
 
 
 	@:overload(function(group:Int):Int {})
 	@:overload(function(group:Int):Int {})
 	function start():Int;
 	function start():Int;
-	
+
+	@:overload(function(startPos:Int):Bool{})
 	function find():Bool;
 	function find():Bool;
-	
+
 	function replaceAll(replacement:String):String;
 	function replaceAll(replacement:String):String;
-	
+
+	function replaceFirst(replacement:String):String;
+
 	function pattern():Pattern;
 	function pattern():Pattern;
-}
+}