Install semver.c
masterYou can install semver.c by cloning the repository directly or using the clib package manager.
Via Git:
$ git clone https://github.com/h2non/semver.cVia clib:
$ clib install h2non/semver.crepository·master·Indexed 41 days ago
https://github.com/h2non/semver.cA lightweight, zero-dependency, standard-compliant Semantic Versioning (v2.0) parser and renderer written in ANSI C. It provides tools for parsing strings into semver_t structures, comparing versions via semver_compare, and checking range satisfaction using operators like caret (^) and tilde (~). Includes utility functions for bumping versions, rendering to strings, and memory management via semver_free.
You can install semver.c by cloning the repository directly or using the clib package manager.
Via Git:
$ git clone https://github.com/h2non/semver.cVia clib:
$ clib install h2non/semver.cTo compare two versions, parse them into semver_t structures using semver_parse and then use semver_compare.
semver_compare returns:
-1 if the first version is lower than the second.0 if the versions are equal.1 if the first version is higher than the second.Note: Always call semver_free on your semver_t objects to release allocated memory.
#include <stdio.h>
#include <semver.h>
char current[] = "1.5.10";
char compare[] = "2.3.0";
int
main(int argc, char *argv[]) {
semver_t current_version = {};
semver_t compare_version = {};
if (semver_parse(current, ¤t_version)
|| semver_parse(compare, &compare_version)) {
fprintf(stderr,"Invalid semver string\n");
return -1;
}
int resolution = semver_compare(compare_version, current_version);
if (resolution == 0) {
printf("Versions %s is equal to: %s\n", compare, current);
}
else if (resolution == -1) {
printf("Version %s is lower than: %s\n", compare, current);
}
else {
printf("Version %s is higher than: %s\n", compare, current);
}
semver_free(¤t_version);
semver_free(&compare_version);
return 0;
}Use semver_satisfies to check if a version matches a specific range defined by an operator.
Supported operators:
= (Equality)>= (Higher or equal to)<= (Lower or equal to)< (Lower than)> (Higher than)^ (Caret operator)~ (Tilde operator)semver_satisfies returns 1 if the condition is met and 0 otherwise.
#include <stdio.h>
#include <semver.h>
semver_t current = {};
semver_t compare = {};
int
main(int argc, char *argv[]) {
semver_parse("1.3.10", ¤t);
semver_parse("1.5.2", &compare);
// Use caret operator for the comparison
char operator[] = "^";
if (semver_satisfies(current, compare, operator)) {
printf("Version %s can be satisfied by %s", "1.3.10", "1.5.2");
}
semver_free(¤t);
semver_free(&compare);
return 0;
}The semver_parse function converts a string into a semver_t structure.
Signature:
int semver_parse(const char *str, semver_t *ver)
Returns:
0 on success.-1 if the string is an invalid semver expression or a parsing error occurs.The library provides several shorthand functions for direct boolean comparisons between two semver_t versions:
semver_eq(semver_t a, semver_t b): Equalitysemver_ne(semver_t a, semver_t b): Non-equalsemver_gt(semver_t a, semver_t b): Greater thansemver_lt(semver_t a, semver_t b): Lower thansemver_gte(semver_t a, semver_t b): Greater than or equalsemver_lte(semver_t a, semver_t b): Lower than or equalThe library provides optimized functions for common range checks:
semver_satisfies_caret(semver_t a, semver_t b): Checks if version a is satisfied by b using the caret (^) operator logic.semver_satisfies_patch(semver_t a, semver_t b): Checks if version a is satisfied by b using the tilde (~) operator logic.Functions for converting versions to strings, numeric values, or bumping version numbers:
semver_render(semver_t *v, char *dest): Renders the version into the provided dest string.semver_numeric(semver_t *v): Returns a numeric representation of the version, useful for sorting/filtering.semver_bump(semver_t *a): Increments the major version.semver_bump_minor(semver_t *a): Increments the minor version.semver_bump_patch(semver_t *a): Increments the patch version.Compares two semver_t versions.
Signature:
int semver_compare(semver_t a, semver_t b)
Returns:
-1 if a < b0 if a == b1 if a > bChecks if version a satisfies the range defined by version b using a specific operator.
Signature:
int semver_satisfies(semver_t a, semver_t b, char *operator)
Allowed operators:
= (Equality)>= (Higher or equal to)<= (Lower or equal to)< (Lower than)> (Higher than)^ (Caret operator)~ (Tilde operator)Returns:
1 if satisfied.0 if not satisfied.Utility functions for cleaning up and validating semver data:
semver_free(semver_t *a): Frees heap-allocated memory (like prerelease or metadata) within the semver_t struct. Must be called to avoid leaks.semver_is_valid(char *str): Returns non-zero if the string is a valid semver expression.semver_clean(char *str): Removes invalid semver characters from a string in-place.The semver_t struct is the core data type used to represent a semantic version.
Fields:
int majorint minorint patchchar * prereleasechar * metadata