1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-17 10:06:13 +00:00

Count zero-length matches in `count-matches' correctly

* lisp/replace.el (how-many): Count zero-length matches correctly
(bug#27359).
This commit is contained in:
Lars Ingebrigtsen 2021-07-05 16:30:43 +02:00
parent 26b9564bd5
commit a111978de8
2 changed files with 17 additions and 6 deletions

View File

@ -1089,17 +1089,17 @@ a previously found match."
rend (point-max)))
(goto-char rstart))
(let ((count 0)
opoint
(case-fold-search
(if (and case-fold-search search-upper-case)
(isearch-no-upper-case-p regexp t)
case-fold-search)))
(while (and (< (point) rend)
(progn (setq opoint (point))
(re-search-forward regexp rend t)))
(if (= opoint (point))
(forward-char 1)
(setq count (1+ count))))
(re-search-forward regexp rend t))
;; Ensure forward progress on zero-length matches like "^$".
(when (and (= (match-beginning 0) (match-end 0))
(not (eobp)))
(forward-char 1))
(setq count (1+ count)))
(when interactive (message (ngettext "%d occurrence"
"%d occurrences"
count)

View File

@ -601,4 +601,15 @@ bound to HIGHLIGHT-LOCUS."
(if (match-string 2) "R" "L")))
(should (equal (buffer-string) after)))))
(ert-deftest test-count-matches ()
(with-temp-buffer
(insert "oooooooooo")
(goto-char (point-min))
(should (= (count-matches "oo") 5))
(should (= (count-matches "o+") 1)))
(with-temp-buffer
(insert "o\n\n\n\no\n\n")
(goto-char (point-min))
(should (= (count-matches "^$") 4))))
;;; replace-tests.el ends here