]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/emacs-lisp/helpers.el: Add some string helpers.
authorBozhidar Batsov <bozhidar@batsov.com>
Sun, 24 Nov 2013 09:31:51 +0000 (11:31 +0200)
committerBozhidar Batsov <bozhidar@batsov.com>
Sun, 24 Nov 2013 09:31:51 +0000 (11:31 +0200)
(string-trim-left): Removes leading whitespace.
(string-trim-right): Removes trailing whitespace.
(string-trim): Removes leading and trailing whitespace.

etc/NEWS
lisp/ChangeLog
lisp/emacs-lisp/helpers.el

index 01e77a441694b925dc68d5a800eb53a7731f5fe4..de541fdb951e575a53728fadce94ff4818d587c0 100644 (file)
--- 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'
index c68a0c402ebfcb8906856dab84a1cd8073884a5b..707484734f4859d06d038791cdc08b5de7726946 100644 (file)
@@ -1,5 +1,10 @@
 2013-11-24  Bozhidar Batsov  <bozhidar@batsov.com>
 
+       * 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  <rgm@gnu.org>
index 73c2ff1c15cfef8ffb09f2153990e68d6d17197e..fd5985467b5bafd98cea57feda1f5d9b23ca0cef 100644 (file)
     (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