Skip to content

Commit

Permalink
ivy.el (ivy--regex-fuzzy): Optimize regex "(a).*?(b)" -> "(a)[^b]*(b)"
Browse files Browse the repository at this point in the history
* ivy-test.el (ivy--regex-fuzzy): Update tests.

Thanks to @PythonNut and @dsedivec for the idea.

Re #848
  • Loading branch information
abo-abo committed Nov 26, 2018
1 parent 79543f3 commit 7b6765b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions ivy-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ will bring the behavior in line with the newer Emacsen."

(ert-deftest ivy--regex-fuzzy ()
(should (string= (ivy--regex-fuzzy "tmux")
"\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)"))
"\\(t\\)[^m]*\\(m\\)[^u]*\\(u\\)[^x]*\\(x\\)"))
(should (string= (ivy--regex-fuzzy ".tmux")
"\\(\\.\\).*?\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)"))
"\\(\\.\\)[^t]*\\(t\\)[^m]*\\(m\\)[^u]*\\(u\\)[^x]*\\(x\\)"))
(should (string= (ivy--regex-fuzzy "^tmux")
"^\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)"))
"^\\(t\\)[^m]*\\(m\\)[^u]*\\(u\\)[^x]*\\(x\\)"))
(should (string= (ivy--regex-fuzzy "^tmux$")
"^\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)$"))
"^\\(t\\)[^m]*\\(m\\)[^u]*\\(u\\)[^x]*\\(x\\)$"))
(should (string= (ivy--regex-fuzzy "")
""))
(should (string= (ivy--regex-fuzzy "^")
Expand Down
13 changes: 8 additions & 5 deletions ivy.el
Original file line number Diff line number Diff line change
Expand Up @@ -2516,11 +2516,14 @@ Insert .* between each char."
(if (string-match "\\`\\(\\^?\\)\\(.*?\\)\\(\\$?\\)\\'" str)
(prog1
(concat (match-string 1 str)
(mapconcat
(lambda (x)
(format "\\(%s\\)" (regexp-quote (string x))))
(string-to-list (match-string 2 str))
".*?")
(let ((lst (string-to-list (match-string 2 str))))
(apply #'concat
(cl-mapcar
#'concat
(cons "" (cdr (mapcar (lambda (c) (format "[^%c]*" c))
lst)))
(mapcar (lambda (x) (format "\\(%s\\)" (regexp-quote (char-to-string x))))
lst))))
(match-string 3 str))
(setq ivy--subexps (length (match-string 2 str))))
str))
Expand Down

0 comments on commit 7b6765b

Please sign in to comment.