* string-strip
@ 2006-06-16 8:07 Andreas Roehler
2006-06-19 7:42 ` string-strip Richard Stallman
0 siblings, 1 reply; 35+ messages in thread
From: Andreas Roehler @ 2006-06-16 8:07 UTC (permalink / raw)
Hi,
quite often I need a function to correct user-input,
i.e. wrongly inserted white spaces.
Used `comment-string-strip' to that purpose, which
works fine so far. However, there are two problems with
it:
- it's rather difficult to use, as its requires
handling of three arguments, where just one would
sufficient in the cases I need it.
- it's in newcomment.el, used for a special purpose
there and may be changed. So I hesitate to
make programs depend on them.
What about to introduce a simplified version of `comment-string-strip'
into subr.el - if it's not already somewhere?
(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"
__
Andreas Roehler
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-16 8:07 string-strip Andreas Roehler
@ 2006-06-19 7:42 ` Richard Stallman
2006-06-19 17:59 ` string-strip Lars Hansen
0 siblings, 1 reply; 35+ messages in thread
From: Richard Stallman @ 2006-06-19 7:42 UTC (permalink / raw)
Cc: emacs-devel
What about to introduce a simplified version of `comment-string-strip'
into subr.el - if it's not already somewhere?
(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.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-19 7:42 ` string-strip Richard Stallman
@ 2006-06-19 17:59 ` Lars Hansen
2006-06-19 18:24 ` string-strip Stuart D. Herring
0 siblings, 1 reply; 35+ messages in thread
From: Lars Hansen @ 2006-06-19 17:59 UTC (permalink / raw)
Cc: Andreas Roehler, emacs-devel
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
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-19 17:59 ` string-strip Lars Hansen
@ 2006-06-19 18:24 ` Stuart D. Herring
2006-06-19 20:20 ` string-strip Lars Hansen
` (2 more replies)
0 siblings, 3 replies; 35+ messages in thread
From: Stuart D. Herring @ 2006-06-19 18:24 UTC (permalink / raw)
Cc: emacs-devel
> 1. Allow newlines in STR.
> 2. Treat newlines as white space.
Shouldn't it really be up to the syntax table? Or is this supposed to be
so much in Lisp land that the buffer shouldn't matter?
> 3. Return nil when resulting string is empty.
Why do we want to do that? How often do we want
(if (string-strip str) ; it's not empty
...or is it for some other reason? It just seems strange to me to have
the return value not always be a string.
> (defun string-strip (str)
> "Return STR with leading and traling white space removed.
"trailing", of course.
> If the resulting str has zero lenght, nil is returned."
"length", of course.
> (save-match-data
> (string-match
> "\\`[[:space:]\n]*\\(\\(.\\|\n\\)*?\\)[[:space:]\n]*\\'" str)
Probably want to make the inner group non-capturing, at least. Moreover,
is this really the most efficient/reasonable way to match "as little as
possible of absolutely anything"? I can't think of an improvement
immediately, but it just seems ugly. I'd be tempted to string-match the
leading and trailing whitespace separately and then just use the substring
indices:
(let ((begin (save-match-data (string-match "\\`[[:space:]\n]*" str)
(match-end 0))))
(substring str begin
(max begin (save-match-data
(string-match "[[:space:]\n]*\\'" str)
(match-beginning 0))))))
It'd be even simpler if it weren't for the case of all-whitespace; it
might make sense to instead check for all-whitespace and return "" (or
nil) then.
Davis
--
This product is sold by volume, not by mass. If it appears too dense or
too sparse, it is because mass-energy conversion has occurred during
shipping.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-19 18:24 ` string-strip Stuart D. Herring
@ 2006-06-19 20:20 ` Lars Hansen
2006-06-19 20:23 ` string-strip Andreas Schwab
2006-06-19 23:19 ` string-strip Richard Stallman
2 siblings, 0 replies; 35+ messages in thread
From: Lars Hansen @ 2006-06-19 20:20 UTC (permalink / raw)
Cc: emacs-devel
Stuart D. Herring wrote:
>>1. Allow newlines in STR.
>>2. Treat newlines as white space.
>>
>>
>
>Shouldn't it really be up to the syntax table? Or is this supposed to be
>so much in Lisp land that the buffer shouldn't matter?
>
At least newlines should be allowed.
(my-string-strip " foo\n ") => (args-out-of-range " foo\n" 8 9)
(my-string-strip " foo\nbar ") => " "
is not acceptable IMO.
>>3. Return nil when resulting string is empty.
>>
>>
>
>Why do we want to do that?
>
To do something like
(defun (str)
(setq str (string-strip str))
(if str
;; handle string
...
;; no input
...
>"trailing", of course.
>
:-)
>"length", of course.
>
>
:-o
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-19 18:24 ` string-strip Stuart D. Herring
2006-06-19 20:20 ` string-strip Lars Hansen
@ 2006-06-19 20:23 ` Andreas Schwab
2006-06-19 22:04 ` string-strip Kim F. Storm
2006-06-19 23:19 ` string-strip Richard Stallman
2 siblings, 1 reply; 35+ messages in thread
From: Andreas Schwab @ 2006-06-19 20:23 UTC (permalink / raw)
Cc: emacs-devel
"Stuart D. Herring" <herring@lanl.gov> writes:
>> 1. Allow newlines in STR.
>> 2. Treat newlines as white space.
>
> Shouldn't it really be up to the syntax table? Or is this supposed to be
> so much in Lisp land that the buffer shouldn't matter?
The current syntax table is associated with the current buffer, but the
string is not, so it would be confusing make the operation depend on the
current buffer. It would be ok if the function would operate on some
buffer contents.
Andreas.
--
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-19 20:23 ` string-strip Andreas Schwab
@ 2006-06-19 22:04 ` Kim F. Storm
0 siblings, 0 replies; 35+ messages in thread
From: Kim F. Storm @ 2006-06-19 22:04 UTC (permalink / raw)
Cc: emacs-devel
Andreas Schwab <schwab@suse.de> writes:
> "Stuart D. Herring" <herring@lanl.gov> writes:
>
>>> 1. Allow newlines in STR.
>>> 2. Treat newlines as white space.
>>
>> Shouldn't it really be up to the syntax table? Or is this supposed to be
>> so much in Lisp land that the buffer shouldn't matter?
>
> The current syntax table is associated with the current buffer, but the
> string is not, so it would be confusing make the operation depend on the
> current buffer. It would be ok if the function would operate on some
> buffer contents.
Since there is currently no need for this function, and there is
obviously disagreement on what it should do, I suggest that we
postpone the issue until after the release.
--
Kim F. Storm <storm@cua.dk> http://www.cua.dk
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-19 18:24 ` string-strip Stuart D. Herring
2006-06-19 20:20 ` string-strip Lars Hansen
2006-06-19 20:23 ` string-strip Andreas Schwab
@ 2006-06-19 23:19 ` Richard Stallman
2006-06-20 6:34 ` string-strip Lars Hansen
2 siblings, 1 reply; 35+ messages in thread
From: Richard Stallman @ 2006-06-19 23:19 UTC (permalink / raw)
Cc: emacs-devel
> 1. Allow newlines in STR.
> 2. Treat newlines as white space.
Shouldn't it really be up to the syntax table? Or is this supposed to be
so much in Lisp land that the buffer shouldn't matter?
The syntax table is relevant only for a buffer.
This has nothing to do with any particular buffer.
> 3. Return nil when resulting string is empty.
That would be a bad idea.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-19 23:19 ` string-strip Richard Stallman
@ 2006-06-20 6:34 ` Lars Hansen
2006-06-20 17:55 ` string-strip Richard Stallman
0 siblings, 1 reply; 35+ messages in thread
From: Lars Hansen @ 2006-06-20 6:34 UTC (permalink / raw)
Cc: andreas.roehler, emacs-devel
Richard Stallman wrote:
> > 3. Return nil when resulting string is empty.
>
>That would be a bad idea.
>
>
Ok. Then what about
(defun string-strip (str)
"Return a copy of STR with leading and trailing white space removed."
(save-match-data
(string-match
"\\`[[:space:]\n]*\\(\\(?:.\\|\n\\)*?\\)[[:space:]\n]*\\'" str)
(match-string 1 str)))
(string-strip " foo ") => "foo"
(string-strip " \nfoo\n ") => "foo"
(string-strip " foo\nbar ") => "foo\nbar"
(string-strip " ") => ""
(string-strip " \n ") => ""
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-20 6:34 ` string-strip Lars Hansen
@ 2006-06-20 17:55 ` Richard Stallman
2006-06-20 19:28 ` string-strip Andreas Roehler
0 siblings, 1 reply; 35+ messages in thread
From: Richard Stallman @ 2006-06-20 17:55 UTC (permalink / raw)
Cc: andreas.roehler, emacs-devel
(string-match
"\\`[[:space:]\n]*\\(\\(?:.\\|\n\\)*?\\)[[:space:]\n]*\\'" str)
I think [[:space:]] obeys the syntax table,
and that is not good.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-20 17:55 ` string-strip Richard Stallman
@ 2006-06-20 19:28 ` Andreas Roehler
2006-06-20 19:50 ` string-strip Nick Roberts
0 siblings, 1 reply; 35+ messages in thread
From: Andreas Roehler @ 2006-06-20 19:28 UTC (permalink / raw)
Richard Stallman schrieb:
> (string-match
> "\\`[[:space:]\n]*\\(\\(?:.\\|\n\\)*?\\)[[:space:]\n]*\\'" str)
>
> I think [[:space:]] obeys the syntax table,
> and that is not good.
>
>
Why not? :
Not to lobby the concept of syntax-tables as such, but
"The syntax table is relevant only for a buffer.
This has nothing to do with any particular buffer."
AFAIU just the syntax of the current buffer will be used
- where is the problem?
A white space is a white space is a white space ...
its up to the mode and the syntax with it to define - if its wrong defined,
it's not lisp to blame for.
_
Andreas Roehler
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-20 19:28 ` string-strip Andreas Roehler
@ 2006-06-20 19:50 ` Nick Roberts
2006-06-20 22:03 ` string-strip Kim F. Storm
0 siblings, 1 reply; 35+ messages in thread
From: Nick Roberts @ 2006-06-20 19:50 UTC (permalink / raw)
Cc: rms, emacs-devel
> > (string-match
> > "\\`[[:space:]\n]*\\(\\(?:.\\|\n\\)*?\\)[[:space:]\n]*\\'" str)
> >
> > I think [[:space:]] obeys the syntax table,
> > and that is not good.
> >
> >
> Why not? :
>
> Not to lobby the concept of syntax-tables as such, but
>
> "The syntax table is relevant only for a buffer.
> This has nothing to do with any particular buffer."
>
> AFAIU just the syntax of the current buffer will be used
> - where is the problem?
>
> A white space is a white space is a white space ...
Except when the syntax-table says otherwise. In the speedbar,
for example (, ), { ,[ etc are whitespace.
--
Nick http://www.inet.net.nz/~nickrob
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-20 19:50 ` string-strip Nick Roberts
@ 2006-06-20 22:03 ` Kim F. Storm
2006-06-20 22:25 ` string-strip Nick Roberts
2006-06-20 22:42 ` string-strip Johan Bockgård
0 siblings, 2 replies; 35+ messages in thread
From: Kim F. Storm @ 2006-06-20 22:03 UTC (permalink / raw)
Cc: Andreas Roehler, rms, emacs-devel
Nick Roberts <nickrob@snap.net.nz> writes:
> > > (string-match
> > > "\\`[[:space:]\n]*\\(\\(?:.\\|\n\\)*?\\)[[:space:]\n]*\\'" str)
> > >
> > > I think [[:space:]] obeys the syntax table,
> > > and that is not good.
> > >
> > >
> > Why not? :
> >
> > Not to lobby the concept of syntax-tables as such, but
> >
> > "The syntax table is relevant only for a buffer.
> > This has nothing to do with any particular buffer."
> >
> > AFAIU just the syntax of the current buffer will be used
> > - where is the problem?
> >
> > A white space is a white space is a white space ...
>
> Except when the syntax-table says otherwise. In the speedbar,
> for example (, ), { ,[ etc are whitespace.
So don't use string-strip there if you want to preserve those chars.
Or ....
We now have three or four different opinions about something which
is utterly irrelevant, and diverting focus from the release!
Let me repeat my suggestion to postpone this to after the release.
--
Kim F. Storm <storm@cua.dk> http://www.cua.dk
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-20 22:03 ` string-strip Kim F. Storm
@ 2006-06-20 22:25 ` Nick Roberts
2006-06-20 22:42 ` string-strip Johan Bockgård
1 sibling, 0 replies; 35+ messages in thread
From: Nick Roberts @ 2006-06-20 22:25 UTC (permalink / raw)
Cc: Andreas Roehler, rms, emacs-devel
> We now have three or four different opinions about something which
> is utterly irrelevant, and diverting focus from the release!
>
> Let me repeat my suggestion to postpone this to after the release.
It could be postponed but I can't see that the discussion is diverting focus
from the things that are needed for the release ...like translating the
TUTORIAL into Romainian and Korean, or trying to debug "mysterious
unreproducible failures".
--
Nick http://www.inet.net.nz/~nickrob
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-20 22:03 ` string-strip Kim F. Storm
2006-06-20 22:25 ` string-strip Nick Roberts
@ 2006-06-20 22:42 ` Johan Bockgård
2006-06-21 16:47 ` string-strip Lars Hansen
1 sibling, 1 reply; 35+ messages in thread
From: Johan Bockgård @ 2006-06-20 22:42 UTC (permalink / raw)
storm@cua.dk (Kim F. Storm) writes:
> So don't use string-strip there if you want to preserve those chars.
> Or ....
Or how about if it worked like `split-string'?
(split-string STRING &optional SEPARATORS [...]
If SEPARATORS is non-nil, it should be a regular expression
matching text which separates, but is not part of, the
substrings. If nil it defaults to
`split-string-default-separators', normally "[ \f\t\n\r\v]+"
--
Johan Bockgård
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-20 22:42 ` string-strip Johan Bockgård
@ 2006-06-21 16:47 ` Lars Hansen
2006-06-22 9:15 ` string-strip Kim F. Storm
0 siblings, 1 reply; 35+ messages in thread
From: Lars Hansen @ 2006-06-21 16:47 UTC (permalink / raw)
Cc: andreas.roehler, Richard Stallman, emacs-devel
[-- Attachment #1: Type: text/plain, Size: 109 bytes --]
Johan Bockgård wrote:
>Or how about if it worked like `split-string'?
>
>
Brilliant idea. How about this?
[-- Attachment #2: subr.diff --]
[-- Type: text/x-patch, Size: 1293 bytes --]
*** /home/lh/cvsroot/emacs/lisp/subr.el 2006-06-13 12:37:24.000000000 +0200
--- subr.el 2006-06-21 18:44:11.200360589 +0200
***************
*** 2615,2620 ****
--- 2615,2642 ----
\f
;;;; Replacement in strings.
+ (defconst string-strip-default-white-space "[ \f\t\n\r\v]*"
+ "The default value of white-space for `string-strip'.
+ A regexp matching strings of white space.")
+
+ (defun string-strip (string &optional white-space)
+ "Remove leading and trailing WHITE-SPACE from STRING.
+ If WHITE-SPACE is non-nil, it should be a regular expression matching white
+ space. If nil it defaults to `string-strip-default-white-space', normally
+ \"[ \\f\\t\\n\\r\\v]*\".
+
+ Examples:
+ (string-strip \" foo \") => \"foo\"
+ (string-strip \" foo\\nbar\\n \") => \"foo\\nbar\"
+ (string-strip \" \") => \"\"
+ "
+ (let ((ws (or white-space string-strip-default-white-space)))
+ (save-match-data
+ (string-match
+ (concat "\\`\\(?:" ws "\\)\\(\\(?:.\\|\n\\)*?\\)\\(?:" ws "\\)\\'")
+ string)
+ (match-string 1 string))))
+
(defun subst-char-in-string (fromchar tochar string &optional inplace)
"Replace FROMCHAR with TOCHAR in STRING each time it occurs.
Unless optional argument INPLACE is non-nil, return a new string."
[-- Attachment #3: Type: text/plain, Size: 142 bytes --]
_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-21 16:47 ` string-strip Lars Hansen
@ 2006-06-22 9:15 ` Kim F. Storm
2006-06-22 11:21 ` string-strip Andreas Roehler
2006-06-28 11:52 ` string-strip Lars Hansen
0 siblings, 2 replies; 35+ messages in thread
From: Kim F. Storm @ 2006-06-22 9:15 UTC (permalink / raw)
Cc: andreas.roehler, emacs-devel, Richard Stallman,
Johan Bockgård
Lars Hansen <lists@soem.dk> writes:
> Johan Bockgård wrote:
>
>>Or how about if it worked like `split-string'?
> Brilliant idea. How about this?
Looks good to me.
But I think examples belong in the manual, not in the doc string.
--
Kim F. Storm <storm@cua.dk> http://www.cua.dk
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-22 9:15 ` string-strip Kim F. Storm
@ 2006-06-22 11:21 ` Andreas Roehler
2006-06-22 15:48 ` string-strip Kim F. Storm
2006-06-28 11:52 ` string-strip Lars Hansen
1 sibling, 1 reply; 35+ messages in thread
From: Andreas Roehler @ 2006-06-22 11:21 UTC (permalink / raw)
Cc: Richard Stallman, emacs-devel
Kim F. Storm schrieb:
> Lars Hansen <lists@soem.dk> writes:
>
>
>> Johan Bockgård wrote:
>>
>>
>>> Or how about if it worked like `split-string'?
>>>
>> Brilliant idea. How about this?
>>
>
> Looks good to me.
>
> But I think examples belong in the manual, not in the doc string.
>
>
With regard to emacs beginners: Why not have it in
both places?
Function docs are a very quick and easy to use tool.
Information there helps a lot.
The manual is fine but too much for the beginner, who
is always in a risk to get drowned (or drunk) with.
__
Andreas Roehler
PS.: As already mentioned: there is a gap between the
tutorial and the manual in the didactic sense (which
does not concern functioning and should not delay the
upcoming release...) Also I don't conceive the manual
as to big as such - on the contrary: the more
information available, the better.
Just propagate to have a second or third tutorial to ease
the next steps of the beginners (there are some tutorials
in the net which could be checked for that.)
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-22 11:21 ` string-strip Andreas Roehler
@ 2006-06-22 15:48 ` Kim F. Storm
0 siblings, 0 replies; 35+ messages in thread
From: Kim F. Storm @ 2006-06-22 15:48 UTC (permalink / raw)
Cc: Richard Stallman, emacs-devel
Andreas Roehler <andreas.roehler@easy-emacs.de> writes:
>> But I think examples belong in the manual, not in the doc string.
>
> With regard to emacs beginners: Why not have it in
> both places?
That may be desireable, but traditionally, we almost never include
examples in the doc strings, and for a trivial function like
string-strip in particular, I don't see the need (ever).
> The manual is fine but too much for the beginner, who
> is always in a risk to get drowned (or drunk) with.
The manual is no more than 5 keystrokes away...
> Just propagate to have a second or third tutorial to ease
> the next steps of the beginners (there are some tutorials
> in the net which could be checked for that.)
Emacs 22 includes the Emacs Lisp Introduction -- a beginner should
read that before using string-strip :-)
--
Kim F. Storm <storm@cua.dk> http://www.cua.dk
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-22 9:15 ` string-strip Kim F. Storm
2006-06-22 11:21 ` string-strip Andreas Roehler
@ 2006-06-28 11:52 ` Lars Hansen
2006-06-28 14:47 ` string-strip Juri Linkov
` (3 more replies)
1 sibling, 4 replies; 35+ messages in thread
From: Lars Hansen @ 2006-06-28 11:52 UTC (permalink / raw)
Cc: emacs-devel, Kim F. Storm
[-- Attachment #1: Type: text/plain, Size: 174 bytes --]
Kim F. Storm wrote:
>Looks good to me.
>
>But I think examples belong in the manual, not in the doc string.
>
>
It seems that there are no more objections. Can I install?
[-- Attachment #2: subr.diff --]
[-- Type: text/x-patch, Size: 1129 bytes --]
*** /home/lh/cvsroot/emacs/lisp/subr.el 2006-06-13 12:37:24.000000000 +0200
--- subr.el 2006-06-28 13:44:50.916887229 +0200
***************
*** 2615,2620 ****
--- 2615,2636 ----
\f
;;;; Replacement in strings.
+ (defconst string-strip-default-white-space "[ \f\t\n\r\v]*"
+ "The default value of white-space for `string-strip'.
+ A regexp matching strings of white space.")
+
+ (defun string-strip (string &optional white-space)
+ "Remove leading and trailing WHITE-SPACE from STRING.
+ If WHITE-SPACE is non-nil, it should be a regular expression matching white
+ space. If nil it defaults to `string-strip-default-white-space', normally
+ \"[ \\f\\t\\n\\r\\v]*\"."
+ (let ((ws (or white-space string-strip-default-white-space)))
+ (save-match-data
+ (string-match
+ (concat "\\`\\(?:" ws "\\)\\(\\(?:.\\|\n\\)*?\\)\\(?:" ws "\\)\\'")
+ string)
+ (match-string 1 string))))
+
(defun subst-char-in-string (fromchar tochar string &optional inplace)
"Replace FROMCHAR with TOCHAR in STRING each time it occurs.
Unless optional argument INPLACE is non-nil, return a new string."
[-- Attachment #3: Type: text/plain, Size: 142 bytes --]
_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-28 11:52 ` string-strip Lars Hansen
@ 2006-06-28 14:47 ` Juri Linkov
2006-06-29 13:00 ` string-strip Richard Stallman
` (2 subsequent siblings)
3 siblings, 0 replies; 35+ messages in thread
From: Juri Linkov @ 2006-06-28 14:47 UTC (permalink / raw)
Cc: storm, rms, emacs-devel
> It seems that there are no more objections. Can I install?
I don't see a need for a special variable string-strip-default-white-space
since a non-interactive function `string-strip' has the `white-space' arg.
IOW, I think it's better to move the default regexp to the function
(and allowing to override the hard-coded default with `white-space' arg).
--
Juri Linkov
http://www.jurta.org/emacs/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-28 11:52 ` string-strip Lars Hansen
2006-06-28 14:47 ` string-strip Juri Linkov
@ 2006-06-29 13:00 ` Richard Stallman
2006-07-02 16:03 ` string-strip Stefan Monnier
2006-09-29 7:41 ` string-strip Andreas Roehler
3 siblings, 0 replies; 35+ messages in thread
From: Richard Stallman @ 2006-06-29 13:00 UTC (permalink / raw)
Cc: emacs-devel, storm
Before installing any such thing, the changes for etc/NEWS and the
Lisp Manual have to be written.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-28 11:52 ` string-strip Lars Hansen
2006-06-28 14:47 ` string-strip Juri Linkov
2006-06-29 13:00 ` string-strip Richard Stallman
@ 2006-07-02 16:03 ` Stefan Monnier
2006-07-03 9:58 ` string-strip Lars Hansen
2006-09-29 7:41 ` string-strip Andreas Roehler
3 siblings, 1 reply; 35+ messages in thread
From: Stefan Monnier @ 2006-07-02 16:03 UTC (permalink / raw)
Cc: Kim F. Storm, Richard Stallman, emacs-devel
> + (save-match-data
Why?
Stefan
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-07-02 16:03 ` string-strip Stefan Monnier
@ 2006-07-03 9:58 ` Lars Hansen
2006-07-06 22:22 ` string-strip Stefan Monnier
0 siblings, 1 reply; 35+ messages in thread
From: Lars Hansen @ 2006-07-03 9:58 UTC (permalink / raw)
Cc: Kim F. Storm, Richard Stallman, emacs-devel
Stefan Monnier wrote:
>>+ (save-match-data
>>
>>
>
>Why?
>
>
The purpose of string-strip is to strip the string, nothing else.
Changing match-data is not an intended side effect, so it should be
avoided IMO.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-07-03 9:58 ` string-strip Lars Hansen
@ 2006-07-06 22:22 ` Stefan Monnier
2006-07-07 19:31 ` string-strip Richard Stallman
0 siblings, 1 reply; 35+ messages in thread
From: Stefan Monnier @ 2006-07-06 22:22 UTC (permalink / raw)
Cc: emacs-devel, Richard Stallman, Kim F. Storm
>>> + (save-match-data
>> Why?
> The purpose of string-strip is to strip the string, nothing else.
> Changing match-data is not an intended side effect, so it should be
> avoided IMO.
Given that it takes a regexp as argument, I'd say that it *is* expected to
mess the match data.
Stefan
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-07-06 22:22 ` string-strip Stefan Monnier
@ 2006-07-07 19:31 ` Richard Stallman
2006-07-08 3:59 ` string-strip Stefan Monnier
0 siblings, 1 reply; 35+ messages in thread
From: Richard Stallman @ 2006-07-07 19:31 UTC (permalink / raw)
Cc: emacs-devel, storm
Given that it takes a regexp as argument, I'd say that it *is* expected to
mess the match data.
It isn't useful for the function to provide anything in the match
data. And not saving the match data creates a pitfall that people
won't generally expect. Therefore, it should save the match data.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-07-07 19:31 ` string-strip Richard Stallman
@ 2006-07-08 3:59 ` Stefan Monnier
2006-07-08 20:57 ` string-strip Richard Stallman
0 siblings, 1 reply; 35+ messages in thread
From: Stefan Monnier @ 2006-07-08 3:59 UTC (permalink / raw)
Cc: emacs-devel, storm
> Given that it takes a regexp as argument, I'd say that it *is* expected to
> mess the match data.
> It isn't useful for the function to provide anything in the match
> data. And not saving the match data creates a pitfall that people
> won't generally expect. Therefore, it should save the match data.
Most elisp functions don't bother to save the match data. That's because it
is costly to do and rarely useful.
Stefan
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-07-08 3:59 ` string-strip Stefan Monnier
@ 2006-07-08 20:57 ` Richard Stallman
0 siblings, 0 replies; 35+ messages in thread
From: Richard Stallman @ 2006-07-08 20:57 UTC (permalink / raw)
Cc: emacs-devel, storm
> It isn't useful for the function to provide anything in the match
> data. And not saving the match data creates a pitfall that people
> won't generally expect. Therefore, it should save the match data.
Most elisp functions don't bother to save the match data. That's because it
is costly to do and rarely useful.
Most functions serve a special purpose and don't have many callers.
This one is a general utility, so we want its interface to be clean.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-06-28 11:52 ` string-strip Lars Hansen
` (2 preceding siblings ...)
2006-07-02 16:03 ` string-strip Stefan Monnier
@ 2006-09-29 7:41 ` Andreas Roehler
2006-09-29 14:22 ` string-strip Lars Hansen
3 siblings, 1 reply; 35+ messages in thread
From: Andreas Roehler @ 2006-09-29 7:41 UTC (permalink / raw)
Cc: Juri Linkov, emacs-devel, Richard Stallman, Stefan Monnier
Lars Hansen schrieb:
> Kim F. Storm wrote:
>
>
>> Looks good to me.
>>
>> But I think examples belong in the manual, not in the doc string.
>>
>>
>>
> It seems that there are no more objections. Can I install?
>
> ------------------------------------------------------------------------
>
> *** /home/lh/cvsroot/emacs/lisp/subr.el 2006-06-13 12:37:24.000000000 +0200
> --- subr.el 2006-06-28 13:44:50.916887229 +0200
> ***************
> *** 2615,2620 ****
> --- 2615,2636 ----
> \f
> ;;;; Replacement in strings.
>
> + (defconst string-strip-default-white-space "[ \f\t\n\r\v]*"
> + "The default value of white-space for `string-strip'.
> + A regexp matching strings of white space.")
> +
> + (defun string-strip (string &optional white-space)
> + "Remove leading and trailing WHITE-SPACE from STRING.
> + If WHITE-SPACE is non-nil, it should be a regular expression matching white
> + space. If nil it defaults to `string-strip-default-white-space', normally
> + \"[ \\f\\t\\n\\r\\v]*\"."
> + (let ((ws (or white-space string-strip-default-white-space)))
> + (save-match-data
> + (string-match
> + (concat "\\`\\(?:" ws "\\)\\(\\(?:.\\|\n\\)*?\\)\\(?:" ws "\\)\\'")
> + string)
> + (match-string 1 string))))
> +
> (defun subst-char-in-string (fromchar tochar string &optional inplace)
> "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
> Unless optional argument INPLACE is non-nil, return a new string."
>
Conceive this as the result of the discussion. Will it be installed?
Thanks.
__
Andreas Roehler
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-09-29 7:41 ` string-strip Andreas Roehler
@ 2006-09-29 14:22 ` Lars Hansen
2006-09-29 18:40 ` string-strip Andreas Roehler
2006-09-29 21:45 ` string-strip Richard Stallman
0 siblings, 2 replies; 35+ messages in thread
From: Lars Hansen @ 2006-09-29 14:22 UTC (permalink / raw)
Cc: Juri Linkov, emacs-devel, Richard Stallman, Stefan Monnier
Andreas Roehler wrote:
> Conceive this as the result of the discussion. Will it be installed?
>
> Thanks.
Richard wrote:
> Before installing any such thing, the changes for etc/NEWS and the
> Lisp Manual have to be written.
This is why it was never installed. I don't have the time to write it
now. If you or someone else will do it I will be thankfull.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-09-29 14:22 ` string-strip Lars Hansen
@ 2006-09-29 18:40 ` Andreas Roehler
2006-09-29 21:45 ` string-strip Richard Stallman
1 sibling, 0 replies; 35+ messages in thread
From: Andreas Roehler @ 2006-09-29 18:40 UTC (permalink / raw)
Cc: Juri Linkov, emacs-devel, Richard Stallman, Stefan Monnier
Lars Hansen schrieb:
> Andreas Roehler wrote:
>
>> Conceive this as the result of the discussion. Will it be installed?
>>
>> Thanks.
>
> Richard wrote:
>
>> Before installing any such thing, the changes for etc/NEWS and the
>> Lisp Manual have to be written.
>
> This is why it was never installed. I don't have the time to write it
> now. If you or someone else will do it I will be thankfull.20060929-an-ed_2.txt
>
Will try my best and look for it.
Sometimes it's good if things take a rest.
Came back to that, because I had to change the
string-strip function for my needs.
The task was to pick and transform references from
textes with legal matters
81 Js 2701/96,
-> 81Js2701-96
As these references sometimes have been followed by a
commata, as shown, this should be removed too.
So I inserted a commata into the expression:
(string-match
"\\`[[:space:]\/\n]*\\(\\(?:.\\|\n\\)*?\\)[[:space:],\n]*\\'" str)
^
Other users may need other changes. What about
to make this expression a customizable user
var?
__
Andreas Roehler
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-09-29 14:22 ` string-strip Lars Hansen
2006-09-29 18:40 ` string-strip Andreas Roehler
@ 2006-09-29 21:45 ` Richard Stallman
2006-11-24 15:02 ` string-strip Andreas Roehler
1 sibling, 1 reply; 35+ messages in thread
From: Richard Stallman @ 2006-09-29 21:45 UTC (permalink / raw)
Cc: juri, andreas.roehler, emacs-devel, monnier
We could install string-strip after the release, but let's not do it now.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: string-strip
2006-09-29 21:45 ` string-strip Richard Stallman
@ 2006-11-24 15:02 ` Andreas Roehler
2006-11-25 6:58 ` string-strip Richard Stallman
0 siblings, 1 reply; 35+ messages in thread
From: Andreas Roehler @ 2006-11-24 15:02 UTC (permalink / raw)
Cc: Juri Linkov, Nick Roberts, emacs-devel, Stefan Monnier,
Kim F. Storm
Richard Stallman schrieb:
> We could install string-strip after the release, but let's not do it now.
>
What about to split the regexp into parts? Thus it's
easier to change values.
Regards
__
Andreas Roehler
;;;
;; (setq strip-chars-before "\\`[[:space:],;\/\n]*")
;; (setq strip-chars-after "[[:space:],;\n]*\\'")
;; (setq string-strip-preserve "\\(\\(?:.\\|\n\\)*?\\)")
(defcustom strip-chars-before "\\`[[:space:]\n]*"
"Regexp indicating which chars shall be stripped before STRING - which
is defined by `string-strip-preserve'"
:type 'string
:group 'convenience)
(defcustom strip-chars-after "[[:space:]\n]*\\'"
"Regexp indicating which chars shall be stripped after STRING - which
is defined by `string-strip-preserve'"
:type 'string
:group 'convenience)
(defcustom string-strip-preserve "\\(\\(?:.\\|\n\\)*?\\)"
"Regexp indicating which chars shall be constitutents of STRING
thus being preserved - whereas `strip-chars-after' and
`strip-chars-before' indicate what class of chars to strip from
beginning or end of STRING"
:type 'string
:group 'convenience)
(defun string-strip (str)
"Return a copy of STR with leading and trailing CHARS removed.
Customize `strip-chars-before' and `strip-chars-after' respectively.
Default is `[[:space:]\n]*', i.e. spaces, tabs and newlines."
(interactive)
(save-match-data
(string-match
(concat strip-chars-before string-strip-preserve strip-chars-after) str)
(match-string 1 str)))
^ permalink raw reply [flat|nested] 35+ messages in thread
* string-strip
@ 2006-06-20 10:26 Andreas Roehler
0 siblings, 0 replies; 35+ messages in thread
From: Andreas Roehler @ 2006-06-20 10:26 UTC (permalink / raw)
Cc: Richard Stallman
Looks great. Thanks.
Here with this example included:
(defun string-strip (str)
"Return a copy of STR with leading and trailing white space removed.
\(string-strip \" foo \") => \"foo\"
\(string-strip \" \\nfoo\\n \") => \"foo\"
\(string-strip \" foo\\nbar \") => \"foo\\nbar\"
\(string-strip \" \") => \"\"
\(string-strip \" \\n \") => \"\"
"
(save-match-data
(string-match
"\\`[[:space:]\n]*\\(\\(?:.\\|\n\\)*?\\)[[:space:]\n]*\\'" str)
(match-string 1 str)))
^ permalink raw reply [flat|nested] 35+ messages in thread
end of thread, other threads:[~2006-11-25 6:58 UTC | newest]
Thread overview: 35+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-06-16 8:07 string-strip Andreas Roehler
2006-06-19 7:42 ` string-strip Richard Stallman
2006-06-19 17:59 ` string-strip Lars Hansen
2006-06-19 18:24 ` string-strip Stuart D. Herring
2006-06-19 20:20 ` string-strip Lars Hansen
2006-06-19 20:23 ` string-strip Andreas Schwab
2006-06-19 22:04 ` string-strip Kim F. Storm
2006-06-19 23:19 ` string-strip Richard Stallman
2006-06-20 6:34 ` string-strip Lars Hansen
2006-06-20 17:55 ` string-strip Richard Stallman
2006-06-20 19:28 ` string-strip Andreas Roehler
2006-06-20 19:50 ` string-strip Nick Roberts
2006-06-20 22:03 ` string-strip Kim F. Storm
2006-06-20 22:25 ` string-strip Nick Roberts
2006-06-20 22:42 ` string-strip Johan Bockgård
2006-06-21 16:47 ` string-strip Lars Hansen
2006-06-22 9:15 ` string-strip Kim F. Storm
2006-06-22 11:21 ` string-strip Andreas Roehler
2006-06-22 15:48 ` string-strip Kim F. Storm
2006-06-28 11:52 ` string-strip Lars Hansen
2006-06-28 14:47 ` string-strip Juri Linkov
2006-06-29 13:00 ` string-strip Richard Stallman
2006-07-02 16:03 ` string-strip Stefan Monnier
2006-07-03 9:58 ` string-strip Lars Hansen
2006-07-06 22:22 ` string-strip Stefan Monnier
2006-07-07 19:31 ` string-strip Richard Stallman
2006-07-08 3:59 ` string-strip Stefan Monnier
2006-07-08 20:57 ` string-strip Richard Stallman
2006-09-29 7:41 ` string-strip Andreas Roehler
2006-09-29 14:22 ` string-strip Lars Hansen
2006-09-29 18:40 ` string-strip Andreas Roehler
2006-09-29 21:45 ` string-strip Richard Stallman
2006-11-24 15:02 ` string-strip Andreas Roehler
2006-11-25 6:58 ` string-strip Richard Stallman
-- strict thread matches above, loose matches on Subject: below --
2006-06-20 10:26 string-strip Andreas Roehler
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.