From b55aea382c32f4448892265f322a38290ce10305 Mon Sep 17 00:00:00 2001 From: Bozhidar Batsov Date: Sun, 24 Nov 2013 11:31:51 +0200 Subject: [PATCH] * lisp/emacs-lisp/helpers.el: Add some string helpers. (string-trim-left): Removes leading whitespace. (string-trim-right): Removes trailing whitespace. (string-trim): Removes leading and trailing whitespace. --- etc/NEWS | 3 +++ lisp/ChangeLog | 5 +++++ lisp/emacs-lisp/helpers.el | 16 ++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index 01e77a44169..de541fdb951 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -769,6 +769,9 @@ frame. ** New library helpers.el for misc helper functions *** `hash-table-keys' *** `hash-table-values' +*** `string-trim-left' +*** `string-trim-right' +*** `string-trim' ** Obsoleted functions: *** `log10' diff --git a/lisp/ChangeLog b/lisp/ChangeLog index c68a0c402eb..707484734f4 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,10 @@ 2013-11-24 Bozhidar Batsov + * emacs-lisp/helpers.el: Add some string helpers. + (string-trim-left): Removes leading whitespace. + (string-trim-right): Removes trailing whitespace. + (string-trim): Removes leading and trailing whitespace. + * subr.el (string-suffix-p): New function. 2013-11-23 Glenn Morris diff --git a/lisp/emacs-lisp/helpers.el b/lisp/emacs-lisp/helpers.el index 73c2ff1c15c..fd5985467b5 100644 --- a/lisp/emacs-lisp/helpers.el +++ b/lisp/emacs-lisp/helpers.el @@ -37,6 +37,22 @@ (maphash (lambda (_k v) (push v values)) hash-table) values)) +(defsubst string-trim-left (string) + "Remove leading whitespace from STRING." + (if (string-match "\\`[ \t\n\r]+" string) + (replace-match "" t t string) + string)) + +(defsubst string-trim-right (string) + "Remove trailing whitespace from STRING." + (if (string-match "[ \t\n\r]+\\'" string) + (replace-match "" t t string) + string)) + +(defsubst string-trim (string) + "Remove leading and trailing whitespace from STRING." + (string-trim-left (string-trim-right string))) + (provide 'helpers) ;;; helpers.el ends here -- 2.39.2