2

Given two buffers, how do I find the first character position where their contents differ? E.g. for contents abcdef and abcDeF, the first difference would be at point=4. For abc and ab, it would be at point=3.

Is there anything more direct than writing my own binary search based on compare-buffer-substrings to compare progressively smaller substrings?

1 Answer 1

3

Why a binary search? compare-buffer-substrings returns the number of chars that were equal. So you can just do:

(goto-char (+ (point-min) -1 (abs (compare-buffer-substrings BUF1 nil nil BUF2 nil nil)))) 

you may want to let-bind case-fold-search around the call to specify if you prefer to be case-sensitive or case-insensitive, of course.

1
  • lol, sure enough the function's documentation says "if first string is less after N-1 chars". Somehow I managed to miss the after N-1 chars part and got the impression that the return value worked like strcmp in C. Should I just delete my question? Commented Mar 29, 2018 at 12:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.