Function: version-to-list

version-to-list is a byte-compiled function defined in subr.el.gz.

Signature

(version-to-list VER)

Documentation

Convert version string VER into a list of integers.

The version syntax is given by the following EBNF:

   VERSION ::= NUMBER ( SEPARATOR NUMBER )*.

   NUMBER ::= (0|1|2|3|4|5|6|7|8|9)+.

   SEPARATOR ::= version-separator (which see)
| version-regexp-alist (which see).

The NUMBER part is optional if SEPARATOR is a match for an element in version-regexp-alist.

Examples of valid version syntax:

   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

Examples of version conversion:

   Version String Version as a List of Integers
   ".5" (0 5)
   "0.9 alpha" (0 9 -3)
   "0.9AlphA1" (0 9 -3 1)
   "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.

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun version-to-list (ver)
  "Convert version string VER into a list of integers.

The version syntax is given by the following EBNF:

   VERSION ::= NUMBER ( SEPARATOR NUMBER )*.

   NUMBER ::= (0|1|2|3|4|5|6|7|8|9)+.

   SEPARATOR ::= `version-separator' (which see)
	       | `version-regexp-alist' (which see).

The NUMBER part is optional if SEPARATOR is a match for an element
in `version-regexp-alist'.

Examples of valid version syntax:

   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

Examples of version conversion:

   Version String    Version as a List of Integers
   \".5\"              (0 5)
   \"0.9 alpha\"       (0  9 -3)
   \"0.9AlphA1\"       (0  9 -3 1)
   \"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'."
  (declare (side-effect-free t))
  (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))
        ;; 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))
	;; handle non-numeric part
	(when (and (setq s (string-match "[^0-9]+" ver i))
		   (= s i))
	  (setq s (substring ver i (match-end 0))
		i (match-end 0))
	  ;; handle alpha, beta, pre, etc. separator
	  (unless (string= s version-separator)
	    (setq al version-regexp-alist)
	    (while (and al (not (string-match (caar al) s)))
	      (setq al (cdr al)))
	    (cond (al
		   (push (cdar al) lst))
        ;; 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))))))
    (nreverse lst))))