Anonymous avatar Anonymous committed f87b710

Regex term: fix prefix computation for * and ? special cases

Comments (0)

Files changed (2)

src/whoosh/query/terms.py

         elif text.startswith("\\A"):
             text = text[2:]
 
-        return PatternQuery._find_prefix(self, text)
+        prefix = PatternQuery._find_prefix(self, text)
+
+        lp = len(prefix)
+        if lp < len(text) and text[lp] in "*?":
+            # we stripped something starting from * or ? - they both MAY mean
+            # "0 times". As we had stripped starting from FIRST special char,
+            # that implies there were only ordinary chars left of it. Thus,
+            # the very last of them is not part of the real prefix:
+            prefix = prefix[:-1]
+        return prefix
 
     # _words() implemented in PatternQuery
 

tests/test_queries.py

         assert_equal(q.simplify(r).__unicode__(), "(word:able OR word:ago)")
         assert_equal(q._find_prefix(q.text), "")
 
+        # special case: ? may mean "zero occurences"
+        q = query.Regex("word", "ah?i")
+        assert_equal(q.simplify(r).__unicode__(), "(word:ahi OR word:aim)")
+        assert_equal(q._find_prefix(q.text), "a")
 
+        # special case: * may mean "zero occurences"
+        q = query.Regex("word", "ah*i")
+        assert_equal(q.simplify(r).__unicode__(), "(word:ahi OR word:aim)")
+        assert_equal(q._find_prefix(q.text), "a")
 
-
-
-
-
-
-
-
-
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.