modfetch/semver.c

29 lines
691 B
C
Raw Normal View History

2024-02-10 14:11:26 +01:00
#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 sveq(semver s1, semver s2) {
2024-02-10 14:11:26 +01:00
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;
}