forked from vieter-v/vieter
wip parse range
parent
7490668b44
commit
6d7e50bb25
|
|
@ -1,4 +1,4 @@
|
||||||
*.c
|
vieter.c
|
||||||
/data/
|
/data/
|
||||||
|
|
||||||
# Build artifacts
|
# Build artifacts
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue