Skip to content

Commit

Permalink
change: simplify trailing conditions by embedding into cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
exander77 committed Nov 7, 2024
1 parent 562a3d2 commit dc22f00
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions ccronexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit dc22f00

Please sign in to comment.