Skip to content

Apache Lucene open-source search software

LUCENE-6054: allow repeating the empty automaton

Michael McCandless54156c042d4a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 31be631716b..4b0a2f7e24f 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -212,6 +212,9 @@ Bug Fixes
now throws an exception instead of exhausting CPU/RAM. (Nik
Everett via Mike McCandless)
+* LUCENE-6054: Allow repeating the empty automaton (Nik Everett via
+ Mike McCandless)
+
Documentation
* LUCENE-5392: Add/improve analysis package documentation to reflect
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java b/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
index c480d415c27..f7d1069486c 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
@@ -29,6 +29,13 @@
package org.apache.lucene.util.automaton;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.BytesRefBuilder;
+import org.apache.lucene.util.IntsRef;
+import org.apache.lucene.util.IntsRefBuilder;
+import org.apache.lucene.util.RamUsageEstimator;
+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
@@ -40,13 +47,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
-import org.apache.lucene.util.ArrayUtil;
-import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.BytesRefBuilder;
-import org.apache.lucene.util.IntsRef;
-import org.apache.lucene.util.IntsRefBuilder;
-import org.apache.lucene.util.RamUsageEstimator;
-
/**
* Automata operations.
*
@@ -174,6 +174,10 @@ final public class Operations {
* Complexity: linear in number of states.
*/
static public Automaton repeat(Automaton a) {
+ if (a.getNumStates() == 0) {
+ // Repeating the empty automata will still only accept the empty automata.
+ return a;
+ }
Automaton.Builder builder = new Automaton.Builder();
builder.createState();
builder.setAccept(0, true);
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.java
index 8801bfcfd7d..63bd074b72a 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
@@ -55,4 +55,16 @@ public class TestRegExp extends LuceneTestCase {
// paranoia:
assertTrue(a.toString().length() > 0);
}
+
+ public void testRepeatWithEmptyLanguage() throws Exception {
+ Automaton a = new RegExp("#*").toAutomaton(1000);
+ // paranoia:
+ assertTrue(a.toString().length() > 0);
+ a = new RegExp("#+").toAutomaton(1000);
+ assertTrue(a.toString().length() > 0);
+ a = new RegExp("#{2,10}").toAutomaton(1000);
+ assertTrue(a.toString().length() > 0);
+ a = new RegExp("#?").toAutomaton(1000);
+ assertTrue(a.toString().length() > 0);
+ }
}