unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Elisp string function question
       [not found] <20210618121807.xsjdbv4ouqbv4uex.ref@Ergus>
@ 2021-06-18 12:18 ` Ergus
  2021-06-18 12:28   ` Jean Louis
                     ` (5 more replies)
  0 siblings, 6 replies; 15+ messages in thread
From: Ergus @ 2021-06-18 12:18 UTC (permalink / raw)
  To: help-gnu-emacs

Hi:

I just tried and this:

(string-empty-p nil) 

returns nil. But nil is not an non-empty string. This forces to add some
extra checks when using this function.

Is this intended? 




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

* Re: Elisp string function question
  2021-06-18 12:18 ` Elisp string function question Ergus
@ 2021-06-18 12:28   ` Jean Louis
  2021-06-18 12:36   ` Eli Zaretskii
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 15+ messages in thread
From: Jean Louis @ 2021-06-18 12:28 UTC (permalink / raw)
  To: Ergus; +Cc: help-gnu-emacs

* Ergus <spacibba@aol.com> [2021-06-18 15:19]:
> Hi:
> 
> I just tried and this:
> 
> (string-empty-p nil)
> 
> returns nil. But nil is not an non-empty string. This forces to add some
> extra checks when using this function.
> 
> Is this intended?

I got this too, and it is not for all cases suitable.

(defun string-blank-if-nil (s)
  "Returns blank string for nil values"
  (if (null s) "" s))

I often use this one as `nil' I sometimes wish to convert to empty string.

(string-blank-if-nil nil) ⇒ ""

(string-blank-if-nil "1") ⇒ "1"

Maybe that idea will help.

(defun string-or-empty-string (i)
  "Returns empty string for nil"
  (let* ((type (type-of i)))
    (cond ((or (eql type 'integer)
	       (eql type 'float)) 
	   (number-to-string i))
	  ((null i) "")
	  ((eql type 'symbol) (prin1-to-string i))
	  ((eql type 'string) i))))

Or that anything should be converted:

(string-or-empty-string nil) ⇒ ""
(string-or-empty-string 1) ⇒ "1"
(string-or-empty-string "ok") ⇒ "ok"

Or better idea:

(seq-empty-p nil) ⇒ t
(seq-empty-p "") ⇒ t

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Elisp string function question
  2021-06-18 12:18 ` Elisp string function question Ergus
  2021-06-18 12:28   ` Jean Louis
@ 2021-06-18 12:36   ` Eli Zaretskii
  2021-06-18 13:32     ` Robert Pluim
  2021-06-18 14:43   ` Stefan Monnier via Users list for the GNU Emacs text editor
                     ` (3 subsequent siblings)
  5 siblings, 1 reply; 15+ messages in thread
From: Eli Zaretskii @ 2021-06-18 12:36 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Fri, 18 Jun 2021 14:18:07 +0200
> From: Ergus <spacibba@aol.com>
> 
> I just tried and this:
> 
> (string-empty-p nil) 
> 
> returns nil. But nil is not an non-empty string. This forces to add some
> extra checks when using this function.
> 
> Is this intended? 

Not clear.  string-empty-p follows the examples of string=, which
allows symbols as arguments (and uses their name).

I suggest to report a bug about this, because at the very least the
doc string should mention this.



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

* Re: Elisp string function question
  2021-06-18 12:36   ` Eli Zaretskii
@ 2021-06-18 13:32     ` Robert Pluim
  2021-06-18 13:48       ` Eli Zaretskii
  0 siblings, 1 reply; 15+ messages in thread
From: Robert Pluim @ 2021-06-18 13:32 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs

>>>>> On Fri, 18 Jun 2021 15:36:25 +0300, Eli Zaretskii <eliz@gnu.org> said:

    >> Date: Fri, 18 Jun 2021 14:18:07 +0200
    >> From: Ergus <spacibba@aol.com>
    >> 
    >> I just tried and this:
    >> 
    >> (string-empty-p nil) 
    >> 
    >> returns nil. But nil is not an non-empty string. This forces to add some
    >> extra checks when using this function.
    >> 
    >> Is this intended? 

    Eli> Not clear.  string-empty-p follows the examples of string=, which
    Eli> allows symbols as arguments (and uses their name).

Then itʼs buggy:

(let ((s (make-symbol "")))
  (string-empty-p s))
  => t

    Eli> I suggest to report a bug about this, because at the very least the
    Eli> doc string should mention this.

(stringp nil) => nil
so the answer to the question "is this the empty string" is no.

Robert
-- 



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

* Re: Elisp string function question
  2021-06-18 13:32     ` Robert Pluim
@ 2021-06-18 13:48       ` Eli Zaretskii
  2021-06-18 14:02         ` Robert Pluim
  0 siblings, 1 reply; 15+ messages in thread
From: Eli Zaretskii @ 2021-06-18 13:48 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Robert Pluim <rpluim@gmail.com>
> Cc: help-gnu-emacs@gnu.org
> Gmane-Reply-To-List: yes
> Date: Fri, 18 Jun 2021 15:32:19 +0200
> 
>     Eli> Not clear.  string-empty-p follows the examples of string=, which
>     Eli> allows symbols as arguments (and uses their name).
> 
> Then itʼs buggy:
> 
> (let ((s (make-symbol "")))
>   (string-empty-p s))
>   => t

Is it?  What is the name of the symbol created there?



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

* Re: Elisp string function question
  2021-06-18 13:48       ` Eli Zaretskii
@ 2021-06-18 14:02         ` Robert Pluim
  2021-06-18 14:23           ` Eli Zaretskii
  0 siblings, 1 reply; 15+ messages in thread
From: Robert Pluim @ 2021-06-18 14:02 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs

>>>>> On Fri, 18 Jun 2021 16:48:18 +0300, Eli Zaretskii <eliz@gnu.org> said:

    >> From: Robert Pluim <rpluim@gmail.com>
    >> Cc: help-gnu-emacs@gnu.org
    >> Gmane-Reply-To-List: yes
    >> Date: Fri, 18 Jun 2021 15:32:19 +0200
    >> 
    Eli> Not clear.  string-empty-p follows the examples of string=, which
    Eli> allows symbols as arguments (and uses their name).
    >> 
    >> Then itʼs buggy:
    >> 
    >> (let ((s (make-symbol "")))
    >> (string-empty-p s))
    >> => t

    Eli> Is it?  What is the name of the symbol created there?

The name is "", but the symbol itself has no value, so string-empty-p
should not say t

(let ((s (make-symbol "")))
  (message s))
=>
Debugger entered--Lisp error: (wrong-type-argument stringp ##)
  message(##)
  (let ((s (make-symbol ""))) (message s))
  (progn (let ((s (make-symbol ""))) (message s)))
  eval((progn (let ((s (make-symbol ""))) (message s))) t)

Compare this with:

(let ((s ""))
  (string-empty-p s) => t
  (message "string '%s'" s)) => no complaints from 'message'
  
If youʼre saying this is expected, then it should *definitely* be
documented, because for me the argument to 'string-empty-p' should be
a string.

Robert
-- 



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

* Re: Elisp string function question
  2021-06-18 14:02         ` Robert Pluim
@ 2021-06-18 14:23           ` Eli Zaretskii
  2021-06-18 14:54             ` Robert Pluim
  0 siblings, 1 reply; 15+ messages in thread
From: Eli Zaretskii @ 2021-06-18 14:23 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Robert Pluim <rpluim@gmail.com>
> Cc: help-gnu-emacs@gnu.org
> Date: Fri, 18 Jun 2021 16:02:07 +0200
> 
>     >> Then itʼs buggy:
>     >> 
>     >> (let ((s (make-symbol "")))
>     >> (string-empty-p s))
>     >> => t
> 
>     Eli> Is it?  What is the name of the symbol created there?
> 
> The name is "", but the symbol itself has no value, so string-empty-p
> should not say t

But if the documentation will say that string-empty-p accepts symbols
and uses their name, then the name of this symbol _is_ empty.

> (let ((s (make-symbol "")))
>   (message s))
> =>
> Debugger entered--Lisp error: (wrong-type-argument stringp ##)
>   message(##)
>   (let ((s (make-symbol ""))) (message s))
>   (progn (let ((s (make-symbol ""))) (message s)))
>   eval((progn (let ((s (make-symbol ""))) (message s))) t)
> 
> Compare this with:
> 
> (let ((s ""))
>   (string-empty-p s) => t
>   (message "string '%s'" s)) => no complaints from 'message'

This is because 'message' does NOT allow symbols as its first
argument.



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

* Re: Elisp string function question
  2021-06-18 12:18 ` Elisp string function question Ergus
  2021-06-18 12:28   ` Jean Louis
  2021-06-18 12:36   ` Eli Zaretskii
@ 2021-06-18 14:43   ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-06-18 17:41   ` Michael Heerdegen
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 15+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-06-18 14:43 UTC (permalink / raw)
  To: help-gnu-emacs

Ergus [2021-06-18 14:18:07] wrote:
> (string-empty-p nil) returns nil. But nil is not an non-empty string. This
> forces to add some extra checks when using this function.

BTW, I think you're much better off just not using `string-empty-p` and
just use `equal` or `string=`, which is more concise and at least
as clear.


        Stefan




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

* Re: Elisp string function question
  2021-06-18 14:23           ` Eli Zaretskii
@ 2021-06-18 14:54             ` Robert Pluim
  0 siblings, 0 replies; 15+ messages in thread
From: Robert Pluim @ 2021-06-18 14:54 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs

>>>>> On Fri, 18 Jun 2021 17:23:56 +0300, Eli Zaretskii <eliz@gnu.org> said:

    >> From: Robert Pluim <rpluim@gmail.com>
    >> Cc: help-gnu-emacs@gnu.org
    >> Date: Fri, 18 Jun 2021 16:02:07 +0200
    >> 
    >> >> Then itʼs buggy:
    >> >> 
    >> >> (let ((s (make-symbol "")))
    >> >> (string-empty-p s))
    >> >> => t
    >> 
    Eli> Is it?  What is the name of the symbol created there?
    >> 
    >> The name is "", but the symbol itself has no value, so string-empty-p
    >> should not say t

    Eli> But if the documentation will say that string-empty-p accepts symbols
    Eli> and uses their name, then the name of this symbol _is_ empty.

Yes. But it doesnʼt say that. And I find it surprising.

    >> (let ((s (make-symbol "")))
    >> (message s))
    >> =>
    >> Debugger entered--Lisp error: (wrong-type-argument stringp ##)
    >> message(##)
    >> (let ((s (make-symbol ""))) (message s))
    >> (progn (let ((s (make-symbol ""))) (message s)))
    >> eval((progn (let ((s (make-symbol ""))) (message s))) t)
    >> 
    >> Compare this with:
    >> 
    >> (let ((s ""))
    >> (string-empty-p s) => t
    >> (message "string '%s'" s)) => no complaints from 'message'

    Eli> This is because 'message' does NOT allow symbols as its first
    Eli> argument.

It was intended to illustrate the state of 's'.

Robert
-- 



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

* Re: Elisp string function question
  2021-06-18 12:18 ` Elisp string function question Ergus
                     ` (2 preceding siblings ...)
  2021-06-18 14:43   ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-06-18 17:41   ` Michael Heerdegen
  2021-06-18 21:25   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-06-19  0:24   ` Michael Heerdegen
  5 siblings, 0 replies; 15+ messages in thread
From: Michael Heerdegen @ 2021-06-18 17:41 UTC (permalink / raw)
  To: help-gnu-emacs

Ergus <spacibba@aol.com> writes:

> I just tried and this:
>
> (string-empty-p nil) returns nil. But nil is not an non-empty
> string. This forces to add some
> extra checks when using this function.
>
> Is this intended? 

As far as I remember, that question had been discussed at large not long
ago.  I don't recall if it was a bug report or in emacs-dev or here.

Michael.




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

* Re: Elisp string function question
  2021-06-18 12:18 ` Elisp string function question Ergus
                     ` (3 preceding siblings ...)
  2021-06-18 17:41   ` Michael Heerdegen
@ 2021-06-18 21:25   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-06-20  1:09     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-06-19  0:24   ` Michael Heerdegen
  5 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-06-18 21:25 UTC (permalink / raw)
  To: help-gnu-emacs

Ergus wrote:

> I just tried and this:
>
> (string-empty-p nil) returns nil. But nil is not an
> non-empty string.

nil is a lot of things, it is the boolean false, it is
a variable with a value that isn't ready to be used, it is an
empty list, and apparently and empty string as well:

  (string= "" nil)

Thanks for mentioning `string-empty-p' BTW, didn't know of
that, now I can remove my own `empty-string-p' :P

(which shows the same bahavior BTW)

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Elisp string function question
  2021-06-18 12:18 ` Elisp string function question Ergus
                     ` (4 preceding siblings ...)
  2021-06-18 21:25   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-06-19  0:24   ` Michael Heerdegen
  5 siblings, 0 replies; 15+ messages in thread
From: Michael Heerdegen @ 2021-06-19  0:24 UTC (permalink / raw)
  To: help-gnu-emacs

Ergus <spacibba@aol.com> writes:

> I just tried and this:
>
> (string-empty-p nil) returns nil. But nil is not an non-empty
> string. This forces to add some
> extra checks when using this function.
>
> Is this intended? 

BTW, I'm just curious: If this was changed to return non-nil and
somebody in the future would ask

> I just tried and this:
>
> (string-empty-p nil) returns t. But nil is not an empty
> string.

what would you respond?  This is not a trick question, because the
predicate can be used to test for an empty as well as for an non-empty
string.

And if `string-empty-p' would be changed to error for symbol arguments,
this would still require extra tests to avoid that error.  So how would
you change things for the better?

Michael.




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

* Re: Elisp string function question
  2021-06-18 21:25   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-06-20  1:09     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-06-20  1:15       ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-06-20  1:09 UTC (permalink / raw)
  To: help-gnu-emacs

>> I just tried and this:
>>
>> (string-empty-p nil) returns nil. But nil is not an
>> non-empty string.
>
> nil is a lot of things, it is the boolean false, it is
> a variable with a value that isn't ready to be used, it is
> an empty list, and apparently and empty string as well:
>
>   (string= "" nil)
>
> Thanks for mentioning `string-empty-p' BTW, didn't know of
> that, now I can remove my own `empty-string-p' :P
>
> (which shows the same bahavior BTW)

No, it doesn't, why did I ever remove it? I thought it was the
same as this but it wasn't, (string-empty-p nil) should be
t according to the way I've been using it! Christ, no wonder
nothing works all of a sudden.

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Elisp string function question
  2021-06-20  1:09     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-06-20  1:15       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-06-20  1:26         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-06-20  1:15 UTC (permalink / raw)
  To: help-gnu-emacs

>>
>> Thanks for mentioning `string-empty-p' BTW, didn't know of
>> that, now I can remove my own `empty-string-p' :P
>>
>> (which shows the same bahavior BTW)
>
> No, it doesn't, why did I ever remove it? I thought it was the
> same as this but it wasn't, (string-empty-p nil) should be
> t according to the way I've been using it! Christ, no wonder
> nothing works all of a sudden.

What I want is

(defun string-data-p (str)
  (and (stringp str)
       (not (string= str "")) ))

Is it a string?

Does it have data?

nil and "" is (here) just as useless.

But the funny thing is, my "empty-string-p" looked like
`string-empty-p', that's why I replaced the calls to that
instead. Then everything broke. So I guess it didn't look
quite like that after all...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Elisp string function question
  2021-06-20  1:15       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-06-20  1:26         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-06-20  1:26 UTC (permalink / raw)
  To: help-gnu-emacs

> What I want is
>
> (defun string-data-p (str)
>   (and (stringp str)
>        (not (string= str "")) ))
>
> Is it a string?
>
> Does it have data?

(string-data-p nil)                 ; nil
(string-data-p "")                  ; nil
(string-data-p "ready for takeoff") ; t

But that is actually much better! It is much clearer to ask if
something is ready than to ask if something isn't empty!

Now I can use `when' instead of `unless', or have it as the
first branch of `if's without an`not'!

Much better!

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2021-06-20  1:26 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20210618121807.xsjdbv4ouqbv4uex.ref@Ergus>
2021-06-18 12:18 ` Elisp string function question Ergus
2021-06-18 12:28   ` Jean Louis
2021-06-18 12:36   ` Eli Zaretskii
2021-06-18 13:32     ` Robert Pluim
2021-06-18 13:48       ` Eli Zaretskii
2021-06-18 14:02         ` Robert Pluim
2021-06-18 14:23           ` Eli Zaretskii
2021-06-18 14:54             ` Robert Pluim
2021-06-18 14:43   ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-18 17:41   ` Michael Heerdegen
2021-06-18 21:25   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20  1:09     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20  1:15       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-20  1:26         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-19  0:24   ` Michael Heerdegen

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