Przeglądaj źródła

Unicode regex (#7419)

* [eval] let's try some regex unicoding

* add more tests because I'm so much fun at parties

* make sure emojis are supported as well

* [lua] enable appropriate utf8 behavior in EReg (#58)

* [eval] steal Justin's lua regex flags

* adjust tests

* add tests from #3430

* add an /i test

* [php] fix unicode /i

* [lua] fix escape behavior for pattern matches in ereg

* fix java regex flags
Simon Krajewski 7 lat temu
rodzic
commit
776c4f2e3d

+ 2 - 1
src/macro/eval/evalStdLib.ml

@@ -750,10 +750,11 @@ module StdEReg = struct
 			| 'i' -> Some `CASELESS
 			| 's' -> Some `DOTALL
 			| 'm' -> Some `MULTILINE
-			| 'u' -> Some `UTF8
+			| 'u' -> None
 			| 'g' -> global := true; None
 			| c -> failwith ("Unsupported regexp option '" ^ String.make 1 c ^ "'")
 		) (ExtString.String.explode opt) in
+		let flags = `UTF8 :: `UCP :: flags in
 		let r = try regexp ~flags r with Error error -> failwith (string_of_pcre_error error) in
 		let pcre = {
 			r = r;

+ 3 - 0
std/java/_std/EReg.hx

@@ -45,6 +45,9 @@ import java.util.regex.*;
 			}
 		}
 
+		flags |= Pattern.UNICODE_CASE;
+		flags |= Pattern.UNICODE_CHARACTER_CLASS;
+
 		matcher = Pattern.compile(convert(r), flags).matcher("");
 		pattern = r;
 	}

+ 11 - 5
std/lua/_std/EReg.hx

@@ -41,11 +41,14 @@ class EReg {
 				case "i" : ropt |= FLAGS.CASELESS;
 				case "m" : ropt |= FLAGS.MULTILINE;
 				case "s" : ropt |= FLAGS.DOTALL;
-				case "u" : ropt |= FLAGS.UTF8;
 				case "g" : global = true;
 				default : null;
 			}
 		}
+
+		ropt |= FLAGS.UTF8; // always check validity of utf8 string
+		ropt |= FLAGS.UCP; // always enable utf8 character properties
+
 		if (global == null) global = false;
 		this.r = Rex.create(r, ropt);
 	}
@@ -82,10 +85,12 @@ class EReg {
 	}
 
 	public function matchedPos() : { pos : Int, len : Int } {
+		var left = matchedLeft();
+		var matched = matched(0);
 		if( m[1] == null ) throw "No string matched";
 		return {
-			pos : m[1]-1,
-			len : m[2]-m[1]+ 1
+			pos : left.length,
+			len : matched.length
 		}
 	}
 
@@ -121,8 +126,9 @@ class EReg {
 	}
 
 	public function replace( s : String, by : String ) : String {
-		by = Rex.gsub(by, "\\$(\\d)", "%%%1"); // convert dollar sign matched groups to Rex equivalent
-		by = Rex.gsub(by, "\\${2}", "$"); // escape double dollar signs
+		var chunks = by.split("$$");
+		chunks = [for (chunk in chunks) Rex.gsub(chunk, "\\$(\\d)", "%%%1", 1)];
+		by = chunks.join("$");
 		return Rex.gsub(s,r,by, global ? null : 1);
 	}
 

+ 46 - 0
std/lua/internal/StringImpl.hx

@@ -0,0 +1,46 @@
+/*
+ * Copyright (C)2005-2018 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package lua.internal;
+import lua.lib.luautf8.Utf8;
+
+/**
+  An implementation for some of the string logic
+**/
+class StringImpl {
+	static public function indexOf( _this : String, str : String, ?startIndex : Int ) : Int {
+		if (startIndex == null) startIndex = 0;
+		if (str == "") {
+			if (_this == ""){
+				return 0;
+			} else {
+				var max = cast Math.max(startIndex,0);
+				return cast Math.min(_this.length, max);
+			}
+		} else {
+			var startIndex = startIndex >= 0 ? startIndex + 1 : startIndex;
+			var r = Utf8.find(_this, str, startIndex, true).begin;
+			if (r != null && r > 0) return r-1;
+			else return -1;
+		}
+	}
+}

+ 1 - 1
std/php/_std/EReg.hx

@@ -37,7 +37,7 @@ import php.*;
 		this.pattern = r;
 		options = Global.str_replace('g', '', opt);
 		global = options != opt;
-		this.re = '"' + Global.str_replace('"', '\\"', r) + '"' + options;
+		this.re = '"' + Global.str_replace('"', '\\"', r) + '"u' + options;
 	}
 
 	public function match( s : String ) : Bool {

+ 8 - 1
tests/unit/src/unitstd/EReg.unit.hx

@@ -112,4 +112,11 @@ pos.len == 2;
 new EReg("^" + EReg.escape("\\ ^ $ * + ? . ( ) | { } [ ]") + "$", "").match("\\ ^ $ * + ? . ( ) | { } [ ]") == true;
 
 // #6641
-~/(b)/.split("abc") == ["a","c"];
+~/(b)/.split("abc") == ["a","c"];
+
+// #3430
+~/(\d+)/g.replace("a1234b12","$1") == "a1234b12";
+#if !java
+~/(\d+)/g.replace("a1234b12","\\$1") == "a\\1234b\\12";
+#end
+~/(\d+)/g.replace("a1234b12","$$1") == "a$1b$1";

+ 21 - 4
tests/unit/src/unitstd/Unicode.unit.hx

@@ -225,9 +225,11 @@ Reflect.field(obj, field) == null;
 
 // EReg -_-
 
-function test(left:String, middle:String, right:String) {
+function test(left:String, middle:String, right:String, ?rex:EReg) {
 	var s = '$left:$middle:$right';
-	var rex = new EReg(':($middle):', "");
+	if (rex == null) {
+		rex = new EReg(':($middle):', "");
+	}
 	function check(rex:EReg) {
 		eq(rex.matchedLeft(), left);
 		eq(rex.matchedRight(), right);
@@ -237,7 +239,11 @@ function test(left:String, middle:String, right:String) {
 		eq(pos.len, middle.length + 2);
 	}
 
-	t(rex.match(s));
+	if (!rex.match(s)) {
+		assert();
+		infos("For " + s);
+		return;
+	}
 	check(rex);
 
 	var split = rex.split(s);
@@ -259,7 +265,6 @@ function test(left:String, middle:String, right:String) {
 	}), '${left}ä$right');
 }
 
-#if !(lua || cpp || flash)
 test("äb", "ä", "bc");
 test("äb", "a", "bc");
 test("ab", "a", "bc");
@@ -275,10 +280,22 @@ test("あb", "abc", "bc");
 test("ab", "abc", "bc");
 test("ab", "あbc", "bc");
 
+#if !flash
+// wontfix (cantfix?)
 test("😂b", "😂bc", "bc");
 test("😂b", "abc", "bc");
 test("ab", "abc", "bc");
 test("ab", "😂bc", "bc");
 #end
 
+#if (eval || lua || python)
+// unspecced?
+test("()", "ä", "[]", ~/:(\w):/);
+~/\bx/.match("äx") == false;
+~/x\b/.match("xä") == false;
+#end
+
+test("a", "É", "b", ~/:(é):/i);
+test("a", "é", "b", ~/:(É):/i);
+
 #end