Subject: bug#13041: 24.2; diacritic-fold-search

From: "Drew Adams" <[email-masked]>
Date: Tue, 4 Dec 2012 22:50:30 -0800

This version of Martin's function (but respecting `case-fold-search') is maybe a
tiny bit simpler.  It could also be a bit slower because of `substring'
returning a copy (vs just incrementing an offset).  It should also be checked
for correctness - not really tested.  FWIW/HTH.

(It does correct the two double `(< value 0)' typos I mentioned earlier.
That should be done in any case.)

(defun decomposed-string-lessp (string1 string2)
  "Return non-nil if decomposed STRING1 is less than decomposed STRING2.
Comparison respects `case-fold-search'."
  (let ((s1  string1)
        (s2  string2)
        prop1  prop2  type1  type2)
    (catch 'found
      (while (and (> (length s1) 0)  (> (length s2) 0))
        (setq prop1  (get-char-code-property (if case-fold-search
                                                 (downcase (elt s1 0))
                                               (elt s1 0))
                                             'decomposition)
              prop2  (get-char-code-property (if case-fold-search
                                                 (downcase (elt s2 0))
                                               (elt s2 0))
                                             'decomposition)
              type1  (car prop1)
              type2  (car prop2))
        (when (eq type1 'compat) (setq s1  (concat (cdr prop1))))
        (when (eq type2 'compat) (setq s2  (concat (cdr prop2))))
        (cond ((eq type1 'compat)
               (let ((cs  (compare-strings
                           s1 0 nil
                           s2 0 (and (not (eq type2 'compat))
                                     (min (length s1) (length s2)))
                           case-fold-search)))
                 (unless (eq cs t) (throw 'found (< cs 0)))))
              ((eq type2 'compat)
               (let ((cs  (compare-strings
                           s1 0 (min (length s2) (length s1))
                           s2 0 nil
                           case-fold-search)))
                 (unless (eq cs t) (throw 'found (< cs 0)))))
              ((= type1 type2)
               (setq s1  (substring s1 1)
                     s2  (substring s2 1)))
              (t (throw 'found (< type1 type2)))))
      (< (length string1) (length string2)))))