From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alex Dunn Newsgroups: gmane.emacs.bugs Subject: bug#22046: [PATCH] Improve version-to-list parsing Date: Tue, 01 Dec 2015 20:14:29 -0800 Message-ID: References: <83y4dgma7k.fsf@gnu.org> <83r3j7mqpr.fsf@gnu.org> <83k2oyltx2.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1449029724 16939 80.91.229.3 (2 Dec 2015 04:15:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 2 Dec 2015 04:15:24 +0000 (UTC) Cc: 22046@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Dec 02 05:15:14 2015 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1a3yoz-0001Ad-OV for geb-bug-gnu-emacs@m.gmane.org; Wed, 02 Dec 2015 05:15:14 +0100 Original-Received: from localhost ([::1]:56043 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a3yoy-0000Zn-Pf for geb-bug-gnu-emacs@m.gmane.org; Tue, 01 Dec 2015 23:15:12 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41548) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a3yot-0000Xr-P0 for bug-gnu-emacs@gnu.org; Tue, 01 Dec 2015 23:15:09 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a3yop-0007hA-Kg for bug-gnu-emacs@gnu.org; Tue, 01 Dec 2015 23:15:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:45841) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a3yop-0007h6-HN for bug-gnu-emacs@gnu.org; Tue, 01 Dec 2015 23:15:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1a3yoo-0003Hu-UU for bug-gnu-emacs@gnu.org; Tue, 01 Dec 2015 23:15:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Alex Dunn Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 02 Dec 2015 04:15:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 22046 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 22046-submit@debbugs.gnu.org id=B22046.144902969612608 (code B ref 22046); Wed, 02 Dec 2015 04:15:02 +0000 Original-Received: (at 22046) by debbugs.gnu.org; 2 Dec 2015 04:14:56 +0000 Original-Received: from localhost ([127.0.0.1]:35549 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a3yoh-0003HH-6X for submit@debbugs.gnu.org; Tue, 01 Dec 2015 23:14:56 -0500 Original-Received: from mail-pa0-f53.google.com ([209.85.220.53]:34727) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a3yoL-0003Go-Qd for 22046@debbugs.gnu.org; Tue, 01 Dec 2015 23:14:53 -0500 Original-Received: by padhx2 with SMTP id hx2so27370787pad.1 for <22046@debbugs.gnu.org>; Tue, 01 Dec 2015 20:14:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:in-reply-to:references:user-agent:date :message-id:mime-version:content-type; bh=lw4chsK1y/Yl1WCK3RdcxOjOtRJtUzUVg6ncsNvVZUg=; b=pvT0sBhHIZVUKPmIjNasuk0Fj/5AWhqT9gncQ1xofCrtrSLz3yAoHtY6CoGNxOs1Ji 2aenYgog50OF5ULejIt2RgPKGQDKJM0ROb/cEdZOwFuHvkZqi5jOaDq/VqStSO9FJjsl IoIjkN8VBArBhCC2j1ELDocm1KHTbTuXixu4AayETumaQi+K0UfAG5VxMsPCMw3PTcLI griQ11iQr8f1LbOJdEq5ZLrAu64OTZK7X1axj65v+eRfSA/oSe+kjKdIv/v62+2P7T10 OYLHOo9PiqcHgdk5w/rwkux/c281ipY89ur2X5BD8GvY96LroW4ZMwgxAs77vYrDNYKm aOIw== X-Received: by 10.98.11.133 with SMTP id 5mr1622066pfl.26.1449029672822; Tue, 01 Dec 2015 20:14:32 -0800 (PST) Original-Received: from localhost (pool-98-108-240-213.snloca.dsl-w.verizon.net. [98.108.240.213]) by smtp.gmail.com with ESMTPSA id 24sm912704pfr.27.2015.12.01.20.14.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Dec 2015 20:14:32 -0800 (PST) In-Reply-To: <83k2oyltx2.fsf@gnu.org> User-Agent: Notmuch/0.21 (http://notmuchmail.org) Emacs/25.0.50.1 (x86_64-apple-darwin15.0.0) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 208.118.235.43 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.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:109520 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable OK, that=E2=80=99s fair. Attaching a new patch and changelog. Thanks! --=-=-= Content-Type: text/plain Content-Disposition: inline; filename=ChangeLog 2015-12-01 Alex Dunn Improve version-to-list parsing * lisp/subr.el (version-regexp-alist): allow "." as priority separator * lisp/subr.el (version-to-list): more helpful error messages * lisp/subr.el (version-to-list): ".5" is valid (update docstring) * lisp/subr.el (version-to-list): make "22.8X3" invalid * test/lisp/subr-tests.el (ert-test-version-parsing): parsing tests --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Improve-version-to-list-parsing.patch >From e3a5c8f2a4b1020f2381f352048db988ad039feb Mon Sep 17 00:00:00 2001 From: Alex Dunn Date: Fri, 27 Nov 2015 15:39:24 -0800 Subject: [PATCH] Improve version-to-list parsing * lisp/subr.el (version-regexp-alist): allow "." as priority separator * lisp/subr.el (version-to-list): more helpful error messages * lisp/subr.el (version-to-list): ".5" is valid (update docstring) * lisp/subr.el (version-to-list): make "22.8X3" invalid * test/lisp/subr-tests.el (ert-test-version-parsing): parsing tests --- lisp/subr.el | 55 +++++++++++++----------- test/lisp/subr-tests.el | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 24 deletions(-) diff --git a/lisp/subr.el b/lisp/subr.el index ea926ae..a72d925 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -4686,14 +4686,14 @@ version-separator (defconst version-regexp-alist - '(("^[-_+ ]?snapshot$" . -4) + '(("^[-\._+ ]?snapshot$" . -4) ;; treat "1.2.3-20050920" and "1.2-3" as snapshot releases - ("^[-_+]$" . -4) + ("^[-\._+]$" . -4) ;; treat "1.2.3-CVS" as snapshot release - ("^[-_+ ]?\\(cvs\\|git\\|bzr\\|svn\\|hg\\|darcs\\)$" . -4) - ("^[-_+ ]?alpha$" . -3) - ("^[-_+ ]?beta$" . -2) - ("^[-_+ ]?\\(pre\\|rc\\)$" . -1)) + ("^[-\._+ ]?\\(cvs\\|git\\|bzr\\|svn\\|hg\\|darcs\\)$" . -4) + ("^[-\._+ ]?alpha$" . -3) + ("^[-\._+ ]?beta$" . -2) + ("^[-\._+ ]?\\(pre\\|rc\\)$" . -1)) "Specify association between non-numeric version and its priority. This association is used to handle version string like \"1.0pre2\", @@ -4703,6 +4703,7 @@ version-regexp-alist String Version Integer List Version \"0.9snapshot\" (0 9 -4) \"1.0-git\" (1 0 -4) + \"1.0.cvs\" (1 0 -4) \"1.0pre2\" (1 0 -1 2) \"1.0PRE2\" (1 0 -1 2) \"22.8beta3\" (22 8 -2 3) @@ -4742,41 +4743,47 @@ version-to-list Examples of valid version syntax: - 1.0pre2 1.0.7.5 22.8beta3 0.9alpha1 6.9.30Beta + 1.0pre2 1.0.7.5 22.8beta3 0.9alpha1 6.9.30Beta 2.4.snapshot .5 Examples of invalid version syntax: - 1.0prepre2 1.0..7.5 22.8X3 alpha3.2 .5 + 1.0prepre2 1.0..7.5 22.8X3 alpha3.2 Examples of version conversion: Version String Version as a List of Integers - \"1.0.7.5\" (1 0 7 5) - \"1.0pre2\" (1 0 -1 2) - \"1.0PRE2\" (1 0 -1 2) - \"22.8beta3\" (22 8 -2 3) - \"22.8Beta3\" (22 8 -2 3) - \"0.9alpha1\" (0 9 -3 1) + \".5\" (0 5) + \"0.9 alpha\" (0 9 -3) \"0.9AlphA1\" (0 9 -3 1) - \"0.9alpha\" (0 9 -3) \"0.9snapshot\" (0 9 -4) \"1.0-git\" (1 0 -4) + \"1.0.7.5\" (1 0 7 5) + \"1.0.cvs\" (1 0 -4) + \"1.0PRE2\" (1 0 -1 2) + \"1.0pre2\" (1 0 -1 2) + \"22.8 Beta3\" (22 8 -2 3) + \"22.8beta3\" (22 8 -2 3) See documentation for `version-separator' and `version-regexp-alist'." - (or (and (stringp ver) (> (length ver) 0)) - (error "Invalid version string: `%s'" ver)) + (unless (stringp ver) + (error "Version must be a string")) ;; Change .x.y to 0.x.y (if (and (>= (length ver) (length version-separator)) (string-equal (substring ver 0 (length version-separator)) version-separator)) (setq ver (concat "0" ver))) + (unless (string-match-p "^[0-9]" ver) + (error "Invalid version syntax: `%s' (must start with a number)" ver)) + (save-match-data (let ((i 0) (case-fold-search t) ; ignore case in matching lst s al) + ;; Parse the version-string up to a separator until there are none left (while (and (setq s (string-match "[0-9]+" ver i)) (= s i)) - ;; handle numeric part + ;; Add the numeric part to the beginning of the version list; + ;; lst gets reversed at the end (setq lst (cons (string-to-number (substring ver i (match-end 0))) lst) i (match-end 0)) @@ -4792,15 +4799,15 @@ version-to-list (setq al (cdr al))) (cond (al (push (cdar al) lst)) - ;; Convert 22.3a to 22.3.1, 22.3b to 22.3.2, etc. - ((string-match "^[-_+ ]?\\([a-zA-Z]\\)$" s) + ;; Convert 22.3a to 22.3.1, 22.3b to 22.3.2, etc., but only if + ;; the letter is the end of the version-string, to avoid + ;; 22.8X3 being valid + ((and (string-match "^[-\._+ ]?\\([a-zA-Z]\\)$" s) + (= i (length ver))) (push (- (aref (downcase (match-string 1 s)) 0) ?a -1) lst)) (t (error "Invalid version syntax: `%s'" ver)))))) - (if (null lst) - (error "Invalid version syntax: `%s'" ver) - (nreverse lst))))) - + (nreverse lst)))) (defun version-list-< (l1 l2) "Return t if L1, a list specification of a version, is lower than L2. diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index ee8db59..3fcb7d3 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -103,5 +103,117 @@ (should (equal (macroexpand-all '(when a b c d)) '(if a (progn b c d))))) +(ert-deftest subr-test-version-parsing () + (should (equal (version-to-list ".5") '(0 5))) + (should (equal (version-to-list "0.9 alpha1") '(0 9 -3 1))) + (should (equal (version-to-list "0.9 snapshot") '(0 9 -4))) + (should (equal (version-to-list "0.9-alpha1") '(0 9 -3 1))) + (should (equal (version-to-list "0.9-snapshot") '(0 9 -4))) + (should (equal (version-to-list "0.9.snapshot") '(0 9 -4))) + (should (equal (version-to-list "0.9_snapshot") '(0 9 -4))) + (should (equal (version-to-list "0.9alpha1") '(0 9 -3 1))) + (should (equal (version-to-list "0.9snapshot") '(0 9 -4))) + (should (equal (version-to-list "1.0 git") '(1 0 -4))) + (should (equal (version-to-list "1.0 pre2") '(1 0 -1 2))) + (should (equal (version-to-list "1.0-git") '(1 0 -4))) + (should (equal (version-to-list "1.0-pre2") '(1 0 -1 2))) + (should (equal (version-to-list "1.0.1-a") '(1 0 1 1))) + (should (equal (version-to-list "1.0.1-f") '(1 0 1 6))) + (should (equal (version-to-list "1.0.1.a") '(1 0 1 1))) + (should (equal (version-to-list "1.0.1.f") '(1 0 1 6))) + (should (equal (version-to-list "1.0.1_a") '(1 0 1 1))) + (should (equal (version-to-list "1.0.1_f") '(1 0 1 6))) + (should (equal (version-to-list "1.0.1a") '(1 0 1 1))) + (should (equal (version-to-list "1.0.1f") '(1 0 1 6))) + (should (equal (version-to-list "1.0.7.5") '(1 0 7 5))) + (should (equal (version-to-list "1.0.git") '(1 0 -4))) + (should (equal (version-to-list "1.0.pre2") '(1 0 -1 2))) + (should (equal (version-to-list "1.0_git") '(1 0 -4))) + (should (equal (version-to-list "1.0_pre2") '(1 0 -1 2))) + (should (equal (version-to-list "1.0git") '(1 0 -4))) + (should (equal (version-to-list "1.0pre2") '(1 0 -1 2))) + (should (equal (version-to-list "22.8 beta3") '(22 8 -2 3))) + (should (equal (version-to-list "22.8-beta3") '(22 8 -2 3))) + (should (equal (version-to-list "22.8.beta3") '(22 8 -2 3))) + (should (equal (version-to-list "22.8_beta3") '(22 8 -2 3))) + (should (equal (version-to-list "22.8beta3") '(22 8 -2 3))) + (should (equal (version-to-list "6.9.30 Beta") '(6 9 30 -2))) + (should (equal (version-to-list "6.9.30-Beta") '(6 9 30 -2))) + (should (equal (version-to-list "6.9.30.Beta") '(6 9 30 -2))) + (should (equal (version-to-list "6.9.30Beta") '(6 9 30 -2))) + (should (equal (version-to-list "6.9.30_Beta") '(6 9 30 -2))) + + (should (equal + (error-message-string (should-error (version-to-list "OTP-18.1.5"))) + "Invalid version syntax: `OTP-18.1.5' (must start with a number)")) + (should (equal + (error-message-string (should-error (version-to-list ""))) + "Invalid version syntax: `' (must start with a number)")) + (should (equal + (error-message-string (should-error (version-to-list "1.0..7.5"))) + "Invalid version syntax: `1.0..7.5'")) + (should (equal + (error-message-string (should-error (version-to-list "1.0prepre2"))) + "Invalid version syntax: `1.0prepre2'")) + (should (equal + (error-message-string (should-error (version-to-list "22.8X3"))) + "Invalid version syntax: `22.8X3'")) + (should (equal + (error-message-string (should-error (version-to-list "beta22.8alpha3"))) + "Invalid version syntax: `beta22.8alpha3' (must start with a number)")) + (should (equal + (error-message-string (should-error (version-to-list "honk"))) + "Invalid version syntax: `honk' (must start with a number)")) + (should (equal + (error-message-string (should-error (version-to-list 9))) + "Version must be a string")) + + (let ((version-separator "_")) + (should (equal (version-to-list "_5") '(0 5))) + (should (equal (version-to-list "0_9 alpha1") '(0 9 -3 1))) + (should (equal (version-to-list "0_9 snapshot") '(0 9 -4))) + (should (equal (version-to-list "0_9-alpha1") '(0 9 -3 1))) + (should (equal (version-to-list "0_9-snapshot") '(0 9 -4))) + (should (equal (version-to-list "0_9.alpha1") '(0 9 -3 1))) + (should (equal (version-to-list "0_9.snapshot") '(0 9 -4))) + (should (equal (version-to-list "0_9alpha1") '(0 9 -3 1))) + (should (equal (version-to-list "0_9snapshot") '(0 9 -4))) + (should (equal (version-to-list "1_0 git") '(1 0 -4))) + (should (equal (version-to-list "1_0 pre2") '(1 0 -1 2))) + (should (equal (version-to-list "1_0-git") '(1 0 -4))) + (should (equal (version-to-list "1_0.pre2") '(1 0 -1 2))) + (should (equal (version-to-list "1_0_1-a") '(1 0 1 1))) + (should (equal (version-to-list "1_0_1-f") '(1 0 1 6))) + (should (equal (version-to-list "1_0_1.a") '(1 0 1 1))) + (should (equal (version-to-list "1_0_1.f") '(1 0 1 6))) + (should (equal (version-to-list "1_0_1_a") '(1 0 1 1))) + (should (equal (version-to-list "1_0_1_f") '(1 0 1 6))) + (should (equal (version-to-list "1_0_1a") '(1 0 1 1))) + (should (equal (version-to-list "1_0_1f") '(1 0 1 6))) + (should (equal (version-to-list "1_0_7_5") '(1 0 7 5))) + (should (equal (version-to-list "1_0_git") '(1 0 -4))) + (should (equal (version-to-list "1_0pre2") '(1 0 -1 2))) + (should (equal (version-to-list "22_8 beta3") '(22 8 -2 3))) + (should (equal (version-to-list "22_8-beta3") '(22 8 -2 3))) + (should (equal (version-to-list "22_8.beta3") '(22 8 -2 3))) + (should (equal (version-to-list "22_8beta3") '(22 8 -2 3))) + (should (equal (version-to-list "6_9_30 Beta") '(6 9 30 -2))) + (should (equal (version-to-list "6_9_30-Beta") '(6 9 30 -2))) + (should (equal (version-to-list "6_9_30.Beta") '(6 9 30 -2))) + (should (equal (version-to-list "6_9_30Beta") '(6 9 30 -2))) + + (should (equal + (error-message-string (should-error (version-to-list "1_0__7_5"))) + "Invalid version syntax: `1_0__7_5'")) + (should (equal + (error-message-string (should-error (version-to-list "1_0prepre2"))) + "Invalid version syntax: `1_0prepre2'")) + (should (equal + (error-message-string (should-error (version-to-list "22.8X3"))) + "Invalid version syntax: `22.8X3'")) + (should (equal + (error-message-string (should-error (version-to-list "beta22_8alpha3"))) + "Invalid version syntax: `beta22_8alpha3' (must start with a number)")))) + (provide 'subr-tests) ;;; subr-tests.el ends here -- 2.6.3 --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Eli Zaretskii writes: > Sounds to me as a MELPA-specific problem that should be solved there, > not in Emacs. > >> But it=E2=80=99s true that with this change some very long strings will = be >> parsed as valid. This returns '(0 9), which is sort of ridiculous: >>=20 >> (version-to-list "It=E2=80=99s true that with this change some very long= strings will be parsed as valid: 0.9") >>=20 >> But I guess I=E2=80=99m not sure what the danger is in letting that happ= en. Is >> version-to-list often used to parse arbitrary strings, where false >> positives would cause problems? > > I just don't see a reason for such a radical change in behavior of a > feature that has been very stable lately. > > Thanks. --=-=-=--