From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.bugs Subject: bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number Date: Thu, 24 Nov 2022 13:08:20 +0300 Message-ID: References: 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="9188"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.2.7+37 (a90f69b) (2022-09-02) Cc: 59531@debbugs.gnu.org To: Ramesh Nedunchezian Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Nov 24 11:33:22 2022 Return-path: Envelope-to: geb-bug-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 1oy9Xl-0002Eh-M2 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 24 Nov 2022 11:33:21 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oy9XX-0000NC-LD; Thu, 24 Nov 2022 05:33:07 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oy9XT-0000L0-TC for bug-gnu-emacs@gnu.org; Thu, 24 Nov 2022 05:33:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oy9XS-0005zg-8i for bug-gnu-emacs@gnu.org; Thu, 24 Nov 2022 05:33:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oy9XS-0006SC-4n for bug-gnu-emacs@gnu.org; Thu, 24 Nov 2022 05:33:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Jean Louis Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 24 Nov 2022 10:33:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59531 X-GNU-PR-Package: emacs Original-Received: via spool by 59531-submit@debbugs.gnu.org id=B59531.166928596624786 (code B ref 59531); Thu, 24 Nov 2022 10:33:02 +0000 Original-Received: (at 59531) by debbugs.gnu.org; 24 Nov 2022 10:32:46 +0000 Original-Received: from localhost ([127.0.0.1]:57389 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oy9XC-0006Ri-6m for submit@debbugs.gnu.org; Thu, 24 Nov 2022 05:32:46 -0500 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:39293) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oy9Wz-0006RM-DW for 59531@debbugs.gnu.org; Thu, 24 Nov 2022 05:32:44 -0500 Original-Received: from localhost ([::ffff:102.82.241.226]) (AUTH: PLAIN admin, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 00000000000F6138.00000000637F483F.00003FBB; Thu, 24 Nov 2022 03:32:30 -0700 Content-Disposition: inline In-Reply-To: X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:248845 Archived-At: * Ramesh Nedunchezian [2022-11-24 09:22]: > `string-to-number` returns ZERO if the input is not a number.   > > > This return value is not very helpful.  The choice of a number ZERO as "Not A Number" doesn't help one to distinguish between the following two cases > > (1) Input was a valid number, and it parses to number zero > > (2) Input was NOT a valid number, and it was forcibly reported as ZERO > > Consider amending `string-to-number` to throw an error (or return NIL) when the input is not parseable as a number, or providing an alternative API to validate numbers.  I am trying to parse some fields in an org table, and see if the field value is a number or not; > > If there is already an alternative to what I am trying to > accomplish, I would appreciate a recipe. I had the same problem, so I have solved it this way. (defun string-is-number-p (s) "Return number only if string is actual number, otherwise NIL." (let* ((s (string-trim s))) (cond ((seq-empty-p s) nil) ((string-match "[^0123456789\\.-]" s) nil) ((string-match "-" s 1) nil) ((numberp (string-to-number s)) (string-to-number s))))) As in my case I liketo know that string is really representing number and nothing else. (string-is-number-p "Hello") ➜ nil (string-is-number-p "") ➜ nil (string-is-number-p "0") ➜ 0 (string-is-number-p "0a") ➜ nil (string-to-number "0a") ➜ 0 (string-is-number-p "0.1") ➜ 0.1 (string-is-number-p "-2.5121212") ➜ -2.5121212 My function may not be perfect. -- Jean Take action in Free Software Foundation campaigns: https://www.fsf.org/campaigns In support of Richard M. Stallman https://stallmansupport.org/