Add string-chop-newline

* doc/lispref/strings.texi (Creating Strings): Document it.
* lisp/emacs-lisp/subr-x.el (string-chop-newline): Add new function.
This commit is contained in:
Lars Ingebrigtsen 2020-12-21 22:05:37 +01:00
parent e967ba3018
commit f329a3180e
5 changed files with 16 additions and 1 deletions

View 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

View file

@ -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'.

View 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

View 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.

View file

@ -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