[Top][All Lists]

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

Re: How to read an integer from the minibuffer

From: Emanuel Berg
Subject: Re: How to read an integer from the minibuffer
Date: Tue, 16 Nov 2021 14:10:24 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Yuri Khan wrote:

>>> It might also be a good idea to wrap all that in
>>> a ‘save-match-data’.)
>> What/how do you mean?
> Suppose we put that function in a library and document it as
> “it parses decimal numbers, checking that it is actually
> a decimal number”.
> A user tries to use the function between a (string-match …)
> and a subsequent (match-beginning), (match-end),
> (match-string) or (match-substitute-replacement). But our
> function itself uses (string-match) so we trash the user’s
> match result.

See `string-data-p':

  Same as ‘string-match’ except this function does not change
  the match data.

I also found this \\`syntax\\' - maybe that denotes the
beginning and end of the string, so one can use ^ and $ for
beginning-of-line and end-of-line only?

Both seem to work anyway ...

See the Java inspired unit testing last, well, maybe they got
it from us :P

(defun string-to-number-number (str)
  (let ((s (string-trim str)))
    (when (string-match-p
           "\\`[+-]?\\([[:digit:]]+\\|[[:digit:]]*\\.[[:digit:]]+\\)\\'" s)
      (string-to-number s) )))

(when nil
  (cl-map 'list (lambda (e)
                  (let ((a (car  e))
                        (b (cadr e)) )
                    (if (and a b)
                        (= a b)
                      (eq a b) )))
           (list (string-to-number-number " 10")                   10)
           (list (string-to-number-number "  1.5")                 1.5)
           (list (string-to-number-number " +0")                   0)
           (list (string-to-number-number "  0")                   0)
           (list (string-to-number-number " -0.0")                 -0.0)
           (list (string-to-number-number " -1.5")                -1.5)
           (list (string-to-number-number "-10")                  -10)
           (list (string-to-number-number "123this used to work")  nil)
           (list (string-to-number-number "NAN")                   nil)
           )) ; (t t t t t t t t t)

underground experts united

reply via email to

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