modfetch/semver.c

28 lines
692 B
C

#include "semver.h"
#include <stdio.h>
#include <stdlib.h>
uint8_t num_digits(uint8_t x) {
return ((x) / 100) != 0 ? 3 :
((x) / 10) != 0 ? 2 :
1;
}
bool cmpsv(semver s1, semver s2) {
return s1.major == s2.major &&
s1.minor == s2.minor &&
s1.patch == s2.patch;
}
char *svtoa(semver s) {
uint8_t major_len = num_digits(s.major);
uint8_t minor_len = num_digits(s.minor);
uint8_t patch_len = num_digits(s.patch);
// \0 dots
size_t len = (size_t)1 + 2 + major_len + minor_len + patch_len;
char *ret = malloc(len * sizeof(char));
snprintf(ret, len, "%d.%d.%d", s.major, s.minor, s.patch);
return ret;
}