wip parse range

Jef Roosens 2023-01-12 14:01:18 +01:00
parent 7490668b44
commit 6d7e50bb25
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 61 additions and 1 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
*.c
vieter.c
/data/
# Build artifacts

View File

@ -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;
}