GNU bug report logs - #13617
24.2.92; [PATCH] Add strip-string

Previous Next

Package: emacs;

Reported by: Leo Liu <sdl.web <at> gmail.com>

Date: Sun, 3 Feb 2013 07:38:02 UTC

Severity: wishlist

Tags: fixed, patch

Found in version 24.2.92

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 13617 in the body.
You can then email your comments to 13617 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to monnier <at> iro.umontreal.ca, bug-gnu-emacs <at> gnu.org:
bug#13617; Package emacs. (Sun, 03 Feb 2013 07:38:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Leo Liu <sdl.web <at> gmail.com>:
New bug report received and forwarded. Copy sent to monnier <at> iro.umontreal.ca, bug-gnu-emacs <at> gnu.org. (Sun, 03 Feb 2013 07:38:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Leo Liu <sdl.web <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 24.2.92; [PATCH] Add strip-string
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.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13617; Package emacs. (Sun, 03 Feb 2013 08:26:02 GMT) Full text and rfc822 format available.

Message #8 received at 13617 <at> debbugs.gnu.org (full text, mbox):

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Leo Liu <sdl.web <at> gmail.com>
Cc: 13617 <at> debbugs.gnu.org
Subject: Re: bug#13617: 24.2.92; [PATCH] Add strip-string
Date: Sun, 03 Feb 2013 09:23:58 +0100
Leo Liu <sdl.web <at> gmail.com> writes:

> +		  (concat "[" (or leading-chars " \f\t\n\r\v") "]*")))

What about characters that are special in [...]?

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13617; Package emacs. (Sun, 03 Feb 2013 08:53:02 GMT) Full text and rfc822 format available.

Message #11 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Leo Liu <sdl.web <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Re: bug#13617: 24.2.92; [PATCH] Add strip-string
Date: Sun, 03 Feb 2013 16:50:53 +0800
On 2013-02-03 16:23 +0800, Andreas Schwab wrote:
> What about characters that are special in [...]?

I am thinking of document how leading-chars and trailing-chars should be
written i.e. should be written as if in a [] regexp but without the
enclosing []. What do you think?

Leo





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13617; Package emacs. (Sun, 03 Feb 2013 08:58:02 GMT) Full text and rfc822 format available.

Message #14 received at 13617 <at> debbugs.gnu.org (full text, mbox):

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Leo Liu <sdl.web <at> gmail.com>
Cc: 13617 <at> debbugs.gnu.org
Subject: Re: bug#13617: 24.2.92; [PATCH] Add strip-string
Date: Sun, 03 Feb 2013 09:56:20 +0100
You could use the same rules as skip-chars-forward.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13617; Package emacs. (Sun, 03 Feb 2013 09:42:02 GMT) Full text and rfc822 format available.

Message #17 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Leo Liu <sdl.web <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Re: bug#13617: 24.2.92; [PATCH] Add strip-string
Date: Sun, 03 Feb 2013 17:40:01 +0800
On 2013-02-03 16:56 +0800, Andreas Schwab wrote:
> You could use the same rules as skip-chars-forward.

I looked at skip-chars-forward before opening this bug but found it
complicate things by introducing new rules.

In strip-string the idea is to have a default covering 90% of the use
cases:

(strip-string s)        ; strip both
(strip-string s nil "") ; strip leading
(strip-string s "")     ; strip trailing

Refer users to regexp as in (info "(elisp)Regexp Special") for the
remaining 10% use cases.

Regards,
Leo





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13617; Package emacs. (Mon, 25 Mar 2013 03:45:02 GMT) Full text and rfc822 format available.

Message #20 received at 13617 <at> debbugs.gnu.org (full text, mbox):

From: Leo Liu <sdl.web <at> gmail.com>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 13617 <at> debbugs.gnu.org
Subject: Re: bug#13617: 24.2.92; [PATCH] Add strip-string
Date: Mon, 25 Mar 2013 11:42:18 +0800
Hello Stefan,

On 2013-02-03 17:40 +0800, Leo Liu wrote:
> I looked at skip-chars-forward before opening this bug but found it
> complicate things by introducing new rules.
>
> In strip-string the idea is to have a default covering 90% of the use
> cases:
>
> (strip-string s)        ; strip both
> (strip-string s nil "") ; strip leading
> (strip-string s "")     ; strip trailing
>
> Refer users to regexp as in (info "(elisp)Regexp Special") for the
> remaining 10% use cases.
>
> Regards,
> Leo

What is your take on this?

BTW, comment-string-strip cannot handle strings with newline in them, is
this an oversight?

Leo




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13617; Package emacs. (Mon, 25 Mar 2013 07:50:02 GMT) Full text and rfc822 format available.

Message #23 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Andreas Röhler <andreas.roehler <at> easy-emacs.de>
To: bug-gnu-emacs <at> gnu.org
Subject: Re: bug#13617: 24.2.92; [PATCH] Add strip-string
Date: Mon, 25 Mar 2013 08:48:22 +0100
Am 03.02.2013 10:40, schrieb Leo Liu:
> On 2013-02-03 16:56 +0800, Andreas Schwab wrote:
>> You could use the same rules as skip-chars-forward.
>

+1
> I looked at skip-chars-forward before opening this bug but found it
> complicate things by introducing new rules.
>

Which one?
IMO you may use skip-chars... internally, which is fast and reliable.

> In strip-string the idea is to have a default covering 90% of the use
> cases:
>
> (strip-string s)        ; strip both
> (strip-string s nil "") ; strip leading
> (strip-string s "")     ; strip trailing

Suggest keeping either boolean for not, resp. default,
or a string containing chars overriding the default.

while this is obsolet meanwhile:

http://lists.gnu.org/archive/html/gnu-emacs-sources/2007-01/msg00004.html

Andreas

>
> Refer users to regexp as in (info "(elisp)Regexp Special") for the
> remaining 10% use cases.
>
> Regards,
> Leo
>
>
>
>
>





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13617; Package emacs. (Wed, 24 Feb 2016 05:53:01 GMT) Full text and rfc822 format available.

Message #26 received at 13617 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Leo Liu <sdl.web <at> gmail.com>
Cc: 13617 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#13617: 24.2.92; [PATCH] Add strip-string
Date: Wed, 24 Feb 2016 16:51:47 +1100
Leo Liu <sdl.web <at> gmail.com> writes:

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

This seems to have been implemented in the meantime as `string-trim'.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) fixed. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Wed, 24 Feb 2016 05:53:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 13617 <at> debbugs.gnu.org and Leo Liu <sdl.web <at> gmail.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Wed, 24 Feb 2016 05:53:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 23 Mar 2016 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 9 years and 176 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.