help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Elisp string function question


From: Jean Louis
Subject: Re: Elisp string function question
Date: Fri, 18 Jun 2021 15:28:05 +0300
User-agent: Mutt/2.0.7+183 (3d24855) (2021-05-28)

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



reply via email to

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