From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Emanuel Berg via Users list for the GNU Emacs text editor Newsgroups: gmane.emacs.help Subject: Re: How to read an integer from the minibuffer Date: Tue, 16 Nov 2021 14:10:24 +0100 Message-ID: <874k8c1c1r.fsf@zoho.eu> References: <87ee7nqomk.fsf@mbork.pl> <875ysxrj0v.fsf@mbork.pl> <877dd835cp.fsf@zoho.eu> <87y25o1lv4.fsf@zoho.eu> <87ilws1gcf.fsf@zoho.eu> Reply-To: Emanuel Berg Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27052"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:5rgZl69NS5zjE7Ax9md663Xxvfw= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Nov 16 14:44:10 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mmyks-0006nk-1b for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 16 Nov 2021 14:44:10 +0100 Original-Received: from localhost ([::1]:43106 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mmykq-0000x9-WC for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 16 Nov 2021 08:44:09 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:60646) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mmyER-0000jt-3y for help-gnu-emacs@gnu.org; Tue, 16 Nov 2021 08:10:39 -0500 Original-Received: from ciao.gmane.io ([116.202.254.214]:44240) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mmyEP-0007cV-8e for help-gnu-emacs@gnu.org; Tue, 16 Nov 2021 08:10:38 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1mmyEM-0004wJ-6o for help-gnu-emacs@gnu.org; Tue, 16 Nov 2021 14:10:34 +0100 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: help-gnu-emacs@gnu.org Mail-Copies-To: never Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:134635 Archived-At: 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 (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 https://dataswamp.org/~incal