2
0
Simon Krajewski 11 жил өмнө
parent
commit
19c3636303

+ 42 - 44
std/python/_std/EReg.hx

@@ -3,22 +3,20 @@ import python.lib.Re;
 import python.lib.Re.MatchObject;
 import python.lib.Re.MatchObject;
 import python.lib.Re.Pattern;
 import python.lib.Re.Pattern;
 
 
+@:coreApi
 class EReg {
 class EReg {
 
 
 	/**
 	/**
 		Creates a new regular expression with pattern `r` and modifiers `opt`.
 		Creates a new regular expression with pattern `r` and modifiers `opt`.
-		
+
 		This is equivalent to the shorthand syntax `~/r/opt`
 		This is equivalent to the shorthand syntax `~/r/opt`
-		
+
 		If `r` or `opt` are null, the result is unspecified.
 		If `r` or `opt` are null, the result is unspecified.
 	**/
 	**/
 
 
 	var pattern:Regex;
 	var pattern:Regex;
 	var matchObj:MatchObject;
 	var matchObj:MatchObject;
 	var global:Bool;
 	var global:Bool;
-	
-
-
 
 
 	public function new( r : String, opt : String ) {
 	public function new( r : String, opt : String ) {
 		global = false;
 		global = false;
@@ -36,9 +34,9 @@ class EReg {
 
 
 	/**
 	/**
 		Tells if `this` regular expression matches String `s`.
 		Tells if `this` regular expression matches String `s`.
-		
+
 		This method modifies the internal state.
 		This method modifies the internal state.
-		
+
 		If `s` is `null`, the result is unspecified.
 		If `s` is `null`, the result is unspecified.
 	**/
 	**/
 	public function match( s : String ) : Bool {
 	public function match( s : String ) : Bool {
@@ -48,13 +46,13 @@ class EReg {
 
 
 	/**
 	/**
 		Returns the matched sub-group `n` of `this` EReg.
 		Returns the matched sub-group `n` of `this` EReg.
-		
+
 		This method should only be called after `this.match` or
 		This method should only be called after `this.match` or
 		`this.matchSub`, and then operates on the String of that operation.
 		`this.matchSub`, and then operates on the String of that operation.
-		
+
 		The index `n` corresponds to the n-th set of parentheses in the pattern
 		The index `n` corresponds to the n-th set of parentheses in the pattern
 		of `this` EReg. If no such sub-group exists, an exception is thrown.
 		of `this` EReg. If no such sub-group exists, an exception is thrown.
-		
+
 		If `n` equals 0, the whole matched substring is returned.
 		If `n` equals 0, the whole matched substring is returned.
 	**/
 	**/
 	public function matched( n : Int ) : String {
 	public function matched( n : Int ) : String {
@@ -63,13 +61,13 @@ class EReg {
 
 
 	/**
 	/**
 		Returns the part to the left of the last matched substring.
 		Returns the part to the left of the last matched substring.
-		
+
 		If the most recent call to `this.match` or `this.matchSub` did not
 		If the most recent call to `this.match` or `this.matchSub` did not
 		match anything, the result is unspecified.
 		match anything, the result is unspecified.
-		
+
 		If the global g modifier was in place for the matching, only the
 		If the global g modifier was in place for the matching, only the
 		substring to the left of the leftmost match is returned.
 		substring to the left of the leftmost match is returned.
-		
+
 		The result does not include the matched part.
 		The result does not include the matched part.
 	**/
 	**/
 	public function matchedLeft() : String {
 	public function matchedLeft() : String {
@@ -78,13 +76,13 @@ class EReg {
 
 
 	/**
 	/**
 		Returns the part to the right of the last matched substring.
 		Returns the part to the right of the last matched substring.
-		
+
 		If the most recent call to `this.match` or `this.matchSub` did not
 		If the most recent call to `this.match` or `this.matchSub` did not
 		match anything, the result is unspecified.
 		match anything, the result is unspecified.
-		
+
 		If the global g modifier was in place for the matching, only the
 		If the global g modifier was in place for the matching, only the
 		substring to the right of the leftmost match is returned.
 		substring to the right of the leftmost match is returned.
-		
+
 		The result does not include the matched part.
 		The result does not include the matched part.
 	**/
 	**/
 	public function matchedRight() : String {
 	public function matchedRight() : String {
@@ -95,10 +93,10 @@ class EReg {
 		Returns the position and length of the last matched substring, within
 		Returns the position and length of the last matched substring, within
 		the String which was last used as argument to `this.match` or
 		the String which was last used as argument to `this.match` or
 		`this.matchSub`.
 		`this.matchSub`.
-		
+
 		If the most recent call to `this.match` or `this.matchSub` did not
 		If the most recent call to `this.match` or `this.matchSub` did not
 		match anything, the result is unspecified.
 		match anything, the result is unspecified.
-		
+
 		If the global g modifier was in place for the matching, the position and
 		If the global g modifier was in place for the matching, the position and
 		length of the leftmost substring is returned.
 		length of the leftmost substring is returned.
 	**/
 	**/
@@ -108,13 +106,13 @@ class EReg {
 
 
 	/**
 	/**
 		Tells if `this` regular expression matches a substring of String `s`.
 		Tells if `this` regular expression matches a substring of String `s`.
-		
+
 		This function expects `pos` and `len` to describe a valid substring of
 		This function expects `pos` and `len` to describe a valid substring of
 		`s`, or else the result is unspecified. To get more robust behavior,
 		`s`, or else the result is unspecified. To get more robust behavior,
 		`this.matchSub(s.substr(pos,len))` can be used instead.
 		`this.matchSub(s.substr(pos,len))` can be used instead.
-		
+
 		This method modifies the internal state.
 		This method modifies the internal state.
-		
+
 		If `s` is null, the result is unspecified.
 		If `s` is null, the result is unspecified.
 	**/
 	**/
 	public function matchSub( s : String, pos : Int, ?len : Int):Bool {
 	public function matchSub( s : String, pos : Int, ?len : Int):Bool {
@@ -123,34 +121,34 @@ class EReg {
 		} else {
 		} else {
 			matchObj = pattern.search(s, pos);
 			matchObj = pattern.search(s, pos);
 		}
 		}
-		
+
 		return this.matchObj != null;
 		return this.matchObj != null;
-		
+
 	}
 	}
 
 
 	/**
 	/**
 		Splits String `s` at all substrings `this` EReg matches.
 		Splits String `s` at all substrings `this` EReg matches.
-		
+
 		If a match is found at the start of `s`, the result contains a leading
 		If a match is found at the start of `s`, the result contains a leading
 		empty String "" entry.
 		empty String "" entry.
-		
+
 		If a match is found at the end of `s`, the result contains a trailing
 		If a match is found at the end of `s`, the result contains a trailing
 		empty String "" entry.
 		empty String "" entry.
-		
+
 		If two matching substrings appear next to each other, the result
 		If two matching substrings appear next to each other, the result
 		contains the empty String "" between them.
 		contains the empty String "" between them.
-		
+
 		By default, this method splits `s` into two parts at the first matched
 		By default, this method splits `s` into two parts at the first matched
 		substring. If the global g modifier is in place, `s` is split at each
 		substring. If the global g modifier is in place, `s` is split at each
 		matched substring.
 		matched substring.
-		
+
 		If `s` is null, the result is unspecified.
 		If `s` is null, the result is unspecified.
 	**/
 	**/
 	public function split( s : String ) : Array<String> {
 	public function split( s : String ) : Array<String> {
 		return if (global) {
 		return if (global) {
 			var ret = [];
 			var ret = [];
 			var lastEnd = 0;
 			var lastEnd = 0;
-			
+
 			for (x in Re.finditer(pattern, s)) {
 			for (x in Re.finditer(pattern, s)) {
 
 
 				ret.push(s.substring(lastEnd, x.start() ));
 				ret.push(s.substring(lastEnd, x.start() ));
@@ -161,7 +159,7 @@ class EReg {
 		} else {
 		} else {
 			this.match(s);
 			this.match(s);
 			if (matchObj == null) {
 			if (matchObj == null) {
-				
+
 				return [s];
 				return [s];
 			} else {
 			} else {
 				return [ s.substring(0, matchObj.start()), s.substr(matchObj.end()) ];
 				return [ s.substring(0, matchObj.start()), s.substr(matchObj.end()) ];
@@ -172,19 +170,19 @@ class EReg {
 
 
 	/**
 	/**
 		Replaces the first substring of `s` which `this` EReg matches with `by`.
 		Replaces the first substring of `s` which `this` EReg matches with `by`.
-		
+
 		If `this` EReg does not match any substring, the result is `s`.
 		If `this` EReg does not match any substring, the result is `s`.
-		
+
 		By default, this method replaces only the first matched substring. If
 		By default, this method replaces only the first matched substring. If
 		the global g modifier is in place, all matched substrings are replaced.
 		the global g modifier is in place, all matched substrings are replaced.
-		
+
 		If `by` contains `$1` to `$9`, the digit corresponds to number of a
 		If `by` contains `$1` to `$9`, the digit corresponds to number of a
 		matched sub-group and its value is used instead. If no such sub-group
 		matched sub-group and its value is used instead. If no such sub-group
 		exists, the replacement is unspecified. The string `$$` becomes `$`.
 		exists, the replacement is unspecified. The string `$$` becomes `$`.
-		
+
 		If `s` or `by` are null, the result is unspecified.
 		If `s` or `by` are null, the result is unspecified.
 	**/
 	**/
-	public function replace( s : String, by : String ) : String 
+	public function replace( s : String, by : String ) : String
 	{
 	{
 		var by = by.split("$$").join("_hx_#repl#__");
 		var by = by.split("$$").join("_hx_#repl#__");
 		function replace (x:MatchObject) {
 		function replace (x:MatchObject) {
@@ -206,14 +204,14 @@ class EReg {
 		and setting the `g` flag might cause some incorrect behavior on some platforms.
 		and setting the `g` flag might cause some incorrect behavior on some platforms.
 	**/
 	**/
 	public function map( s : String, f : EReg -> String ) : String {
 	public function map( s : String, f : EReg -> String ) : String {
-		
+
 		var buf = new StringBuf();
 		var buf = new StringBuf();
 		var pos = 0;
 		var pos = 0;
 		var right = s;
 		var right = s;
-		
+
 		var cur = this;
 		var cur = this;
 		while( pos < s.length ) {
 		while( pos < s.length ) {
-			
+
 			if (matchObj == null) {
 			if (matchObj == null) {
 				matchObj = Re.search(pattern, s);
 				matchObj = Re.search(pattern, s);
 			} else {
 			} else {
@@ -222,8 +220,8 @@ class EReg {
 
 
 			if( matchObj == null )
 			if( matchObj == null )
 				break;
 				break;
-			
-			
+
+
 
 
 			var pos1 = matchObj.end();
 			var pos1 = matchObj.end();
 
 
@@ -234,14 +232,14 @@ class EReg {
 			buf.add(f(cur));
 			buf.add(f(cur));
 
 
 			right = cur.matchedRight();
 			right = cur.matchedRight();
-			
-			
+
+
 
 
 			if (!global) {
 			if (!global) {
 				buf.add(right);
 				buf.add(right);
 				return buf.toString();
 				return buf.toString();
 			}
 			}
-			
+
 			if (curPos.len == 0) {
 			if (curPos.len == 0) {
 				buf.add(s.charAt(pos1));
 				buf.add(s.charAt(pos1));
 				right = right.substr(1);
 				right = right.substr(1);
@@ -250,7 +248,7 @@ class EReg {
 				pos = pos1;
 				pos = pos1;
 			}
 			}
 
 
-			
+
 		}
 		}
 		buf.add(right);
 		buf.add(right);
 		return buf.toString();
 		return buf.toString();