emacs-diffs
[Top][All Lists]
Advanced

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

master f329a31: Add string-chop-newline


From: Lars Ingebrigtsen
Subject: master f329a31: Add string-chop-newline
Date: Mon, 21 Dec 2020 16:05:48 -0500 (EST)

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

    Add string-chop-newline
    
    * doc/lispref/strings.texi (Creating Strings): Document it.
    * lisp/emacs-lisp/subr-x.el (string-chop-newline): Add new function.
---
 doc/lispref/strings.texi             | 4 ++++
 etc/NEWS                             | 2 +-
 lisp/emacs-lisp/shortdoc.el          | 2 ++
 lisp/emacs-lisp/subr-x.el            | 4 ++++
 test/lisp/emacs-lisp/subr-x-tests.el | 5 +++++
 5 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index c65d839..17cc1a4 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -428,6 +428,10 @@ string, and if it's negative, to the start of the string 
(using the
 absolute value).
 @end defun
 
+@defun string-chop-newline string
+Remove the final newline, if any, from @var{string}.
+@end defun
+
 @node Modifying Strings
 @section Modifying Strings
 @cindex modifying strings
diff --git a/etc/NEWS b/etc/NEWS
index a6774be..46b8435 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1443,7 +1443,7 @@ that makes it a valid button.
 +++
 *** A number of new string manipulation functions have been added.
 'string-clean-whitespace', 'string-fill', 'string-limit',
-'string-lines', 'string-pad' and 'string-slice'.
+'string-lines', 'string-pad', 'string-chop-newline' and 'string-slice'.
 
 +++
 *** New variable 'current-minibuffer-command'.
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index df31b0a..9bd0663 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -181,6 +181,8 @@ There can be any number of :example/:result elements."
   (string-remove-prefix
    :no-manual t
    :eval (string-remove-prefix "foo" "foobar"))
+  (string-chop-newline
+   :eval (string-chop-newline "foo\n"))
   (string-clean-whitespace
    :eval (string-clean-whitespace " foo   bar   "))
   (string-fill
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 78d0b05..80d4cb9 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -337,6 +337,10 @@ string."
               (and (> length 0)
                    (make-string pad-length (or padding ?\s)))))))
 
+(defun string-chop-newline (string)
+  "Remove the final newline (if any) from STRING."
+  (replace-regexp-in-string "\n\\'" "" string))
+
 (defun replace-region-contents (beg end replace-fn
                                     &optional max-secs max-costs)
   "Replace the region between BEG and END using REPLACE-FN.
diff --git a/test/lisp/emacs-lisp/subr-x-tests.el 
b/test/lisp/emacs-lisp/subr-x-tests.el
index c655fcf..ab5a5bf 100644
--- a/test/lisp/emacs-lisp/subr-x-tests.el
+++ b/test/lisp/emacs-lisp/subr-x-tests.el
@@ -615,5 +615,10 @@
   (should (equal (string-pad "foo" -5 ?-) "--foo"))
   (should (equal (string-pad "foo" 2 ?-) "foo")))
 
+(ert-deftest subr-string-chop-newline ()
+  (should (equal (string-chop-newline "foo\n") "foo"))
+  (should (equal (string-chop-newline "foo\nbar\n") "foo\nbar"))
+  (should (equal (string-chop-newline "foo\nbar") "foo\nbar")))
+
 (provide 'subr-x-tests)
 ;;; subr-x-tests.el ends here



reply via email to

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