Apache Lucene open-source search software
Add deprecated complement (~) operator to RegExp (#13739)
Robert Muir22bbc603b40e
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java b/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.javaindex e6ff852ee20..92bfe41b462 100644--- a/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java@@ -368,7 +368,7 @@ public class RegExp {REGEXP_REPEAT_MIN,/** An expression that repeats a minimum and maximum number of times */REGEXP_REPEAT_MINMAX,- /** The complement of an expression */+ /** The complement of a character class */REGEXP_COMPLEMENT,/** A Character */REGEXP_CHAR,@@ -387,7 +387,14 @@ public class RegExp {/** An Interval expression */REGEXP_INTERVAL,/** An expression for a pre-defined class e.g. \w */- REGEXP_PRE_CLASS+ REGEXP_PRE_CLASS,+ /**+ * The complement of an expression.+ *+ * @deprecated Will be removed in Lucene 11+ */+ @Deprecated+ REGEXP_DEPRECATED_COMPLEMENT}// ----- Syntax flags ( <= 0xff ) ------@@ -412,11 +419,23 @@ public class RegExp {/** Syntax flag, enables no optional regexp syntax. */public static final int NONE = 0x0000;- // ----- Matching flags ( > 0xff ) ------+ // ----- Matching flags ( > 0xff <= 0xffff ) ------/** Allows case insensitive matching of ASCII characters. */public static final int ASCII_CASE_INSENSITIVE = 0x0100;+ // ----- Deprecated flags ( > 0xffff ) ------++ /**+ * Allows regexp parsing of the complement (<code>~</code>).+ *+ * <p>Note that processing the complement can require exponential time, but will be bounded by an+ * internal limit. Regexes exceeding the limit will fail with TooComplexToDeterminizeException.+ *+ * @deprecated This method will be removed in Lucene 11+ */+ @Deprecated public static final int DEPRECATED_COMPLEMENT = 0x10000;+// Immutable parsed state/** The type of expression */public final Kind kind;@@ -471,7 +490,7 @@ public class RegExp {* @exception IllegalArgumentException if an error occurred while parsing the regular expression*/public RegExp(String s, int syntax_flags, int match_flags) throws IllegalArgumentException {- if (syntax_flags > ALL) {+ if ((syntax_flags & ~DEPRECATED_COMPLEMENT) > ALL) {throw new IllegalArgumentException("Illegal syntax flag");}@@ -621,6 +640,12 @@ public class RegExp {a = exp1.toAutomaton(automata, automaton_provider);a = Operations.complement(a, Integer.MAX_VALUE);break;+ case REGEXP_DEPRECATED_COMPLEMENT:+ // to ease transitions for users only, support arbitrary complement+ // but bounded by DEFAULT_DETERMINIZE_WORK_LIMIT: must not be configurable.+ a = exp1.toAutomaton(automata, automaton_provider);+ a = Operations.complement(a, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);+ break;case REGEXP_CHAR:if (check(ASCII_CASE_INSENSITIVE)) {a = toCaseInsensitiveChar(c);@@ -768,6 +793,7 @@ public class RegExp {b.append("){").append(min).append(",").append(max).append("}");break;case REGEXP_COMPLEMENT:+ case REGEXP_DEPRECATED_COMPLEMENT:b.append("~(");exp1.toStringBuilder(b);b.append(")");@@ -831,6 +857,7 @@ public class RegExp {case REGEXP_OPTIONAL:case REGEXP_REPEAT:case REGEXP_COMPLEMENT:+ case REGEXP_DEPRECATED_COMPLEMENT:b.append(indent);b.append(kind);b.append('\n');@@ -935,6 +962,7 @@ public class RegExp {case REGEXP_REPEAT_MIN:case REGEXP_REPEAT_MINMAX:case REGEXP_COMPLEMENT:+ case REGEXP_DEPRECATED_COMPLEMENT:exp1.getIdentifiers(set);break;case REGEXP_AUTOMATON:@@ -1011,6 +1039,16 @@ public class RegExp {return newContainerNode(flags, Kind.REGEXP_COMPLEMENT, exp, null);}+ /**+ * Creates node that will compute complement of arbitrary expression.+ *+ * @deprecated Will be removed in Lucene 11+ */+ @Deprecated+ static RegExp makeDeprecatedComplement(int flags, RegExp exp) {+ return newContainerNode(flags, Kind.REGEXP_DEPRECATED_COMPLEMENT, exp, null);+ }+static RegExp makeChar(int flags, int c) {return newLeafNode(flags, Kind.REGEXP_CHAR, null, c, 0, 0, 0, 0, 0);}@@ -1140,7 +1178,9 @@ public class RegExp {}final RegExp parseComplExp() throws IllegalArgumentException {- return parseCharClassExp();+ if (check(DEPRECATED_COMPLEMENT) && match('~'))+ return makeDeprecatedComplement(flags, parseComplExp());+ else return parseCharClassExp();}final RegExp parseCharClassExp() throws IllegalArgumentException {diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExp.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExp.javaindex c934108115d..1747926295e 100644--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExp.java+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExp.java@@ -19,6 +19,7 @@ package org.apache.lucene.util.automaton;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.lucene.tests.util.LuceneTestCase;+import org.apache.lucene.tests.util.automaton.AutomatonTestUtil;import org.apache.lucene.util.BytesRef;public class TestRegExp extends LuceneTestCase {@@ -259,4 +260,19 @@ public class TestRegExp extends LuceneTestCase {public void testRegExpNoStackOverflow() {new RegExp("(a)|".repeat(50000) + "(a)");}++ /**+ * Tests the deprecate complement flag. Keep the simple test only, no random tests to let it cause+ * us pain.+ *+ * @deprecated Remove in Lucene 11+ */+ @Deprecated+ public void testDeprecatedComplement() {+ Automaton expected =+ Operations.complement(+ Automata.makeString("abcd"), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);+ Automaton actual = new RegExp("~(abcd)", RegExp.DEPRECATED_COMPLEMENT).toAutomaton();+ assertTrue(AutomatonTestUtil.sameLanguage(expected, actual));+ }}