]> git.eshelyaron.com Git - emacs.git/commitdiff
Add string-chop-newline
authorLars Ingebrigtsen <larsi@gnus.org>
Mon, 21 Dec 2020 21:05:37 +0000 (22:05 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Mon, 21 Dec 2020 21:05:37 +0000 (22:05 +0100)
* doc/lispref/strings.texi (Creating Strings): Document it.
* lisp/emacs-lisp/subr-x.el (string-chop-newline): Add new function.

doc/lispref/strings.texi
etc/NEWS
lisp/emacs-lisp/shortdoc.el
lisp/emacs-lisp/subr-x.el
test/lisp/emacs-lisp/subr-x-tests.el

index c65d839a028431f13ccc5fc167b4bb38bc482e2f..17cc1a471248d5d04f0f4a12fbb4a773d00da424 100644 (file)
@@ -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
index a6774be8f7373ece04af6b103bdda2b9f59b5651..46b8435a14d28883904a5dd249bfe5463d702a48 100644 (file)
--- 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'.
index df31b0aaf1fc9fcb4f16ca4df5d76eff66831dd7..9bd06636f4d38efc2bd1e0e02686467a734a8271 100644 (file)
@@ -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
index 78d0b054b359493dcf66c0cf476278e849a5a313..80d4cb9b650dca4b04ac9d3ee2dabca2c2e6bcad 100644 (file)
@@ -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.
index c655fcf6eadae0f5060f0013c22afddc28a9aaf2..ab5a5bfa64108eea03f9be70957b9b93ada49d78 100644 (file)
   (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