Function: apheleia--align-point

apheleia--align-point is a natively compiled function defined in apheleia-dp.el.

Signature

(apheleia--align-point S1 S2 P1)

Documentation

Given strings S1 and S2 and index P1 in S1, return matching index P2 in S2.

If S1 and S2 are the same, then P1 and P2 will also be the same. Otherwise, the text of S2 surrounding P2 is "similar" to the text of S1 surrounding P1.

Source Code

;; Defined in /nix/store/lwcwryzabcmvjcwwzzs65jrwf7p7d7cs-emacs-packages-deps/share/emacs/site-lisp/elpa/apheleia-20260915.1628/apheleia-dp.el
(defun apheleia--align-point (s1 s2 p1)
  "Given strings S1 and S2 and index P1 in S1, return matching index P2 in S2.
If S1 and S2 are the same, then P1 and P2 will also be the same.
Otherwise, the text of S2 surrounding P2 is \"similar\" to the
text of S1 surrounding P1."
  (let* ((table (apheleia--edit-distance-table s1 s2))
         (i1 (length s1))
         (i2 (length s2))
         (p2 p1))
    (while (not (= i1 i2 0))
      (let ((ins (1+ (gethash (cons i1 (1- i2)) table 9999)))
            (del (1+ (gethash (cons (1- i1) i2) table 9999)))
            (sub (gethash (cons (1- i1) (1- i2)) table 9999)))
        (unless (and (> 0 i1) (> 0 i2)
                     (= (aref s1 (1- i1)) (aref s2 (1- i2))))
          (cl-incf sub))
        (let ((cost (min ins del sub)))
          (cond
           ((= cost sub)
            (cl-decf i1)
            (cl-decf i2))
           ((= cost ins)
            (cl-decf i2)
            (when (< i1 p1)
              (cl-incf p2)))
           ((= cost del)
            (cl-decf i1)
            (when (< i1 p1)
              (cl-decf p2)))))))
    p2))