emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master c44190c: Fix string-distance for two empty strings


From: Mattias Engdegård
Subject: master c44190c: Fix string-distance for two empty strings
Date: Thu, 23 Sep 2021 13:12:26 -0400 (EDT)

branch: master
commit c44190ca5b9873fceae8aee7034ce8e89c42d4dd
Author: Philip Kaludercic <philipk@posteo.net>
Commit: Mattias Engdegård <mattiase@acm.org>

    Fix string-distance for two empty strings
    
    * fns.c (Fstring_distance): Avoid using uninitialized memory.
    * test/src/fns-tests.el (test-string-distance): Add test cases.
---
 src/fns.c             |  2 +-
 test/src/fns-tests.el | 10 +++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/fns.c b/src/fns.c
index 4e74589..a72e41a 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -322,7 +322,7 @@ Letter-case is significant, but text properties are 
ignored. */)
 
   USE_SAFE_ALLOCA;
   ptrdiff_t *column = SAFE_ALLOCA ((len1 + 1) * sizeof (ptrdiff_t));
-  for (y = 1; y <= len1; y++)
+  for (y = 0; y <= len1; y++)
     column[y] = y;
 
   if (use_byte_compare)
diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el
index 9f6593a..bd5a435 100644
--- a/test/src/fns-tests.el
+++ b/test/src/fns-tests.el
@@ -786,7 +786,15 @@
   ;; string containing hanzi character, compare by character
   (should (equal 2 (string-distance "ab" "ab我她")))
   (should (equal 1 (string-distance "ab" "a我b")))
-  (should (equal 1 (string-distance "我" "她"))))
+  (should (equal 1 (string-distance "我" "她")))
+
+  ;; correct behaviour with empty strings
+  (should (equal 0 (string-distance "" "")))
+  (should (equal 0 (string-distance "" "" t)))
+  (should (equal 1 (string-distance "x" "")))
+  (should (equal 1 (string-distance "x" "" t)))
+  (should (equal 1 (string-distance "" "x")))
+  (should (equal 1 (string-distance "" "x" t))))
 
 (ert-deftest test-bignum-eql ()
   "Test that `eql' works for bignums."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]