From a27410cd2ef7558fc66842dab3454be44d816b62 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Tue, 12 Nov 2019 02:01:22 +0100 Subject: [PATCH 1/2] Add version comparison predicates for > and >= * lisp/subr.el (version-list->, version-list->=, version>) (version>=): New functions. * test/lisp/subr-tests.el (subr-test-version-list-<) (subr-test-version-list->, subr-test-version-list-=) (subr-test-version-list-<=, subr-test-version-list->=): New tests. --- lisp/subr.el | 39 +++++++++++++++++++++++++++++++++++++-- test/lisp/subr-tests.el | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/lisp/subr.el b/lisp/subr.el index c1614c2e03..0e03392561 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -5568,6 +5568,14 @@ version-list-< ;; l1 null and l2 not null ==> l2 length > l1 length (t (< 0 (version-list-not-zero l2))))) +(defun version-list-> (l1 l2) + "Return t if L1, a list specification of a version, is higher than L2. + +Note that a version specified by the list (1) is equal to (1 0), +\(1 0 0), (1 0 0 0), etc. That is, the trailing zeros are insignificant. +Also, a version given by the list (1) is higher than (1 -1), which in +turn is higher than (1 -2), which is higher than (1 -3)." + (not (version-list-<= l1 l2))) (defun version-list-= (l1 l2) "Return t if L1, a list specification of a version, is equal to L2. @@ -5589,7 +5597,6 @@ version-list-= ;; l1 null and l2 not null ==> l2 length > l1 length (t (zerop (version-list-not-zero l2))))) - (defun version-list-<= (l1 l2) "Return t if L1, a list specification of a version, is lower or equal to L2. @@ -5610,6 +5617,15 @@ version-list-<= ;; l1 null and l2 not null ==> l2 length > l1 length (t (<= 0 (version-list-not-zero l2))))) +(defun version-list->= (l1 l2) + "Return t if L1, a list specification of a version, is higher or equal to L2. + +Note that a version specified by the list (1) is equal to (1 0), +\(1 0 0), (1 0 0 0), etc. That is, the trailing zeros are insignificant. +Also, a version given by the list (1) is higher than (1 -1), which in +turn is higher than (1 -2), which is higher than (1 -3)." + (not (version-list-< l1 l2))) + (defun version-list-not-zero (lst) "Return the first non-zero element of LST, which is a list of integers. @@ -5621,7 +5637,6 @@ version-list-not-zero ;; there is no element different of zero 0)) - (defun version< (v1 v2) "Return t if version V1 is lower (older) than V2. @@ -5652,6 +5667,26 @@ version= Also, \"-GIT\", \"-CVS\" and \"-NNN\" are treated as snapshot versions." (version-list-= (version-to-list v1) (version-to-list v2))) +(defun version> (v1 v2) + "Return t if version V1 is higher than V2. + +Note that version string \"1\" is equal to \"1.0\", \"1.0.0\", \"1.0.0.0\", +etc. That is, the trailing \".0\"s are insignificant. Also, version +string \"1\" is higher (newer) than \"1pre\", which is higher than \"1beta\", +which is higher than \"1alpha\", which is higher than \"1snapshot\". +Also, \"-GIT\", \"-CVS\" and \"-NNN\" are treated as snapshot versions." + (not (version-list-<= (version-to-list v1) (version-to-list v2)))) + +(defun version>= (v1 v2) + "Return t if version V1 is higher or equal to V2. + +Note that version string \"1\" is equal to \"1.0\", \"1.0.0\", \"1.0.0.0\", +etc. That is, the trailing \".0\"s are insignificant. Also, version +string \"1\" is higher (newer) than \"1pre\", which is higher than \"1beta\", +which is higher than \"1alpha\", which is higher than \"1snapshot\". +Also, \"-GIT\", \"-CVS\" and \"-NNN\" are treated as snapshot versions." + (not (version-list-< (version-to-list v1) (version-to-list v2)))) + (defvar package--builtin-versions ;; Mostly populated by loaddefs.el via autoload-builtin-package-versions. (purecopy `((emacs . ,(version-to-list emacs-version)))) diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index b3c04cdc9a..f9cdcc4a8b 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -244,6 +244,42 @@ subr-test-version-parsing (error-message-string (should-error (version-to-list "beta22_8alpha3"))) "Invalid version syntax: `beta22_8alpha3' (must start with a number)")))) +(ert-deftest subr-test-version-list-< () + (should (version-list-< '(0) '(1))) + (should (version-list-< '(0 9) '(1 0))) + (should (version-list-< '(1 -1) '(1 0))) + (should (version-list-< '(1 -2) '(1 -1))) + (should (not (version-list-< '(1) '(0)))) + (should (not (version-list-< '(1 1) '(1 0)))) + (should (not (version-list-< '(1) '(1 0)))) + (should (not (version-list-< '(1 0) '(1 0 0))))) + +(ert-deftest subr-test-version-list-> () + (should (version-list-> '(1 0) '(0 9))) + (should (version-list-> '(1) '(1 -1))) + (should (version-list-> '(1) '(0))) + (should (version-list-> '(1 1) '(1 0))) + (should (not (version-list-> '(0) '(1)))) + (should (not (version-list-> '(1) '(1 0)))) + (should (not (version-list-> '(1 0) '(1 0 0))))) + +(ert-deftest subr-test-version-list-= () + (should (version-list-= '(1) '(1))) + (should (version-list-= '(1 0) '(1))) + (should (not (version-list-= '(0) '(1))))) + +(ert-deftest subr-test-version-list-<= () + (should (version-list-<= '(0) '(1))) + (should (version-list-<= '(1) '(1))) + (should (version-list-<= '(1 0) '(1))) + (should (not (version-list-<= '(1) '(0))))) + +(ert-deftest subr-test-version-list->= () + (should (version-list->= '(1) '(0))) + (should (version-list->= '(1) '(1))) + (should (version-list->= '(1 0) '(1))) + (should (not (version-list->= '(0) '(1))))) + (defun subr-test--backtrace-frames-with-backtrace-frame (base) "Reference implementation of `backtrace-frames'." (let ((idx 0) -- 2.20.1