From dc22f003e8323dc9acc6204067ecd2e4a8a592ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radom=C3=ADr=20Pol=C3=A1ch?= Date: Fri, 8 Nov 2024 00:15:15 +0100 Subject: [PATCH] change: simplify trailing conditions by embedding into cycle --- ccronexpr.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/ccronexpr.c b/ccronexpr.c index d87320c..edf95ac 100644 --- a/ccronexpr.c +++ b/ccronexpr.c @@ -585,28 +585,21 @@ static int do_nextprev(cron_expr* expr, struct tm* calendar, int dot, int offset static int generate_field(char *dest, uint8_t *bits, int min, int max, int offset, int buffer_len) { char buf[32]; - int first = 1, from = -1, i, bit, len = 0; - - for (i = min; i < max; i++) { - bit = cron_get_bit(bits, i + offset); - if (bit) { - if (from == -1) from = i; - } else if (from != -1) { - if (first) first = 0; else STRCATC(dest, ",", 1); - if (from == i - 1) len += sprintf(buf, "%d", from); + int first = 1, from = -1, len = 0, i, bit; + + for (i = min; i <= max; i++) { + bit = (i < max) ? cron_get_bit(bits, i + offset) : 0; + if (bit) { if (from == -1) from = i; } + else if (from != -1) { + if (!first) STRCATC(dest, ",", 1); + first = 0; + if (from == min && i - 1 == max - 1) len += sprintf(buf, "*"); + else if (from == i - 1) len += sprintf(buf, "%d", from); else len += sprintf(buf, "%d-%d", from, i - 1); STRCATC(dest, buf, 0); from = -1; } } - if (from == i - 1) { - if (!first) STRCATC(dest, ",", 1); - STRCATC(dest, buf, sprintf(buf, "%d", from)); - } else if (from == min && i == max) STRCATC(dest, "*", 1); - else if (from != -1) { - if (!first) STRCATC(dest, ",", 1); - STRCATC(dest, buf, sprintf(buf, "%d-%d", from, i - 1)); - } return len; }