- Notifications
You must be signed in to change notification settings - Fork31
h2non/semver.c
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Semantic version v2.0 parser and render written inANSI C with zero dependencies.
- Standard compliant (otherwise, open an issue)
- Version metadata parsing
- Version prerelease parsing
- Version comparison helpers
- Supports comparison operators
- Version render
- Version bump
- Version sanitizer
- 100% test coverage
- No regexp (ANSI C doesn't support it)
- Numeric conversion for sorting/filtering
Basic comparison:
#include<stdio.h>#include<semver.h>charcurrent[]="1.5.10";charcompare[]="2.3.0";intmain(intargc,char*argv[]) {semver_tcurrent_version= {};semver_tcompare_version= {};if (semver_parse(current,¤t_version)||semver_parse(compare,&compare_version)) {fprintf(stderr,"Invalid semver string\n");return-1; }intresolution=semver_compare(compare_version,current_version);if (resolution==0) {printf("Versions %s is equal to: %s\n",compare,current); }elseif (resolution==-1) {printf("Version %s is lower than: %s\n",compare,current); }else {printf("Version %s is higher than: %s\n",compare,current); }// Free allocated memory when we're donesemver_free(¤t_version);semver_free(&compare_version);return0;}
Satisfies version:
#include<stdio.h>#include<semver.h>semver_tcurrent= {};semver_tcompare= {};intmain(intargc,char*argv[]) {semver_parse("1.3.10",¤t);semver_parse("1.5.2",&compare);// Use caret operator for the comparisoncharoperator[]="^";if (semver_satisfies(current,compare,operator)) {printf("Version %s can be satisfied by %s","1.3.10","1.5.2"); }// Free allocated memory when we're donesemver_free(¤t);semver_free(&compare);return0;}
Clone this repository:
$ git clone https://github.com/h2non/semver.c
Or install withclib:
$ clib install h2non/semver.c
semver base struct.
Parses a string as semver expression.
Returns:
-1
- In case of invalid semver or parsing error.0
- All was fine!
Compare versionsa
withb
.
Returns:
-1
in case of lower version.0
in case of equal versions.1
in case of higher version.
Checks if both versions can be satisfiedbased on the given comparison operator.
Allowed operators:
=
- Equality>=
- Higher or equal to<=
- Lower or equal to<
- Lower than>
- Higher than^
- Caret operator comparison (more info)~
- Tilde operator comparison (more info)
Returns:
1
- Can be satisfied0
- Cannot be satisfied
Checks if versionx
can be satisfied byy
performing a comparison with caret operator.
See:https://docs.npmjs.com/misc/semver#caret-ranges-1-2-3-0-2-5-0-0-4
Returns:
1
- Can be satisfied0
- Cannot be satisfied
Checks if versionx
can be satisfied byy
performing a comparison with tilde operator.
See:https://docs.npmjs.com/misc/semver#tilde-ranges-1-2-3-1-2-1
Returns:
1
- Can be satisfied0
- Cannot be satisfied
Equality comparison.
Non equal comparison.
Greater than comparison.
Lower than comparison.
Greater than or equal comparison.
Lower than or equal comparison.
Render as string.
Render as numeric value. Useful for ordering and filtering.
Bump major version.
Bump minor version.
Bump patch version.
Helper to free allocated memory from heap.
Checks if the given string is a valid semver expression.
Removes invalid semver characters in a given string.
MIT - Tomas Aparicio
About
Semantic version in ANSI C