emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string-strip


From: Stuart D. Herring
Subject: Re: string-strip
Date: Mon, 19 Jun 2006 11:24:30 -0700 (PDT)
User-agent: SquirrelMail/1.4.3a-11.EL3

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]