Quellcode durchsuchen

added customReplace

Nicolas Cannasse vor 17 Jahren
Ursprung
Commit
3a7bcdad2e
2 geänderte Dateien mit 20 neuen und 1 gelöschten Zeilen
  1. 1 0
      doc/CHANGES.txt
  2. 19 1
      std/EReg.hx

+ 1 - 0
doc/CHANGES.txt

@@ -11,6 +11,7 @@
 	added Reflect.compare
 	fixed "".split() in Neko (now returns [""] instead of [])
 	bugfix for swf-lib f9 classes ordering
+	added EReg.customReplace
 
 2008-01-13: 1.17
 	fixed Int32.compare, added Int32.read and Int32.write

+ 19 - 1
std/EReg.hx

@@ -213,7 +213,7 @@ class EReg {
 		#else (js || flash9)
 		// we can't use directly s.split because it's ignoring the 'g' flag
 		var d = "#__delim__#";
-		return untyped s.replace(r,d).split(d);		
+		return untyped s.replace(r,d).split(d);
 		#else flash
 		throw "EReg::split not implemented";
 		return null;
@@ -285,6 +285,24 @@ class EReg {
 		#end
 	}
 
+	/**
+		For each occurence of the pattern in the string [s], the function [f] is called and
+		can return the string that needs to be replaced. All occurences are matched anyway,
+		and setting the [g] flag might cause some incorrect behavior on some platforms.
+	**/
+	public function customReplace( s : String, f : EReg -> String ) : String {
+		var buf = new StringBuf();
+		while( true ) {
+			if( !match(s) )
+				break;
+			buf.add(matchedLeft());
+			buf.add(f(this));
+			s = matchedRight();
+		}
+		buf.add(s);
+		return buf.toString();
+	}
+
 #if neko
 	static var regexp_new_options = neko.Lib.load("regexp","regexp_new_options",2);
 	static var regexp_match = neko.Lib.load("regexp","regexp_match",4);