struct SoftwareVersion

Overview

The SoftwareVersion type represents a version number.

An instance can be created from a version string which consists of a series of segments separated by periods. Each segment contains one ore more alpanumerical ASCII characters. The first segment is expected to contain only digits.

There may be one instance of a dash (-) which denotes the version as a pre-release. It is otherwise equivalent to a period.

Optional version metadata may be attached and is separated by a plus character (+). All content following a + is considered metadata.

This format is described by the regular expression: /[0-9]+(?>\.[0-9a-zA-Z]+)*(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-.]+)?/

This implementation is compatible to popular versioning schemes such as SemVer and CalVer but doesn't enforce any particular one.

It behaves mostly equivalent to Gem::Version from rubygems.

Sort order

This wrapper type is mostly important for properly sorting version numbers, because generic lexical sorting doesn't work: For instance, 3.10 is supposed to be greater than 3.2.

Every set of consecutive digits anywhere in the string are interpreted as a decimal number and numerically sorted. Letters are lexically sorted. Periods (and dash) delimit numbers but don't affect sort order by themselves. Thus 1.0a is considered equal to 1.0.a.

Pre-release

If a version number contains a letter (a-z) then that version is considered a pre-release. Pre-releases sort lower than the rest of the version prior to the first letter (or dash). For instance 1.0-b compares lower than 1.0 but greater than 1.0-a.

Included Modules

Defined in:

util/software_version.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(version : Number) #

Constructs a Version from the string representation of version number.


[View source]
def self.parse(string : String) : self #

Parses an instance from a string.

A version string is a series of digits or ASCII letters separated by dots.

Raises ArgumentError if string describes an invalid version (see .valid?).


[View source]

Class Method Detail

def self.compare(a : String, b : String) #

[View source]
def self.parse?(string : String) : self | Nil #

Parses an instance from a string.

A version string is a series of digits or ASCII letters separated by dots.

Returns nil if string describes an invalid version (see .valid?).


[View source]
def self.valid?(string : String) : Bool #

Returns true if string is a valid version format.


[View source]

Instance Method Detail

def <=>(other : self) #

Compares this version with other returning -1, 0, or 1 if the other version is larger, the same, or smaller than this one.


[View source]
def hash(hasher) #

Custom hash implementation which produces the same hash for a and b when a <=> b == 0


[View source]
def matches_pessimistic_version_constraint?(constraint : String) #

[View source]
def metadata : String | Nil #

Returns the metadata attached to this version or nil if no metadata available.

SoftwareVersion.new("1.0.0").metadata            # => nil
SoftwareVersion.new("1.0.0-rc1").metadata        # => nil
SoftwareVersion.new("1.0.0+build1").metadata     # => "build1"
SoftwareVersion.new("1.0.0-rc1+build1").metadata # => "build1"

[View source]
def prerelease? : Bool #

Returns true if this version is a pre-release version.

A version is considered pre-release if it contains an ASCII letter or -.

SoftwareVersion.new("1.0.0").prerelease?     # => false
SoftwareVersion.new("1.0.0-dev").prerelease? # => true
SoftwareVersion.new("1.0.0-1").prerelease?   # => true
SoftwareVersion.new("1.0.0a1").prerelease?   # => true

[View source]
def release : self #

Returns version representing the release version associated with this version.

If this version is a pre-release (see #prerelease?) a new instance will be created with the same version string before the first ASCII letter or -.

Version metadata (see #metadata) will be stripped.

SoftwareVersion.new("1.0.0").release        # => SoftwareVersion.new("1.0.0")
SoftwareVersion.new("1.0.0-dev").release    # => SoftwareVersion.new("1.0.0")
SoftwareVersion.new("1.0.0-1").release      # => SoftwareVersion.new("1.0.0")
SoftwareVersion.new("1.0.0a1").release      # => SoftwareVersion.new("1.0.0")
SoftwareVersion.new("1.0.0+b1").release     # => SoftwareVersion.new("1.0.0")
SoftwareVersion.new("1.0.0-rc1+b1").release # => SoftwareVersion.new("1.0.0")

[View source]
def to_s(io : IO) #

Appends the string representation of this version to io.


[View source]
def to_s : String #

Returns the string representation of this version.


[View source]