From 6d7e50bb258ff339df806d6fa6572b7395d724d8 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 12 Jan 2023 14:01:18 +0100 Subject: [PATCH] wip parse range --- .gitignore | 2 +- src/cron/expression/c/parse.c | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/cron/expression/c/parse.c diff --git a/.gitignore b/.gitignore index aaec9ef..daeb3d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -*.c +vieter.c /data/ # Build artifacts diff --git a/src/cron/expression/c/parse.c b/src/cron/expression/c/parse.c new file mode 100644 index 0000000..3f1f255 --- /dev/null +++ b/src/cron/expression/c/parse.c @@ -0,0 +1,60 @@ +#include "expression.h" + +typedef enum parse_error { + ParseOk, + ParseInvalidExpression, + ParseInvalidNumber, + ParseOutOfRange +} ParseError; + +/** + * Given a range expression, produce a bit field defining what numbers in the + * min-max range the expression represents. The first bit corresponds to min, + * the max - min + 1'th bit to max. All trailing bits after this should be + * ignored. + * + * A range expression has one of the following forms: + * + * - * + * - a + * - a-b + * - a/c + * - a-b/c + */ +ParseError ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) { + // The * expression means "every possible value" + if (s[0] == '*') { + // A '*' is only valid on its own + if (s[1] != '\0') { + return ParseInvalidExpression; + } + + *out = ~0; + + return ParseOk; + } + + uint64_t bf = 0; + + size_t slash_index = 0; + size_t dash_index = 0; + size_t i = 0; + + // We first iterate over the string to determine whether it contains a slash + // and/or a dash. We know the dash can only be valid if it appears before + // the slash. + while (s[i] != '\0' && slash_index == 0) { + if (s[i] == '/') { + slash_index = i; + + s[i] = '\0'; + } else if (s[i] == '-') { + dash_index = i; + + s[i] = '\0'; + } + } + + *out = bf; + return ParseOk; +}