Files
Library/include/Void/Text/Semver.hpp
faumaray 3851aeb703
CI / GCC 16 / C++26 / werror (push) Failing after 3m9s
CI / GCC 16 / C++26 / sanitizers (push) Failing after 10m15s
CI / GCC 16 / C++26 / default (push) Failing after 13m1s
Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-20 01:18:53 +03:00

603 lines
16 KiB
C++

#pragma once
#include <charconv>
#include <compare>
#include <format>
#include <limits>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
namespace Void::Semver
{
namespace Detail
{
[[nodiscard]] constexpr bool is_digit(char value) noexcept
{
return value >= '0' && value <= '9';
}
[[nodiscard]] constexpr bool
is_identifier_character(char value) noexcept
{
return is_digit(value) || (value >= 'A' && value <= 'Z') ||
(value >= 'a' && value <= 'z') || value == '-';
}
[[nodiscard]] inline bool
is_numeric(std::string_view identifier) noexcept
{
if (identifier.empty()) {
return false;
}
for (const char value : identifier) {
if (!is_digit(value)) {
return false;
}
}
return true;
}
[[nodiscard]] inline std::strong_ordering
compare_identifier(std::string_view left,
std::string_view right) noexcept
{
const bool left_numeric = is_numeric(left);
const bool right_numeric = is_numeric(right);
if (left_numeric && right_numeric) {
if (left.size() != right.size()) {
return left.size() < right.size()
? std::strong_ordering::less
: std::strong_ordering::greater;
}
return left <=> right;
}
if (left_numeric != right_numeric) {
return left_numeric ? std::strong_ordering::less
: std::strong_ordering::greater;
}
return left <=> right;
}
[[nodiscard]] inline std::strong_ordering
compare_prerelease(std::string_view left,
std::string_view right) noexcept
{
std::size_t left_begin = 0;
std::size_t right_begin = 0;
for (;;) {
const auto left_end = left.find('.', left_begin);
const auto right_end = right.find('.', right_begin);
const auto left_id =
left.substr(left_begin, left_end == std::string_view::npos
? std::string_view::npos
: left_end - left_begin);
const auto right_id = right.substr(
right_begin, right_end == std::string_view::npos
? std::string_view::npos
: right_end - right_begin);
if (const auto comparison =
compare_identifier(left_id, right_id);
comparison != 0) {
return comparison;
}
const bool left_done = left_end == std::string_view::npos;
const bool right_done = right_end == std::string_view::npos;
if (left_done || right_done) {
if (left_done == right_done) {
return std::strong_ordering::equal;
}
return left_done ? std::strong_ordering::less
: std::strong_ordering::greater;
}
left_begin = left_end + 1;
right_begin = right_end + 1;
}
}
[[nodiscard]] inline bool
valid_identifiers(std::string_view value,
bool reject_numeric_leading_zero) noexcept
{
if (value.empty()) {
return false;
}
std::size_t begin = 0;
for (;;) {
const auto end = value.find('.', begin);
const auto identifier =
value.substr(begin, end == std::string_view::npos
? std::string_view::npos
: end - begin);
if (identifier.empty()) {
return false;
}
for (const char character : identifier) {
if (!is_identifier_character(character)) {
return false;
}
}
if (reject_numeric_leading_zero && identifier.size() > 1 &&
identifier.front() == '0' && is_numeric(identifier)) {
return false;
}
if (end == std::string_view::npos) {
return true;
}
begin = end + 1;
}
}
[[nodiscard]] inline bool parse_component(std::string_view value,
unsigned& output) noexcept
{
if (value.empty() || (value.size() > 1 && value.front() == '0')) {
return false;
}
unsigned long long parsed = 0;
const auto [end, error] = std::from_chars(
value.data(), value.data() + value.size(), parsed);
if (error != std::errc{} || end != value.data() + value.size() ||
parsed > std::numeric_limits<unsigned>::max()) {
return false;
}
output = static_cast<unsigned>(parsed);
return true;
}
[[nodiscard]] constexpr std::string_view
trim(std::string_view value) noexcept
{
while (!value.empty() &&
(value.front() == ' ' || value.front() == '\t' ||
value.front() == '\r' || value.front() == '\n')) {
value.remove_prefix(1);
}
while (!value.empty() &&
(value.back() == ' ' || value.back() == '\t' ||
value.back() == '\r' || value.back() == '\n')) {
value.remove_suffix(1);
}
return value;
}
} // namespace Detail
struct Version {
unsigned major{0};
unsigned minor{0};
unsigned patch{0};
std::string pre_release;
std::string build;
[[nodiscard]] std::string to_string() const
{
std::string value =
std::format("{}.{}.{}", major, minor, patch);
if (!pre_release.empty()) {
value += '-';
value += pre_release;
}
if (!build.empty()) {
value += '+';
value += build;
}
return value;
}
[[nodiscard]] std::strong_ordering
operator<=>(const Version& other) const noexcept
{
if (const auto comparison = major <=> other.major;
comparison != 0) {
return comparison;
}
if (const auto comparison = minor <=> other.minor;
comparison != 0) {
return comparison;
}
if (const auto comparison = patch <=> other.patch;
comparison != 0) {
return comparison;
}
if (pre_release.empty() != other.pre_release.empty()) {
return pre_release.empty() ? std::strong_ordering::greater
: std::strong_ordering::less;
}
if (pre_release.empty()) {
return std::strong_ordering::equal;
}
return Detail::compare_prerelease(pre_release,
other.pre_release);
}
[[nodiscard]] bool operator==(const Version& other) const noexcept
{
return major == other.major && minor == other.minor &&
patch == other.patch && pre_release == other.pre_release;
}
[[nodiscard]] bool
compatible_with(const Version& required) const noexcept
{
return major == required.major && (*this <=> required) >= 0;
}
[[nodiscard]] bool is_stable() const noexcept
{
return major >= 1 && pre_release.empty();
}
[[nodiscard]] Version bumped_major() const noexcept
{
return Version{.major = major + 1,
.minor = 0,
.patch = 0,
.pre_release = {},
.build = {}};
}
[[nodiscard]] Version bumped_minor() const noexcept
{
return Version{.major = major,
.minor = minor + 1,
.patch = 0,
.pre_release = {},
.build = {}};
}
[[nodiscard]] Version bumped_patch() const noexcept
{
return Version{.major = major,
.minor = minor,
.patch = patch + 1,
.pre_release = {},
.build = {}};
}
};
namespace Detail
{
[[nodiscard]] inline std::optional<Version>
parse_version(std::string_view value, bool allow_v_prefix)
{
if (allow_v_prefix && !value.empty() &&
(value.front() == 'v' || value.front() == 'V')) {
value.remove_prefix(1);
}
if (value.empty()) {
return std::nullopt;
}
Version version;
const auto plus = value.find('+');
if (plus != std::string_view::npos) {
if (value.find('+', plus + 1) != std::string_view::npos) {
return std::nullopt;
}
const auto metadata = value.substr(plus + 1);
if (!valid_identifiers(metadata, false)) {
return std::nullopt;
}
version.build = std::string{metadata};
value = value.substr(0, plus);
}
const auto dash = value.find('-');
if (dash != std::string_view::npos) {
const auto prerelease = value.substr(dash + 1);
if (!valid_identifiers(prerelease, true)) {
return std::nullopt;
}
version.pre_release = std::string{prerelease};
value = value.substr(0, dash);
}
const auto first_dot = value.find('.');
if (first_dot == std::string_view::npos) {
return std::nullopt;
}
const auto second_dot = value.find('.', first_dot + 1);
if (second_dot == std::string_view::npos ||
value.find('.', second_dot + 1) != std::string_view::npos) {
return std::nullopt;
}
if (!parse_component(value.substr(0, first_dot), version.major) ||
!parse_component(
value.substr(first_dot + 1, second_dot - first_dot - 1),
version.minor) ||
!parse_component(value.substr(second_dot + 1), version.patch)) {
return std::nullopt;
}
return version;
}
} // namespace Detail
/// Parse SemVer while retaining the historic optional `v` prefix.
[[nodiscard]] inline std::optional<Version> parse(std::string_view value)
{
return Detail::parse_version(value, true);
}
/// Parse exactly according to SemVer 2.0.0 (no leading `v`).
[[nodiscard]] inline std::optional<Version>
parse_strict(std::string_view value)
{
return Detail::parse_version(value, false);
}
class VersionRange
{
public:
[[nodiscard]] static std::optional<VersionRange>
parse(std::string_view expression)
{
VersionRange result;
expression = Detail::trim(expression);
if (expression.empty() || expression == "*") {
result.m_alternatives.emplace_back();
return result;
}
std::size_t clause_begin = 0;
for (;;) {
const auto separator = expression.find("||", clause_begin);
const auto clause_text = Detail::trim(expression.substr(
clause_begin, separator == std::string_view::npos
? std::string_view::npos
: separator - clause_begin));
if (clause_text.empty()) {
return std::nullopt;
}
Clause clause;
std::size_t token_begin = 0;
while (token_begin < clause_text.size()) {
while (token_begin < clause_text.size() &&
(clause_text[token_begin] == ' ' ||
clause_text[token_begin] == '\t')) {
++token_begin;
}
if (token_begin == clause_text.size()) {
break;
}
auto token_end =
clause_text.find_first_of(" \t", token_begin);
if (token_end == std::string_view::npos) {
token_end = clause_text.size();
}
if (!append_token(
clause,
clause_text.substr(token_begin,
token_end - token_begin))) {
return std::nullopt;
}
token_begin = token_end;
}
result.m_alternatives.push_back(std::move(clause));
if (separator == std::string_view::npos) {
break;
}
clause_begin = separator + 2;
}
return result;
}
[[nodiscard]] bool contains(const Version& version) const noexcept
{
for (const auto& clause : m_alternatives) {
bool matches = true;
for (const auto& comparator : clause) {
if (!comparator.matches(version)) {
matches = false;
break;
}
}
if (matches) {
return true;
}
}
return false;
}
private:
enum class Operation {
Equal,
Greater,
GreaterEqual,
Less,
LessEqual
};
struct Comparator {
Operation operation{Operation::Equal};
Version version;
[[nodiscard]] bool
matches(const Version& candidate) const noexcept
{
const auto comparison = candidate <=> version;
switch (operation) {
case Operation::Equal:
return comparison == 0;
case Operation::Greater:
return comparison > 0;
case Operation::GreaterEqual:
return comparison >= 0;
case Operation::Less:
return comparison < 0;
case Operation::LessEqual:
return comparison <= 0;
}
return false;
}
};
using Clause = std::vector<Comparator>;
std::vector<Clause> m_alternatives;
[[nodiscard]] static bool append_token(Clause& clause,
std::string_view token)
{
if (token == "*") {
return true;
}
if (token.starts_with('^') || token.starts_with('~')) {
const char kind = token.front();
token.remove_prefix(1);
const auto lower = parse_strict(token);
if (!lower) {
return false;
}
Version upper;
if (kind == '~') {
if (lower->minor ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = lower->major,
.minor = lower->minor + 1,
.patch = 0,
.pre_release = {},
.build = {}};
} else if (lower->major > 0) {
if (lower->major ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = lower->major + 1,
.minor = 0,
.patch = 0,
.pre_release = {},
.build = {}};
} else if (lower->minor > 0) {
if (lower->minor ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = 0,
.minor = lower->minor + 1,
.patch = 0,
.pre_release = {},
.build = {}};
} else {
if (lower->patch ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = 0,
.minor = 0,
.patch = lower->patch + 1,
.pre_release = {},
.build = {}};
}
clause.push_back({Operation::GreaterEqual, *lower});
clause.push_back({Operation::Less, std::move(upper)});
return true;
}
if (token.find_first_of("xX*") != std::string_view::npos) {
return append_wildcard(clause, token);
}
Operation operation = Operation::Equal;
if (token.starts_with(">=")) {
operation = Operation::GreaterEqual;
token.remove_prefix(2);
} else if (token.starts_with("<=")) {
operation = Operation::LessEqual;
token.remove_prefix(2);
} else if (token.starts_with('>')) {
operation = Operation::Greater;
token.remove_prefix(1);
} else if (token.starts_with('<')) {
operation = Operation::Less;
token.remove_prefix(1);
} else if (token.starts_with('=')) {
token.remove_prefix(1);
}
const auto version = parse_strict(token);
if (!version) {
return false;
}
clause.push_back({operation, *version});
return true;
}
[[nodiscard]] static bool append_wildcard(Clause& clause,
std::string_view token)
{
const auto first_dot = token.find('.');
if (first_dot == std::string_view::npos) {
return token == "x" || token == "X" || token == "*";
}
const auto second_dot = token.find('.', first_dot + 1);
unsigned major = 0;
if (!Detail::parse_component(token.substr(0, first_dot),
major)) {
return false;
}
const auto minor_text = token.substr(
first_dot + 1, second_dot == std::string_view::npos
? std::string_view::npos
: second_dot - first_dot - 1);
if (minor_text == "x" || minor_text == "X" ||
minor_text == "*") {
if (second_dot != std::string_view::npos) {
return false;
}
if (major == std::numeric_limits<unsigned>::max()) {
return false;
}
clause.push_back(
{Operation::GreaterEqual, Version{.major = major,
.minor = 0,
.patch = 0,
.pre_release = {},
.build = {}}});
clause.push_back(
{Operation::Less, Version{.major = major + 1,
.minor = 0,
.patch = 0,
.pre_release = {},
.build = {}}});
return true;
}
unsigned minor = 0;
if (!Detail::parse_component(minor_text, minor) ||
second_dot == std::string_view::npos) {
return false;
}
const auto patch_text = token.substr(second_dot + 1);
if (patch_text != "x" && patch_text != "X" &&
patch_text != "*") {
return false;
}
if (minor == std::numeric_limits<unsigned>::max()) {
return false;
}
clause.push_back(
{Operation::GreaterEqual, Version{.major = major,
.minor = minor,
.patch = 0,
.pre_release = {},
.build = {}}});
clause.push_back({Operation::Less, Version{.major = major,
.minor = minor + 1,
.patch = 0,
.pre_release = {},
.build = {}}});
return true;
}
};
[[nodiscard]] inline std::optional<VersionRange>
parse_range(std::string_view expression)
{
return VersionRange::parse(expression);
}
} // namespace Void::Semver