Subject: bug#13617: 24.2.92; [PATCH] Add strip-string

From: Leo Liu <[email-masked]>
Date: Sun, 03 Feb 2013 15:35:58 +0800

Hello Stefan,

Any objection to adding a general utility function strip-string?

Thanks,
Leo.

diff --git a/lisp/subr.el b/lisp/subr.el
index 72b629d6..afa0b753 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3683,6 +3683,19 @@ (defun subst-char-in-string (fromchar tochar string &optional inplace)
 	  (aset newstr i tochar)))
     newstr))
 
+(defun strip-string (string &optional leading-chars trailing-chars)
+  "Strip STRING of LEADING-CHARS and TRAILING-CHARS.
+LEADING-CHARS and TRAILING-CHARS defaults to \" \f\t\n\r\v\"."
+  (let ((lchars (if (equal leading-chars "")
+		    ""
+		  (concat "[" (or leading-chars " \f\t\n\r\v") "]*")))
+	(rchars (if (equal trailing-chars "")
+		    ""
+		  (concat "[" (or trailing-chars " \f\t\n\r\v") "]*"))))
+    (string-match (concat "\\`" lchars "\\(\\(?:.\\|\n\\)*?\\)" rchars "\\'")
+		  string)
+    (match-string 1 string)))
+
 (defun replace-regexp-in-string (regexp rep string &optional
 					fixedcase literal subexp start)
   "Replace all matches for REGEXP with REP in STRING.