Module gversion

Lua library for Gentoo-style versioning format

Examples:

local ver = require "gversion"

-- Parse version
local pkgver = ver.parse("2.1_rc3-r1")
local pkgver = ver"2.1_rc3-r1"

-- Access version components
pkgver[1]     --> "2"
pkgver.major  --> "2"
pkgver[2]     --> "1"
pkgver.minor  --> "1"
pkgver.rc     --> "3"
pkgver.r      --> "1"

-- Change version components
pkgver[1] = "2"     -- 2.1_rc3-r1
pkgver.minor = "0"  -- 2.0_rc3-r1
pkgver.rc = nil     -- 2.0-r1

-- Compare versions
ver"1.5" == ver"1.005"           --> true
ver"1.2_rc1" < ver"1.2b"         --> true
ver"1.2_beta_pre" > ver"1.2_p1"  --> false

-- Normalize version; try to convert it into our versioning format
ver.normalize("2_1-beta3")  --> "2.1_beta3"
ver.normalize("2.1b3")      --> "2.1_beta3"

See https://devmanual.gentoo.org/ebuild-writing/file-format/#file-naming-rules for specification of the versioning format.

Info:

Functions

set_suffixes (pre_release, post_release) Sets pre-release and post-release suffixes.
compare (a, b) Compares two versions.
normalize (version) Tries to convert the given version into a Gentoo-style versioning format.
parse (str) Parses given str and returns Version, if str is a valid version.
__call () An alias for parse.

Fields

_VERSION Version of this module.


Functions

set_suffixes (pre_release, post_release)
Sets pre-release and post-release suffixes.

Default pre-release suffixes are: alpha, beta, pre, rc. Default post-release suffixes are: p.

Suffix must not be r or suffix, these have special meaning!

Parameters:

compare (a, b)
Compares two versions.

Parameters:

  • a Version
  • b Version

Returns:

    number -1 if a < b, 0 if a == b, or 1 if a > b
normalize (version)
Tries to convert the given version into a Gentoo-style versioning format.

The conversion consists of:

  • converting all letters to lowercase (1)
  • removing all whitespaces (2)
  • removing prefix "v" and "r" (3)
  • replacing "_" and "-" between each two numbers with "." (4)
  • inserting "_" between a digit and a known suffix (5.1)
  • replacing "." and "-" between a digit and a known suffix with "_" (5.1)
  • replacing "a", ".a", and "-a" between two numbers with "_alpha" (5.2)
  • replacing "b", ".b", and "-b" between two numbers with "_beta" (5.2)

Note: There's no guarantee that the result will really be a valid version number! Always pass it to parse for validation.

Parameters:

Returns:

    string

Raises:

Error when version is not a string.
parse (str)
Parses given str and returns Version, if str is a valid version.

Parameters:

  • str string The string to parse.

Returns:

    Version A parsed version.

Or

  1. nil
  2. string An error message if version is malformed.

Raises:

Error when str is not a string.
__call ()
An alias for parse.

Fields

_VERSION
Version of this module.
generated by LDoc 1.4.5