commons 0.1.5
Header-only C++23 library of common/shared types for the C++ libraries
Loading...
Searching...
No Matches
semver.hpp File Reference

A value type for a Semantic Versioning 2.0.0 version — major.minor.patch with optional prerelease and build metadata. More...

#include <commons/types.hpp>
#include <algorithm>
#include <array>
#include <compare>
#include <format>
#include <functional>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
Include dependency graph for semver.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  comms::SemVer
 A semantic version: major.minor.patch with optional prerelease/build. More...
 
struct  std::formatter< comms::SemVer >
 Formats comms::SemVer as its canonical version string. Takes no format spec. More...
 
struct  std::hash< comms::SemVer >
 Hash consistent with operator==: it combines major/minor/patch and the prerelease string, and (like equality) excludes build metadata. More...
 

Functions

std::string comms::to_string (const SemVer &v)
 SemVer as its canonical version string.
 

Detailed Description

A value type for a Semantic Versioning 2.0.0 version — major.minor.patch with optional prerelease and build metadata.

comms::SemVer holds the three numeric components plus the textual prerelease (the part after -, before +) and build (the part after +). Because those are std::string members it is a plain runtime value type — not a constexpr literal type like Color/Icon — so parsing and comparison are ordinary runtime functions and there is no UDL literal.

SemVer::parse is non-throwing (mirrors Icon::parse) and applies the full spec where it matters:

  • §11 prerelease precedence. A version with no prerelease outranks one that has a prerelease; otherwise dot-separated identifiers are compared left to right — numerically when both are all-digit (numeric identifiers carry no leading zeros, so the shorter string is the smaller number), a numeric identifier ranks below an alphanumeric one, and otherwise the comparison is ASCII-lexical; a longer run of equal leading identifiers outranks a shorter one (alpha < alpha.1).
  • §10 build metadata. Parsed and validated, but ignored by ordering and equality (so 1.0.0+a == 1.0.0+b); it is still preserved by to_string.

As a convenience the numeric core is parsed leniently: "1" and "1.2" fill the missing components with 0. Prerelease/build identifiers are still validated per spec (dot-separated, each non-empty and [0-9A-Za-z-], numeric prerelease identifiers without leading zeros); any violation yields std::nullopt.

Serialization (in commons/json.hpp, gated by COMMONS_WITH_NLOHMANN_JSON): SemVer travels as its canonical version string.

Text output (always available): to_string, operator<<, and std::format all emit the canonical version string.