feat(cron): some bug fixes & formatting

Jef Roosens 2023-01-14 15:03:11 +01:00
parent 292e43944e
commit fbc18386e2
5 changed files with 266 additions and 238 deletions

View File

@ -5,6 +5,5 @@ root = true
end_of_line = lf end_of_line = lf
insert_final_newline = true insert_final_newline = true
[*.v] [*.{v,c,h}]
# vfmt wants it :(
indent_style = tab indent_style = tab

View File

@ -85,6 +85,7 @@ int ce_next(struct cron_simple_time *out, struct cron_expression *ce, struct cro
while (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) { while (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) {
month_index++; month_index++;
// TODO find out if this can happen
if (month_index == 2 * ce->month_count) { if (month_index == 2 * ce->month_count) {
return 1; return 1;
} }

View File

@ -1,8 +1,11 @@
#include "expression.h" #include "expression.h"
// Allowed value ranges for the minute, hour, day and month field
const uint8_t min[4] = {0, 0, 1, 1}; const uint8_t min[4] = {0, 0, 1, 1};
const uint8_t max[4] = {59, 23, 31, 12}; const uint8_t max[4] = {59, 23, 31, 12};
// Convert a string a uint8_t value by parsing it using atoi and checking
// whether it's contained within the given range
#define SAFE_ATOI(v,s,min,max) \ #define SAFE_ATOI(v,s,min,max) \
int _##v = atoi(s); \ int _##v = atoi(s); \
if ((_##v) == 0 && strcmp((s), "0") != 0) { \ if ((_##v) == 0 && strcmp((s), "0") != 0) { \
@ -15,11 +18,11 @@ const uint8_t max[4] = {59, 23, 31, 12};
/** /**
* Given a range expression, produce a bit field defining what numbers in the * Given a range expression, produce a bit field defining what numbers in the
* min-max range the expression represents. The first bit (starting from the * min-max range the expression represents. Bit 0 (starting from the
* right) corresponds to min, the max - min + 1'th bit to max. All trailing bits * right) corresponds to min, the bit max - min to max. All trailing bits
* after this should be ignored. The given bitfield is modified in-place, so * after this should be ignored. The given bitfield is modified in-place, so
* multiple calls of this function can be performed on the same value to create * multiple calls of this function can be performed on the same value to create
* the effect of ORing their values: * the effect of ORing their values.
* *
* A range expression has one of the following forms: * A range expression has one of the following forms:
* *
@ -65,7 +68,7 @@ enum cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_
// Parse the three possible numbers in the pattern // Parse the three possible numbers in the pattern
uint8_t start = 0; uint8_t start = 0;
uint8_t end = 0; uint8_t end = max;
uint8_t interval = 1; uint8_t interval = 1;
SAFE_ATOI(start, s, min, max); SAFE_ATOI(start, s, min, max);
@ -78,11 +81,10 @@ enum cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_
SAFE_ATOI(interval, &s[slash_index + 1], 1, max - min); SAFE_ATOI(interval, &s[slash_index + 1], 1, max - min);
} }
// Single number doesn't need to loop if (dash_index == 0 && slash_index == 0) {
if (end == 0 && slash_index == 0) {
*out |= ((uint64_t) 1) << (start - min); *out |= ((uint64_t) 1) << (start - min);
} else { } else {
for (;start <= end; start += interval) { while (start <= end) {
*out |= ((uint64_t) 1) << (start - min); *out |= ((uint64_t) 1) << (start - min);
start += interval; start += interval;
} }
@ -91,6 +93,11 @@ enum cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_
return CPEParseOk; return CPEParseOk;
} }
/*
* Given an expression part, produce a bitfield defining what numbers in the
* min-max range the part represents. A part consists of one or more range
* expressions, separated by commas.
*/
enum cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) { enum cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) {
*out = 0; *out = 0;
@ -112,28 +119,47 @@ enum cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t
return ce_parse_range(out, s, min, max); return ce_parse_range(out, s, min, max);
} }
/*
* Return how many bits are set in the bitfield, better known as popcount. I
* added my own implementation (taken from my algorithms course) as I don't want
* to be dependent on GCC-specific extensions.
*/
uint8_t uint64_t_popcount(uint64_t n) {
uint8_t c = 0;
while (n != 0) {
// This sets the least significant bit to zero (very cool)
n &= n - 1;
c++;
}
return c;
}
/*
* Convert a bitfield into an array containing the numbers in the min-max range
* it represents.
*/
uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) { uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) {
uint8_t capacity = 8; // Each bit field only has `max - min + 1` meaningful bits. All other bits
uint8_t size = 0; // should be ignored, and can be any value. By shifting the bit field back and
// forth, we set these excessive bits to zero, ensuring popcount returns the
// correct value.
uint8_t excess_bits = 64 - (max - min + 1);
bf = (bf << excess_bits) >> excess_bits;
uint8_t size = uint64_t_popcount(bf);
uint8_t *buf = malloc(size * sizeof(uint8_t));
uint8_t *buf = malloc(capacity * sizeof(uint8_t)); uint8_t i = 0, j = 0;
for (uint8_t i = 0; i <= max - min; i++) { while (j < size && i <= max - min) {
if (((uint64_t)1 << i) & bf) { if (((uint64_t)1 << i) & bf) {
// Resize buffer if needed // Resize buffer if needed
if (size == capacity) { buf[j] = min + i;
capacity *= 2; j++;
buf = realloc(buf, capacity * sizeof(uint8_t));
} }
buf[size] = min + i; i++;
size++;
}
}
// Resize buffer once more to remove any trailing unused bytes
if (size < capacity) {
buf = realloc(buf, size * sizeof(uint8_t));
} }
*out = buf; *out = buf;
@ -141,6 +167,9 @@ uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) {
return size; return size;
} }
/*
* Parse a cron expression string into a cron_expression struct.
*/
enum cron_parse_error ce_parse_expression(struct cron_expression *out, char *s) { enum cron_parse_error ce_parse_expression(struct cron_expression *out, char *s) {
// The parsing functions modify the input string in-place // The parsing functions modify the input string in-place
s = strdup(s); s = strdup(s);
@ -178,7 +207,6 @@ enum cron_parse_error ce_parse_expression(struct cron_expression *out, char *s)
// Parse final trailing part // Parse final trailing part
if (part_count < 4 && s[0] != '\0') { if (part_count < 4 && s[0] != '\0') {
// Make sure to parse the final range as well
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]); res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
if (res != CPEParseOk) { if (res != CPEParseOk) {

View File

@ -26,11 +26,11 @@ fn test_next_simple() ! {
util_test_time(exp, '2002-01-01 03:00:00', '2002-01-02 03:00:00')! util_test_time(exp, '2002-01-01 03:00:00', '2002-01-02 03:00:00')!
util_test_time(exp, '2002-01-01 04:00:00', '2002-01-02 03:00:00')! util_test_time(exp, '2002-01-01 04:00:00', '2002-01-02 03:00:00')!
/* util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')! */ util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00')!
/* // Overlap to next month */ /* // Overlap to next month */
/* util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')! */ util_test_time('0 3', '2002-11-31 04:00:00', '2002-12-01 03:00:00')!
/* // Overlap to next year */ /* // Overlap to next year */
/* util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')! */ util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')!
} }