Fix value< string comparison ungoodthink
authorMattias Engdegård <mattiase@acm.org>
Sun, 28 Apr 2024 21:17:48 +0000 (23:17 +0200)
committerMattias Engdegård <mattiase@acm.org>
Mon, 29 Apr 2024 12:29:52 +0000 (14:29 +0200)
* src/fns.c (string_cmp): Fix bad comparisons for certain strings.
This only affected `value<` for aggregates, not `string<`.
* test/src/fns-tests.el (fns-value<-ordered): Add test cases.

src/fns.c
test/src/fns-tests.el

index e987d64319f6f6305acda133951268feabed8244..9be42aa8b68a08ad9e6cb39988b23ec5994bcc0b 100644 (file)
--- a/src/fns.c
+++ b/src/fns.c
@@ -481,7 +481,7 @@ string_cmp (Lisp_Object string1, Lisp_Object string2)
       int d = memcmp (SSDATA (string1), SSDATA (string2), n);
       if (d)
        return d;
-      return n < SCHARS (string2) ? -1 : n > SCHARS (string2);
+      return n < SCHARS (string2) ? -1 : n < SCHARS (string1);
     }
   else if (STRING_MULTIBYTE (string1) && STRING_MULTIBYTE (string2))
     {
@@ -515,7 +515,7 @@ string_cmp (Lisp_Object string1, Lisp_Object string2)
 
       if (b >= nb)
        /* One string is a prefix of the other.  */
-       return b < nb2 ? -1 : b > nb2;
+       return b < nb2 ? -1 : b < nb1;
 
       /* Now back up to the start of the differing characters:
         it's the last byte not having the bit pattern 10xxxxxx.  */
@@ -540,7 +540,7 @@ string_cmp (Lisp_Object string1, Lisp_Object string2)
          if (c1 != c2)
            return c1 < c2 ? -1 : 1;
        }
-      return i1 < SCHARS (string2) ? -1 : i1 > SCHARS (string2);
+      return i1 < SCHARS (string2) ? -1 : i1 < SCHARS (string1);
     }
   else
     {
@@ -553,7 +553,7 @@ string_cmp (Lisp_Object string1, Lisp_Object string2)
          if (c1 != c2)
            return c1 < c2 ? -1 : 1;
        }
-      return i1 < SCHARS (string2) ? -1 : i1 > SCHARS (string2);
+      return i1 < SCHARS (string2) ? -1 : i1 < SCHARS (string1);
     }
 }
 
index 5ba7e49324ad02559fe22ed954104a8d78af502b..ca5b10db7052c5b0bee2f320333bb8096f4830eb 100644 (file)
                    ;; strings
                    ("" . "a") ("a" . "b") ("A" . "a") ("abc" . "abd")
                    ("b" . "ba")
+                   ;; strings again, but in a context where 3-way comparison
+                   ;; matters
+                   (("" . 2) . ("a" . 1))
+                   (("å" . 2) . ("åü" . 1))
+                   (("a" . 2) . ("aå" . 1))
+                   (("\x80" . 2) . ("\x80å" . 1))
 
                    ;; lists
                    ((1 2 3) . (2 3 4)) ((2) . (2 1)) (() . (0))