]> granicus.if.org Git - icu/commitdiff
ICU-12979 Fix \Q...\E in UnicodeRegex#transform (#23)
authorDavid Corbett <corbett.dav@husky.neu.edu>
Mon, 10 Sep 2018 22:19:10 +0000 (18:19 -0400)
committerShane Carr <shane@unicode.org>
Thu, 27 Sep 2018 21:27:39 +0000 (14:27 -0700)
icu4j/main/classes/core/src/com/ibm/icu/impl/UnicodeRegex.java
icu4j/main/tests/translit/src/com/ibm/icu/dev/test/translit/RegexUtilitiesTest.java

index fb7a0c0df3028c9a3ba59594bbb38f7b07afa0a5..ccaa6077ccabc778e8090614afc64de9cd311ed2 100644 (file)
@@ -116,7 +116,7 @@ public class UnicodeRegex implements Cloneable, Freezable<UnicodeRegex>, StringT
 
             case 1: // we are after a \
                 if (ch == 'Q') {
-                    state = 1;
+                    state = 2;
                 } else {
                     state = 0;
                 }
@@ -128,11 +128,12 @@ public class UnicodeRegex implements Cloneable, Freezable<UnicodeRegex>, StringT
                 }
                 break;
 
-            case 3: // we are in at \Q...\
+            case 3: // we are in a \Q...\
                 if (ch == 'E') {
                     state = 0;
+                } else if (ch != '\\') {
+                    state = 2;
                 }
-                state = 2;
                 break;
             }
             result.append(ch);
index 5ce5d411e4495d6abee666ec3ae9f1818ab4715f..429086971c8a182062748b8a6d8dc9dc9634047b 100644 (file)
@@ -216,6 +216,29 @@ public class RegexUtilitiesTest extends TestFmwk {
         }
     }
 
+    /**
+     * Check {@code \Q} and {@code \E}.
+     */
+    @Test
+    public void TestTransformQuotation() {
+        String[][] tests = {
+                {"a\\Qb", "a\\Qb"},
+                {"a\\Eb", "a\\Eb"},
+                {"\\Q[ba]", "\\Q[ba]"},
+                {"\\\\Q[ba]", "\\\\Q[ab]"},
+                {"\\Q[ba]\\e[ba]", "\\Q[ba]\\e[ba]"},
+                {"\\Q[ba]\\E[ba]", "\\Q[ba]\\E[ab]"},
+                {"\\Q[ba]\\\\E[ba]", "\\Q[ba]\\\\E[ab]"},
+        };
+        UnicodeRegex regex = new UnicodeRegex();
+        for (int i = 0; i < tests.length; ++i) {
+            final String source = tests[i][0];
+            String expected = tests[i][1];
+            String actual = regex.transform(source);
+            assertEquals(source, expected, actual);
+        }
+    }
+
     /**
      * Utility for checking patterns
      */