From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Lars Hansen Newsgroups: gmane.emacs.devel Subject: Re: string-strip Date: Mon, 19 Jun 2006 19:59:52 +0200 Message-ID: <4496E618.8090401@soem.dk> References: <449266B2.7030500@easy-emacs.de> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1150740034 24424 80.91.229.2 (19 Jun 2006 18:00:34 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 19 Jun 2006 18:00:34 +0000 (UTC) Cc: Andreas Roehler , emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jun 19 20:00:31 2006 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1FsO2s-0005zX-2l for ged-emacs-devel@m.gmane.org; Mon, 19 Jun 2006 20:00:18 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FsO2r-0004vs-GI for ged-emacs-devel@m.gmane.org; Mon, 19 Jun 2006 14:00:17 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1FsO2d-0004so-Mr for emacs-devel@gnu.org; Mon, 19 Jun 2006 14:00:03 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1FsO2b-0004oX-Jy for emacs-devel@gnu.org; Mon, 19 Jun 2006 14:00:03 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FsO2b-0004oG-D2 for emacs-devel@gnu.org; Mon, 19 Jun 2006 14:00:01 -0400 Original-Received: from [212.99.225.245] (helo=odin.broadcom.dk) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA:32) (Exim 4.52) id 1FsOCu-0002lH-BN; Mon, 19 Jun 2006 14:10:40 -0400 Original-Received: from pppoe3-ves.broadcom.dk ([212.99.255.42] helo=[10.17.0.131]) by odin.broadcom.dk with esmtp (Exim 4.24; FreeBSD) id 1FsO1L-0004ar-VR; Mon, 19 Jun 2006 19:58:44 +0200 User-Agent: Debian Thunderbird 1.0.2 (X11/20060423) X-Accept-Language: en-us, en Original-To: rms@gnu.org In-Reply-To: X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:56002 Archived-At: Richard Stallman wrote: > (defun my-string-strip (str) > "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space." > (string-match "\\`\\s-*\\(.*?\\)\\s-*\n?\\'" str) > (match-string 1 str)) > > (string-strip "asdf ")"asdf" > (string-strip " asdf")"asdf" > (string-strip " asdf ")"asdf" > >It is a useful function and a simple one, and it won't delay the >release much. If you write the manual and NEWS changes for it first, >I will say yes. > > I suggest some changes: 1. Allow newlines in STR. 2. Treat newlines as white space. 3. Return nil when resulting string is empty. 4. Use save-match-data. This implementation works that way: (defun string-strip (str) "Return STR with leading and traling white space removed. If the resulting str has zero lenght, nil is returned." (save-match-data (string-match "\\`[[:space:]\n]*\\(\\(.\\|\n\\)*?\\)[[:space:]\n]*\\'" str) (let ((result (match-string 1 str))) (unless (zerop (length result)) result)))) Here are some tests: (my-string-strip " foo ") => "foo" (my-string-strip " foo\n ") => (args-out-of-range " foo\n" 8 9) (my-string-strip " \nfoo ") => (args-out-of-range " \nfoo" 8 9) (my-string-strip " foo\nbar ") => " " (my-string-strip " ") => "" (string-strip " foo ") => "foo" (string-strip " foo\n ") => "foo" (string-strip " \nfoo ") => "foo" (string-strip " foo\nbar ") => "foo\nbar" (string-strip " ") => nil