emacs-diffs
[Top][All Lists]
Advanced

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

master 51186ed69c: Fix string-lines return for ""


From: Lars Ingebrigtsen
Subject: master 51186ed69c: Fix string-lines return for ""
Date: Sun, 1 May 2022 17:05:23 -0400 (EDT)

branch: master
commit 51186ed69c361abd73d20a96e929b127cd7f15f9
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Fix string-lines return for ""
    
    * lisp/subr.el (string-lines): Return the correct result on ""
    (bug#55213).
---
 lisp/subr.el            | 48 +++++++++++++++++++++++++-----------------------
 test/lisp/subr-tests.el |  2 ++
 2 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/lisp/subr.el b/lisp/subr.el
index d6ea309207..aded02c4f7 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -6747,29 +6747,31 @@ is inserted before adjusting the number of empty lines."
 If OMIT-NULLS, empty lines will be removed from the results.
 If KEEP-NEWLINES, don't strip trailing newlines from the result
 lines."
-  (let ((lines nil)
-        (start 0))
-    (while (< start (length string))
-      (let ((newline (string-search "\n" string start)))
-        (if newline
-            (progn
-              (when (or (not omit-nulls)
-                        (not (= start newline)))
-                (let ((line (substring string start
-                                       (if keep-newlines
-                                           (1+ newline)
-                                         newline))))
-                  (when (not (and keep-newlines omit-nulls
-                                  (equal line "\n")))
-                    (push line lines))))
-              (setq start (1+ newline)))
-          ;; No newline in the remaining part.
-          (if (zerop start)
-              ;; Avoid a string copy if there are no newlines at all.
-              (push string lines)
-            (push (substring string start) lines))
-          (setq start (length string)))))
-    (nreverse lines)))
+  (if (equal string "")
+      (list "")
+    (let ((lines nil)
+          (start 0))
+      (while (< start (length string))
+        (let ((newline (string-search "\n" string start)))
+          (if newline
+              (progn
+                (when (or (not omit-nulls)
+                          (not (= start newline)))
+                  (let ((line (substring string start
+                                         (if keep-newlines
+                                             (1+ newline)
+                                           newline))))
+                    (when (not (and keep-newlines omit-nulls
+                                    (equal line "\n")))
+                      (push line lines))))
+                (setq start (1+ newline)))
+            ;; No newline in the remaining part.
+            (if (zerop start)
+                ;; Avoid a string copy if there are no newlines at all.
+                (push string lines)
+              (push (substring string start) lines))
+            (setq start (length string)))))
+      (nreverse lines))))
 
 (defun buffer-match-p (condition buffer-or-name &optional arg)
   "Return non-nil if BUFFER-OR-NAME matches CONDITION.
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index 93e4475d6b..f4676793ff 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -1029,6 +1029,8 @@ final or penultimate step during initialization."))
   (should-not (readablep (list (make-marker)))))
 
 (ert-deftest test-string-lines ()
+  (should (equal (string-lines "") '("")))
+
   (should (equal (string-lines "foo") '("foo")))
   (should (equal (string-lines "foo\n") '("foo")))
   (should (equal (string-lines "foo\nbar") '("foo" "bar")))



reply via email to

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