ddn.util.semver¶
Semantic Versioning (SemVer) implementation according to semver.org.
Module Declaration¶
Classes¶
SemVerParseException¶
Exception thrown when a version string is invalid.
Constructor:
| Parameter | Description |
|---|---|
msg |
The error message describing the parse error |
Structs¶
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¶
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¶
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¶
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¶
Returns true if this version is a pre-release.
Returns: true if the version has pre-release identifiers.
Example:
hasBuild¶
Returns true if this version has build metadata.
Returns: true if the version has build metadata identifiers.
Example:
compare¶
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¶
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¶
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¶
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:
bumpMinor¶
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:
bumpPatch¶
Returns a new SemVer with incremented patch version, clearing prerelease/build.
Returns: A new SemVer with bumped patch version.
Example:
withPrerelease¶
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¶
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:
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.01.0.0-alpha1.0.0-alpha.11.0.0-0.3.71.0.0+201303131447001.0.0-beta+exp.sha.5114f85
See Also¶
- semver.org — Semantic Versioning specification