CFE-3882: Implemented date parser#284
Conversation
|
There was an error running your pipeline, see logs for details. |
|
There was an error running your pipeline, see logs for details. |
|
There was an error running your pipeline, see logs for details. |
|
There was an error running your pipeline, see logs for details. |
|
There was an error running your pipeline, see logs for details. |
Ticket: CFE-3882 Signed-off-by: Victor Moene <victor.moene@northern.tech>
|
There was an error running your pipeline, see logs for details. |
larsewi
left a comment
There was a problem hiding this comment.
Did you consider using YACC/Bison grammar?
| { | ||
| Seq *tokens = SeqNew(10, free); | ||
|
|
||
| DateToken token = {0}; |
There was a problem hiding this comment.
You should probably allocate it on the heap to begin with.
| static DateToken *NextToken(ParserState *state) | ||
| { | ||
| if (state->error || state->idx + 1 >= SeqLength(state->tokens)) | ||
| { | ||
| state->idx = SeqLength(state->tokens); | ||
| return NULL; | ||
| } | ||
| return (DateToken *) SeqAt(state->tokens, ++state->idx); | ||
|
|
||
| } |
There was a problem hiding this comment.
Could reuse function above?
| static DateToken *NextToken(ParserState *state) | |
| { | |
| if (state->error || state->idx + 1 >= SeqLength(state->tokens)) | |
| { | |
| state->idx = SeqLength(state->tokens); | |
| return NULL; | |
| } | |
| return (DateToken *) SeqAt(state->tokens, ++state->idx); | |
| } | |
| static inline DateToken *NextToken(ParserState *state) | |
| { | |
| return PeekTokenAhead(state, 1); | |
| } |
|
|
||
| } | ||
|
|
||
| static int64_t ToYear(int64_t year, size_t ndigit) |
There was a problem hiding this comment.
Would be nice to have a comment here to explain what this function is supposed to do.
| /* =========================== */ | ||
|
|
||
|
|
||
| static bool ParseInt(DateToken *token, int64_t *out) |
There was a problem hiding this comment.
token should be const?
| static bool ParseInt(DateToken *token, int64_t *out) | |
| static bool ParseInt(const DateToken *token, int64_t *out) |
|
|
||
| static bool ParseInt(DateToken *token, int64_t *out) | ||
| { | ||
| assert(token->type == DATE_TOKEN_INTEGER); |
There was a problem hiding this comment.
| assert(token->type == DATE_TOKEN_INTEGER); | |
| assert(token != NULL); | |
| assert(token->type == DATE_TOKEN_INTEGER); |
| } | ||
|
|
||
|
|
||
| static DateToken *PeekTokenAhead(ParserState *state, size_t ahead) |
There was a problem hiding this comment.
I feel like these functions should return const DateToken *. You generally don't want to modify the tokens in any way when parsing text
| #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) \ | ||
| && !defined(__NetBSD__) |
There was a problem hiding this comment.
Please add a comment explaining why this is here
| assert(t == 1783246210); | ||
| assert(StringToTimeFrom("10:10 am", &t, &mydate) == 0); | ||
| assert(t == 1783246200); | ||
| assert(StringToTimeFrom("10:10 p.m.", &t, &mydate) == 0); |
There was a problem hiding this comment.
What happens if?
| assert(StringToTimeFrom("10:10 p.m.", &t, &mydate) == 0); | |
| assert(StringToTimeFrom("10:10 p.m. am", &t, &mydate) == 0); |
Probably not the most optimal implementation, but works for most usecases. I tried followed the specs from gnu date input format and inspired myself from the behavior of
$ date --date=""when in doubt.Some comments:
@second-until-epochis not supported yet