datefmt

format unix timestamps over stdin
git clone git://git.jb55.com/datefmt
Log | Files | Refs | README | LICENSE

commit d21cddd34b838976ff3583f3c85d34f34346b8d6
parent 1a8d9ba01f3bb82b991fb021b95534dc4461f7dd
Author: William Casarin <jb55@jb55.com>
Date:   Fri,  9 Oct 2020 09:56:00 -0700

compress a bit

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdatefmt.c | 15+++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/datefmt.c b/datefmt.c @@ -46,6 +46,13 @@ static enum state update_state(int c, enum state state) return state; } +static void print_rest(struct parser *parser, char *charbuf, enum state *new_state) +{ + *new_state = BOUNDARY; + printf("%.*s%s", parser->n_digits, parser->digits, charbuf); + parser->n_digits = 0; +} + static enum state doaction(int c, enum state new_state, struct parser *parser) { static char timebuf[128]; @@ -66,18 +73,14 @@ static enum state doaction(int c, enum state new_state, struct parser *parser) strftime(timebuf, sizeof(timebuf), parser->format, localtime(&ts)); printf("%s%s", timebuf, charbuf); } else { - new_state = BOUNDARY; - printf("%.*s%s", parser->n_digits, parser->digits, charbuf); - parser->n_digits = 0; + print_rest(parser, charbuf, &new_state); } } else if (new_state == MIDDLE) { if (parser->n_digits < 10) { parser->digits[parser->n_digits++] = (char)c; parser->digits[parser->n_digits] = 0; } else { - new_state = BOUNDARY; - printf("%.*s%s", parser->n_digits, parser->digits, charbuf); - parser->n_digits = 0; + print_rest(parser, charbuf, &new_state); } } else { if (c != -1)