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:
parent
26b9564bd5
commit
a111978de8
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user