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

A semver range constraint that answers satisfies(SemVer). More...

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

Go to the source code of this file.

Classes

class  comms::VersionConstraint
 A semver range constraint (npm ^/~, comparisons, space-separated intersection). More...
 
struct  std::formatter< comms::VersionConstraint >
 Formats comms::VersionConstraint as its raw range string. Takes no spec. More...
 
struct  std::hash< comms::VersionConstraint >
 Hash consistent with operator==: the hash of the raw range string. More...
 

Functions

std::string comms::to_string (const VersionConstraint &v)
 VersionConstraint as its raw range string.
 

Detailed Description

A semver range constraint that answers satisfies(SemVer).

comms::VersionConstraint parses an npm-flavoured range string and matches it against a comms::SemVer. Supported tokens:

  • * — any version
  • 1.2.3 — exact match
  • >=1.2.0 / >1.2.0 / <=1.2.0 / <1.2.0 — comparisons
  • !=1.2.0 — not equal
  • ^1.2.3 — caret: compatible-with (>=1.2.3 <2.0.0; for a 0.x version the range narrows, e.g. ^0.2.3<0.3.0, ^0.0.3==0.0.3)
  • ~1.2.3 — tilde: patch-level (>=1.2.3 <1.3.0)
  • >=1.0.0 <2.0.0 — intersection: space-separated tokens, all must match

Unlike SemVer::parse, VersionConstraint::parse throws std::invalid_argument on a malformed sub-version (aligning with the throwing Icon::from); an empty input is the single * (any) matcher.

VersionConstraint carries no natural ordering — only equality (by raw string) and a std::hash. The matching itself is backed by SemVer's correct §11 ordering.

Serialization (in commons/json.hpp, gated by COMMONS_WITH_NLOHMANN_JSON): a VersionConstraint travels as its raw range string.

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