unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#13617: 24.2.92; [PATCH] Add strip-string
@ 2013-02-03  7:35 Leo Liu
  2013-02-03  8:23 ` Andreas Schwab
  2016-02-24  5:51 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 8+ messages in thread
From: Leo Liu @ 2013-02-03  7:35 UTC (permalink / raw)
  To: 13617

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.





^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#13617: 24.2.92; [PATCH] Add strip-string
  2013-02-03  7:35 bug#13617: 24.2.92; [PATCH] Add strip-string Leo Liu
@ 2013-02-03  8:23 ` Andreas Schwab
  2013-02-03  8:50   ` Leo Liu
  2016-02-24  5:51 ` Lars Ingebrigtsen
  1 sibling, 1 reply; 8+ messages in thread
From: Andreas Schwab @ 2013-02-03  8:23 UTC (permalink / raw)
  To: Leo Liu; +Cc: 13617

Leo Liu <sdl.web@gmail.com> writes:

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

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

Andreas.

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





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#13617: 24.2.92; [PATCH] Add strip-string
  2013-02-03  8:23 ` Andreas Schwab
@ 2013-02-03  8:50   ` Leo Liu
  2013-02-03  8:56     ` Andreas Schwab
  0 siblings, 1 reply; 8+ messages in thread
From: Leo Liu @ 2013-02-03  8:50 UTC (permalink / raw)
  To: 13617

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






^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#13617: 24.2.92; [PATCH] Add strip-string
  2013-02-03  8:50   ` Leo Liu
@ 2013-02-03  8:56     ` Andreas Schwab
  2013-02-03  9:40       ` Leo Liu
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Schwab @ 2013-02-03  8:56 UTC (permalink / raw)
  To: Leo Liu; +Cc: 13617

You could use the same rules as skip-chars-forward.

Andreas.

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





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#13617: 24.2.92; [PATCH] Add strip-string
  2013-02-03  8:56     ` Andreas Schwab
@ 2013-02-03  9:40       ` Leo Liu
  2013-03-25  3:42         ` Leo Liu
  2013-03-25  7:48         ` Andreas Röhler
  0 siblings, 2 replies; 8+ messages in thread
From: Leo Liu @ 2013-02-03  9:40 UTC (permalink / raw)
  To: 13617

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






^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#13617: 24.2.92; [PATCH] Add strip-string
  2013-02-03  9:40       ` Leo Liu
@ 2013-03-25  3:42         ` Leo Liu
  2013-03-25  7:48         ` Andreas Röhler
  1 sibling, 0 replies; 8+ messages in thread
From: Leo Liu @ 2013-03-25  3:42 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 13617

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





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#13617: 24.2.92; [PATCH] Add strip-string
  2013-02-03  9:40       ` Leo Liu
  2013-03-25  3:42         ` Leo Liu
@ 2013-03-25  7:48         ` Andreas Röhler
  1 sibling, 0 replies; 8+ messages in thread
From: Andreas Röhler @ 2013-03-25  7:48 UTC (permalink / raw)
  To: 13617

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
>
>
>
>
>






^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#13617: 24.2.92; [PATCH] Add strip-string
  2013-02-03  7:35 bug#13617: 24.2.92; [PATCH] Add strip-string Leo Liu
  2013-02-03  8:23 ` Andreas Schwab
@ 2016-02-24  5:51 ` Lars Ingebrigtsen
  1 sibling, 0 replies; 8+ messages in thread
From: Lars Ingebrigtsen @ 2016-02-24  5:51 UTC (permalink / raw)
  To: Leo Liu; +Cc: 13617, Stefan Monnier

Leo Liu <sdl.web@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





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-02-24  5:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-03  7:35 bug#13617: 24.2.92; [PATCH] Add strip-string Leo Liu
2013-02-03  8:23 ` Andreas Schwab
2013-02-03  8:50   ` Leo Liu
2013-02-03  8:56     ` Andreas Schwab
2013-02-03  9:40       ` Leo Liu
2013-03-25  3:42         ` Leo Liu
2013-03-25  7:48         ` Andreas Röhler
2016-02-24  5:51 ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).