Skip to content

Commit e547d0e

Browse files
committed
Optimize diff search
1 parent 9ae26d4 commit e547d0e

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

clang/tools/clang-format/clang-format.el

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,6 @@ is a zero-based file offset, assuming ‘utf-8-unix’ coding."
158158
(delete-file (pop ,bind-files-to-delete)))))))
159159

160160

161-
(defun clang-format--vc-diff-match-diff-line (line)
162-
;; We are matching something like:
163-
;; "@@ -80 +80 @@" or "@@ -80,2 +80,2 @@"
164-
;; Return as "<LineStart>:<LineEnd>"
165-
(when (string-match "^@@\s-[0-9,]+\s\\+\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\s@@$" line)
166-
;; If we have multi-line diff
167-
(if (match-string 3 line)
168-
(concat (match-string 1 line)
169-
":"
170-
(number-to-string
171-
(+ (string-to-number (match-string 1 line))
172-
(string-to-number (match-string 3 line)))))
173-
(concat (match-string 1 line) ":" (match-string 1 line)))))
174-
175-
176161
(defun clang-format--vc-diff-get-diff-lines (file-orig file-new)
177162
"Return all line regions that contain diffs between FILE-ORIG and
178163
FILE-NEW. If there is no diff ‘nil’ is returned. Otherwise the
@@ -206,20 +191,30 @@ which can be passed directly to ‘clang-format’."
206191
((= status 0) nil)
207192
;; Return of 1 indicates found diffs and no error.
208193
((= status 1)
209-
;; Iterate through all lines in diff buffer and collect all
210-
;; lines in current buffer that have a diff.
211-
(goto-char (point-min))
212-
(while (not (eobp))
213-
(let ((diff-line (clang-format--vc-diff-match-diff-line
214-
(buffer-substring-no-properties
215-
(line-beginning-position)
216-
(line-end-position)))))
217-
(when diff-line
218-
;; Create list line regions with diffs to pass to
219-
;; clang-format.
220-
(push (concat "--lines=" diff-line) diff-lines)))
221-
(forward-line 1))
222-
(reverse diff-lines))
194+
;; Find and collect all diff lines.
195+
;; We are matching something like:
196+
;; "@@ -80 +80 @@" or "@@ -80,2 +80,2 @@"
197+
(let ((diff-lines-re
198+
"^@@\s-[0-9,]+\s\\+\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\s@@$")
199+
(index 0)
200+
(all-lines
201+
(buffer-substring-no-properties (point-min) (point-max))))
202+
;; We are essentially doing (while (re-search-forward ...) ...)
203+
;; here. We are doing it by hand with (string-match ...) as it
204+
;; is notably faster (about 50%).
205+
(setq index (string-match diff-lines-re all-lines))
206+
(while index
207+
(let ((match1 (string-to-number (match-string 1 all-lines)))
208+
(match3 (if (match-string 3 all-lines)
209+
(string-to-number (match-string 3 all-lines))
210+
nil)))
211+
(push (format
212+
"--lines=%d:%d"
213+
match1
214+
(if match3 (+ match1 match3) match1))
215+
diff-lines))
216+
(setq index (string-match diff-lines-re all-lines (+ index 1)))))
217+
(nreverse diff-lines))
223218
;; Any return != 0 && != 1 indicates some level of error.
224219
(t
225220
(error "(diff returned unsuccessfully %s%s)" status stderr))))))

0 commit comments

Comments
 (0)