From 653fbcc15642b54764b2c0bc24cebbd02b3b7361 Mon Sep 17 00:00:00 2001 From: Gabor Adam Toth Date: Fri, 20 May 2011 02:58:32 +0200 Subject: [PATCH] number, date & time parsing functions; psycString variant for isRoutingVar & getVarType --- include/psyc.h | 33 ++++++++++++++++--------- include/psyc/parse.h | 59 ++++++++++++++++++++++++++++++++++++++++++++ src/variable.c | 16 ++++++++++-- test/getVarType.c | 20 +++++++-------- test/isRoutingVar.c | 18 +++++++------- 5 files changed, 113 insertions(+), 33 deletions(-) diff --git a/include/psyc.h b/include/psyc.h index 2847bfa..abcf9d3 100644 --- a/include/psyc.h +++ b/include/psyc.h @@ -18,6 +18,7 @@ #include #include +#include #define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday) @@ -124,24 +125,22 @@ extern const psycString PSYC_routingVars[]; extern const psycMatchVar PSYC_varTypes[]; /** - * Get the type of variable name. + * Is this a routing variable name? */ -psycBool psyc_isRoutingVar(const char *name, size_t len); +psycBool psyc_isRoutingVar(psycString *name); +/** + * Is this a routing variable name? + */ +psycBool psyc_isRoutingVar2(const char *name, size_t len); /** * Get the type of variable name. */ -psycType psyc_getVarType(const char *name, size_t len); - +psycType psyc_getVarType(psycString *name); /** - * Is this a list variable name? + * Get the type of variable name. */ -static inline -psycBool psyc_isListVar(psycString *name) -{ - return name->length < 5 || memcmp(name->ptr, "_list", 5) != 0 || - (name->length > 5 && name->ptr[5] != '_') ? PSYC_FALSE : PSYC_TRUE; -} +psycType psyc_getVarType2(const char *name, size_t len); /** * Is this a list variable name? @@ -149,7 +148,17 @@ psycBool psyc_isListVar(psycString *name) static inline psycBool psyc_isListVar2(const char *name, size_t len) { - return psyc_isListVar(&(psycString){len, name}); + return len < 5 || memcmp(name, "_list", 5) != 0 || + (len > 5 && name[5] != '_') ? PSYC_FALSE : PSYC_TRUE; +} + +/** + * Is this a list variable name? + */ +static inline +psycBool psyc_isListVar(psycString *name) +{ + return psyc_isListVar2(name->ptr, name->length); } /** diff --git a/include/psyc/parse.h b/include/psyc/parse.h index 0f2332c..1b6ef6a 100644 --- a/include/psyc/parse.h +++ b/include/psyc/parse.h @@ -451,6 +451,65 @@ static inline #endif psycParseListRC psyc_parseList (psycParseListState *state, psycString *elem); +static inline +psycBool psyc_parseNumber2 (const char *value, size_t len, ssize_t *n) +{ + size_t c = 0; + uint8_t neg = 0; + + if (!value) + return PSYC_FALSE; + + if (value[0] == '-') + neg = ++c; + + *n = 0; + while (c < len && value[c] >= '0' && value[c] <= '9') + *n = 10 * *n + (value[c++] - '0'); + + if (c != len) + return PSYC_FALSE; + + if (neg) + *n = 0 - *n; + + return PSYC_TRUE; +} + +static inline +psycBool psyc_parseNumber (psycString *value, ssize_t *n) +{ + return psyc_parseNumber2(value->ptr, value->length, n); +} + +static inline +psycBool psyc_parseTime2 (const char *value, size_t len, time_t *t) +{ + return psyc_parseNumber2(value, len, t); +} + +static inline +psycBool psyc_parseTime (psycString *value, time_t *t) +{ + return psyc_parseNumber2(value->ptr, value->length, t); +} + +static inline +psycBool psyc_parseDate2 (const char *value, size_t len, time_t *t) +{ + if (psyc_parseNumber2(value, len, t)) { + *t += PSYC_EPOCH; + return PSYC_TRUE; + } + return PSYC_FALSE; +} + +static inline +psycBool psyc_parseDate (psycString *value, time_t *t) +{ + return psyc_parseDate2(value->ptr, value->length, t); +} + /** @} */ // end of parse group #define PSYC_PARSE_H diff --git a/src/variable.c b/src/variable.c index a1e4845..e9c96d7 100644 --- a/src/variable.c +++ b/src/variable.c @@ -48,7 +48,8 @@ const size_t psyc_varTypesNum = PSYC_NUM_ELEM(psyc_varTypes); /** * Get the type of variable name. */ -psycBool psyc_isRoutingVar(const char *name, size_t len) +inline +psycBool psyc_isRoutingVar2(const char *name, size_t len) { //return psyc_matchArray(psyc_routingVars, PSYC_NUM_ELEM(psyc_routingVars), name, len, 0); size_t cursor = 1; @@ -86,10 +87,16 @@ psycBool psyc_isRoutingVar(const char *name, size_t len) return matching[0] >= 0 ? PSYC_TRUE : PSYC_FALSE; } +psycBool psyc_isRoutingVar(psycString *name) +{ + return psyc_isRoutingVar2(name->ptr, name->length); +} + /** * Get the type of variable name. */ -psycType psyc_getVarType(const char *name, size_t len) +inline +psycType psyc_getVarType2(const char *name, size_t len) { //return psyc_matchArray(psyc_varTypes, PSYC_NUM_ELEM(psyc_varTypes), name, len, 1); size_t cursor = 1; @@ -129,3 +136,8 @@ psycType psyc_getVarType(const char *name, size_t len) // return first match if found return matching[0] >= 0 ? psyc_varTypes[matching[0]].value : 0; } + +psycType psyc_getVarType(psycString *name) +{ + return psyc_getVarType2(name->ptr, name->length); +} diff --git a/test/getVarType.c b/test/getVarType.c index d0fd152..de7f9e0 100644 --- a/test/getVarType.c +++ b/test/getVarType.c @@ -5,16 +5,16 @@ #include int main() { - unless (psyc_getVarType(PSYC_C2ARG("_list"))) return 1; - unless (psyc_getVarType(PSYC_C2ARG("_list_foo"))) return 2; - unless (psyc_getVarType(PSYC_C2ARG("_color_red"))) return 3; - if (psyc_getVarType(PSYC_C2ARG("_last"))) return 4; - if (psyc_getVarType(PSYC_C2ARG("_lost_foo"))) return 5; - if (psyc_getVarType(PSYC_C2ARG("_colorful"))) return 6; - if (psyc_getVarType(PSYC_C2ARG("_foo"))) return 7; - if (psyc_getVarType(PSYC_C2ARG("bar"))) return 8; - if (psyc_getVarType(PSYC_C2ARG("______"))) return 9; - if (psyc_getVarType(PSYC_C2ARG("_"))) return 10; + unless (psyc_getVarType2(PSYC_C2ARG("_list"))) return 1; + unless (psyc_getVarType2(PSYC_C2ARG("_list_foo"))) return 2; + unless (psyc_getVarType2(PSYC_C2ARG("_color_red"))) return 3; + if (psyc_getVarType2(PSYC_C2ARG("_last"))) return 4; + if (psyc_getVarType2(PSYC_C2ARG("_lost_foo"))) return 5; + if (psyc_getVarType2(PSYC_C2ARG("_colorful"))) return 6; + if (psyc_getVarType2(PSYC_C2ARG("_foo"))) return 7; + if (psyc_getVarType2(PSYC_C2ARG("bar"))) return 8; + if (psyc_getVarType2(PSYC_C2ARG("______"))) return 9; + if (psyc_getVarType2(PSYC_C2ARG("_"))) return 10; puts("psyc_getVarType passed all tests."); return 0; // passed all tests diff --git a/test/isRoutingVar.c b/test/isRoutingVar.c index 9c31685..4b6e431 100644 --- a/test/isRoutingVar.c +++ b/test/isRoutingVar.c @@ -21,17 +21,17 @@ int main() { for (i = 0; i < sizeof(vars) / sizeof(*vars); i++) { printf(">> %s: %d %d\n", vars[i], sizeof(vars[i]), sizeof(*vars[i])); - printf("%s: %d\n", vars[i], psyc_isRoutingVar(vars[i], strlen(vars[i]))); + printf("%s: %d\n", vars[i], psyc_isRoutingVar2(vars[i], strlen(vars[i]))); } #else - unless (psyc_isRoutingVar(PSYC_C2ARG("_source"))) return 1; - unless (psyc_isRoutingVar(PSYC_C2ARG("_source_relay"))) return 2; - if (psyc_isRoutingVar(PSYC_C2ARG("_source_foo"))) return 3; - if (psyc_isRoutingVar(PSYC_C2ARG("_sourcherry"))) return 4; - if (psyc_isRoutingVar(PSYC_C2ARG("_sour"))) return 5; - if (psyc_isRoutingVar(PSYC_C2ARG("_foo"))) return 6; - if (psyc_isRoutingVar(PSYC_C2ARG("bar"))) return 7; - if (psyc_isRoutingVar(PSYC_C2ARG("_"))) return 8; + unless (psyc_isRoutingVar2(PSYC_C2ARG("_source"))) return 1; + unless (psyc_isRoutingVar2(PSYC_C2ARG("_source_relay"))) return 2; + if (psyc_isRoutingVar2(PSYC_C2ARG("_source_foo"))) return 3; + if (psyc_isRoutingVar2(PSYC_C2ARG("_sourcherry"))) return 4; + if (psyc_isRoutingVar2(PSYC_C2ARG("_sour"))) return 5; + if (psyc_isRoutingVar2(PSYC_C2ARG("_foo"))) return 6; + if (psyc_isRoutingVar2(PSYC_C2ARG("bar"))) return 7; + if (psyc_isRoutingVar2(PSYC_C2ARG("_"))) return 8; puts("psyc_isRoutingVar passed all tests."); #endif