Semantic Versioning 2.0.0 in ANSI-C.
For building and testing here, meson is required.
- compiles with c89
- no dependencies for core library parts
- supports semver 2.0.0
- support for TILDE and CARET comparisons (see also)[https://nodesource.com/blog/semver-tilde-and-caret/], e.g.
~1.3.1
includes1.3.4
up to (not including)1.4.0
(flexible patch)^1.1.0
includes1.3.4
up to (not including)2.0.0
(Caret: Flexible Minor and Patch)^0.1.0
includes0.1.4
up to but not including0.2.0
(Caret: Major Zero)^0.0.4
matches only0.0.4
(Caret: Major Zero and Minor Zero)
The library is divided into semver.h
for working with semantic version strings (e.g. "1.2.0"),
and semverreq.h
for working with requirements (e.g. ">=1.0.0 <2.0.0"). Both have convenience functions
operating on strings, as well as functions for parsing, printing and comparing into/of specialised structs.
Compare two semver strings
#include "semver.h"
#include <assert.h>
/* ... */
int res, err;
err = semver_cmp( "1.0.4", "1.1.0", &res);
assert(err == SEMVER_OK);
assert(res < 0);
Check if a semver version string matches a semver requirement
#include "semverreq.h"
#include <assert.h>
/* ... */
int res, err;
err = semver_matches( "1.5.4", ">=1.4.0 <2.0.0", &res);
assert(err == SEMVERREQ_OK);
assert(res == 1);
Use the semver_version_from_
and semver_version_req_from_
functions to construct semver and semver requirement structs
from inputs such as strings:
semver_version v;
v = semver_version_from_string("1.3.7");
semver_versionreq r;
r = semver_version_req_from_string(">=1.2.0 <2.0.0");
Checking for requirements, e.g. is v
included in the range of r
:
assert(semver_version_req_matches(r, v) == 1);
Printing:
char buf[256];
semver_version_snprint(v, buf, sizeof(buf));
/* ... */
semver_version_req_snprint(r, buf, sizeof(buf));
First time setup: cloning Unity as a submodule and initializing the meson build system:
$ git submodule update --init
$ meson setup build
To run tests:
$ meson test -C build --print-errorlogs -v