From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.help Subject: Re: How to read an integer from the minibuffer Date: Tue, 16 Nov 2021 18:18:31 +0700 Message-ID: References: <87ee7nqomk.fsf@mbork.pl> <875ysxrj0v.fsf@mbork.pl> <877dd835cp.fsf@zoho.eu> <87y25o1lv4.fsf@zoho.eu> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14756"; mail-complaints-to="usenet@ciao.gmane.io" To: Emanuel Berg , help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Nov 16 12:21:40 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 1mmwWy-0003eG-73 for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 16 Nov 2021 12:21:40 +0100 Original-Received: from localhost ([::1]:58348 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mmwWw-00066P-23 for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 16 Nov 2021 06:21:38 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:34920) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mmwUA-0003Yh-FP for help-gnu-emacs@gnu.org; Tue, 16 Nov 2021 06:18:49 -0500 Original-Received: from [2607:f8b0:4864:20::929] (port=38606 helo=mail-ua1-x929.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mmwU8-0006vK-Lt for help-gnu-emacs@gnu.org; Tue, 16 Nov 2021 06:18:46 -0500 Original-Received: by mail-ua1-x929.google.com with SMTP id w23so18612447uao.5 for ; Tue, 16 Nov 2021 03:18:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=kGJ55cVeJA5FcFAxwhXfQcI2qX4gqn7ZyIa9sKAzBvM=; b=ZKOpwHSaNOJSNCaQiY/J6HaNnmxC3p/jX/xaMe+RbVlbdQ01v7TWtSo5DDGcb1uNwX 4HL40d3xbhgVH7tOKXYwxy3UDDnhmmYXHOCOkyhzUSSsc/m2FGKH0VMt4rC6ZY4TT9Ww SrAdVKxmOkW1TSIgFOWLRNvfEfMJtaJggyuvf7N67qiYE64Wjwn1xbBmHyeoAJlkhc8Q RtK8BnOrZonV3HptBSnwlHDzDLQoDetdkWB+BCXgrGZgI5hQAy1N+dQoaR27tJqoEoi+ 0PIjX/FiG2gga+osElzEH+BlV3pLMEfEnn4/bkXvp4GughYaMmo9d186Y+H0q6D9UF3p tUIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=kGJ55cVeJA5FcFAxwhXfQcI2qX4gqn7ZyIa9sKAzBvM=; b=CbZTdpnOAGWs7ssfA+pSN5kD7LUA61me/icHYbFIRywDe3XSzv/duKpHEL6BRSqrj9 /nR/wXJ5hfzxiOt3p8HUs629rniETRdhGwBJoodxRcqcyACLs4EV9XNwR9BDaiH8LQVI xfPbr58bjIt5LZdS1gxbrynSW39kAtj10mc4RB2QLqywOOd3n5IQjNStKOQ6mmmMJTd3 6DQrRhk0rt5MErE5Ow8Xq2wT1fAUFmn56BxnYCD1GMxzN0SnoCImcvMtdfSWeoFWjWS5 mmP3tFN+B/Bm7/e5T0zzzEJGhNdJdpOY6EGVG4PaPwdEP43SnsYSOh0U92RULvocOJkA m6CA== X-Gm-Message-State: AOAM532OPD5T5CwxUH7dGyaDpvQ/e9EVwftJILDw67D4rHa5duRPDOcK gsLxs8tqBdt3hdIilj0tVBLvsvBEOPJjL4DqpZ8= X-Google-Smtp-Source: ABdhPJxaOHMyzjdBBwhbvd1UVfi6trbfQ/eU6IrE/NQsKqz0oJ7alcgmv/SBo1UEMiMigDHUZxtAnilmPJiaMjMbQhI= X-Received: by 2002:ab0:72c8:: with SMTP id g8mr9674309uap.86.1637061523651; Tue, 16 Nov 2021 03:18:43 -0800 (PST) In-Reply-To: <87y25o1lv4.fsf@zoho.eu> X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::929 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::929; envelope-from=yurivkhan@gmail.com; helo=mail-ua1-x929.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, 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:134630 Archived-At: On Tue, 16 Nov 2021 at 16:38, Emanuel Berg via Users list for the GNU Emacs text editor wrote: > (defun string-to-number-number (str) > (let ((s (string-trim str))) > (if (string-match "^[+-]\\{0,1\\}\\(0+\\|0*\\.0+\\)$" s) > 0 > (let ((num (string-to-number s))) > (when (and (not (zerop num)) > (string-match "^[+-]\\{0,1\\}\\([[:digit:]]+\\|[[:digi= t:]]*\\.[[:digit:]]+\\)$" s) ) > num) )))) If you=E2=80=99re checking the input string by regexp anyway, you could do that before calling the parser and simplify to: (defun string-to-number-strict (str) (if (string-match "^[+-]?\\([[:digit:]]+\\|[[:digit:]]*\\.[[:digit:]]+\\)$" s) (string-to-number s) (error "invalid number"))) (Depending on circumstances, you might want to refine the regexp to disallow leading zeros unless it is the only digit in the integer part; and/or allow exponential format. It might also be a good idea to wrap all that in a =E2=80=98save-match-data=E2=80=99.)