Skip to content

ddn.util.semver

Semantic Versioning (SemVer) implementation according to semver.org.

Module Declaration

module ddn.util.semver;

Classes

SemVerParseException

class SemVerParseException : Exception

Exception thrown when a version string is invalid.

Constructor:

this(string msg);
Parameter Description
msg The error message describing the parse error

Structs

SemVer

struct SemVer

Represents a Semantic Version according to semver.org.

Provides parsing, comparison, and stringification.

Fields

Field Type Description
major uint Major version (non-negative integer)
minor uint Minor version (non-negative integer)
patch uint Patch version (non-negative integer)
prerelease string[] Pre-release identifiers (if any), in order
build string[] Build metadata identifiers (if any), in order

Constructor

this(uint major, uint minor, uint patch, string[] prerelease = [], string[] build = []);

Constructs a SemVer from its components.

Parameter Description
major Major version
minor Minor version
patch Patch version
prerelease Pre-release identifiers (optional)
build Build metadata identifiers (optional)

Example:

auto v1 = SemVer(2, 0, 0);                           // 2.0.0
auto v2 = SemVer(1, 0, 0, ["beta"]);                 // 1.0.0-beta
auto v3 = SemVer(1, 0, 0, ["rc", "1"], ["build"]);   // 1.0.0-rc.1+build

Methods

parse
static SemVer parse(string verstr);

Parses a version string into a SemVer object.

Parameter Description
verstr The version string to parse

Returns: A SemVer object representing the parsed version.

Throws: SemVerParseException if the string is not a valid semantic version.

Example:

auto v = SemVer.parse("1.2.3-alpha.1+build.5");
assert(v.major == 1 && v.minor == 2 && v.patch == 3);
assert(v.prerelease == ["alpha", "1"]);
assert(v.build == ["build", "5"]);

toString
string toString() const;

Converts this SemVer to its canonical string representation.

Returns: The version string in format MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD].

Example:

auto v = SemVer(1, 2, 3, ["alpha", "1"], ["build", "5"]);
assert(v.toString() == "1.2.3-alpha.1+build.5");

isPrerelease
bool isPrerelease() const;

Returns true if this version is a pre-release.

Returns: true if the version has pre-release identifiers.

Example:

assert(SemVer(1, 0, 0, ["alpha"]).isPrerelease());
assert(!SemVer(1, 0, 0).isPrerelease());

hasBuild
bool hasBuild() const;

Returns true if this version has build metadata.

Returns: true if the version has build metadata identifiers.

Example:

assert(SemVer(1, 0, 0, [], ["build"]).hasBuild());
assert(!SemVer(1, 0, 0).hasBuild());

compare
int compare(const SemVer other) const;

Compares this SemVer to another according to semver precedence rules.

Build metadata is ignored for precedence.

Parameter Description
other The SemVer to compare against

Returns: -1 if this < other, 0 if equal, 1 if this > other.

Example:

assert(SemVer.parse("1.0.0").compare(SemVer.parse("2.0.0")) < 0);
assert(SemVer.parse("1.0.0-alpha").compare(SemVer.parse("1.0.0")) < 0);
assert(SemVer.parse("1.0.0+build.1").compare(SemVer.parse("1.0.0+build.2")) == 0);

opEquals
bool opEquals(const SemVer other) const;

Equality operator. Ignores build metadata.

Returns: true if versions are equal (excluding build metadata).

Example:

assert(SemVer.parse("1.2.3-alpha") == SemVer.parse("1.2.3-alpha+build.1"));
assert(SemVer.parse("1.2.3") != SemVer.parse("1.2.3-alpha"));

opCmp
int opCmp(const SemVer other) const;

Less-than operator, for sorting. Ignores build metadata.

Returns: Comparison result for use with D's comparison operators.

Example:

assert(SemVer.parse("1.0.0-alpha") < SemVer.parse("1.0.0"));
assert(SemVer.parse("1.0.0") > SemVer.parse("1.0.0-alpha"));

bumpMajor
SemVer bumpMajor() const;

Returns a new SemVer with incremented major version, resetting minor and patch to 0, and clearing prerelease/build.

Returns: A new SemVer with bumped major version.

Example:

auto v = SemVer.parse("1.2.3-alpha+build.1").bumpMajor();
assert(v == SemVer(2, 0, 0));

bumpMinor
SemVer bumpMinor() const;

Returns a new SemVer with incremented minor version, resetting patch to 0, and clearing prerelease/build.

Returns: A new SemVer with bumped minor version.

Example:

auto v = SemVer.parse("1.2.3-alpha+build.1").bumpMinor();
assert(v == SemVer(1, 3, 0));

bumpPatch
SemVer bumpPatch() const;

Returns a new SemVer with incremented patch version, clearing prerelease/build.

Returns: A new SemVer with bumped patch version.

Example:

auto v = SemVer.parse("1.2.3-alpha+build.1").bumpPatch();
assert(v == SemVer(1, 2, 4));

withPrerelease
SemVer withPrerelease(string[] ids) const;

Returns a new SemVer with the given prerelease identifiers.

Parameter Description
ids The prerelease identifiers

Returns: A new SemVer with the specified prerelease identifiers.

Example:

auto v = SemVer(1, 2, 3).withPrerelease(["alpha", "1"]);
assert(v.prerelease == ["alpha", "1"]);
assert(v.toString() == "1.2.3-alpha.1");

withBuild
SemVer withBuild(string[] ids) const;

Returns a new SemVer with the given build metadata identifiers.

Parameter Description
ids The build metadata identifiers

Returns: A new SemVer with the specified build metadata.

Example:

auto v = SemVer(1, 2, 3).withBuild(["build", "123"]);
assert(v.build == ["build", "123"]);
assert(v.toString() == "1.2.3+build.123");

Version String Format

A valid semantic version string follows this format:

MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD]

Where:

  • MAJOR, MINOR, PATCH: Non-negative integers without leading zeros
  • PRERELEASE: Dot-separated identifiers (alphanumeric and hyphens)
  • BUILD: Dot-separated identifiers (alphanumeric and hyphens)

Examples of valid versions:

  • 1.0.0
  • 1.0.0-alpha
  • 1.0.0-alpha.1
  • 1.0.0-0.3.7
  • 1.0.0+20130313144700
  • 1.0.0-beta+exp.sha.5114f85

See Also

  • semver.org — Semantic Versioning specification