Add method _parse_str for RBTREE.
NOTE: A refactorisation seems needed as all the in_str / parse_str share a lot of the same algorithms. Maybe move them into algo?
This commit is contained in:
1 parent
f01a593ff7
commit
e855f27c8c
3 files changed
+52
-3
No files matched your search
+32
-1
@@ -76,6 +76,7 @@
|
|||||||
GET_MIN(M_C(name,_min)), \
|
GET_MIN(M_C(name,_min)), \
|
||||||
GET_MAX(M_C(name,_max)), \
|
GET_MAX(M_C(name,_max)), \
|
||||||
M_IF_METHOD(GET_STR, oplist)(GET_STR(M_C(name, _get_str)),), \
|
M_IF_METHOD(GET_STR, oplist)(GET_STR(M_C(name, _get_str)),), \
|
||||||
|
M_IF_METHOD(PARSE_STR, oplist)(PARSE_STR(M_C(name, _parse_str)),), \
|
||||||
M_IF_METHOD(OUT_STR, oplist)(OUT_STR(M_C(name, _out_str)),), \
|
M_IF_METHOD(OUT_STR, oplist)(OUT_STR(M_C(name, _out_str)),), \
|
||||||
M_IF_METHOD(IN_STR, oplist)(IN_STR(M_C(name, _in_str)),), \
|
M_IF_METHOD(IN_STR, oplist)(IN_STR(M_C(name, _in_str)),), \
|
||||||
M_IF_METHOD(EQUAL, oplist)(EQUAL(M_C(name, _equal_p)),), \
|
M_IF_METHOD(EQUAL, oplist)(EQUAL(M_C(name, _equal_p)),), \
|
||||||
@@ -944,6 +945,36 @@ typedef enum {
|
|||||||
} \
|
} \
|
||||||
, /* no out_str */ ) \
|
, /* no out_str */ ) \
|
||||||
\
|
\
|
||||||
|
M_IF_METHOD(PARSE_STR, oplist)( \
|
||||||
|
static inline bool \
|
||||||
|
M_C(name, _parse_str)(tree_t rbtree, const char str[], const char **endp) \
|
||||||
|
{ \
|
||||||
|
RBTREEI_CONTRACT(rbtree); \
|
||||||
|
assert (str != NULL); \
|
||||||
|
M_C(name,_clean)(rbtree); \
|
||||||
|
bool success = false; \
|
||||||
|
int c = *str++; \
|
||||||
|
if (M_UNLIKELY (c != '[')) goto exit; \
|
||||||
|
c = *str++; \
|
||||||
|
if (M_UNLIKELY (c == ']')) { success = true; goto exit; } \
|
||||||
|
if (M_UNLIKELY (c == 0)) goto exit; \
|
||||||
|
str--; \
|
||||||
|
type item; \
|
||||||
|
M_GET_INIT oplist (item); \
|
||||||
|
do { \
|
||||||
|
bool b = M_GET_PARSE_STR oplist (item, str, &str); \
|
||||||
|
do { c = *str++; } while (isspace(c)); \
|
||||||
|
if (b == false || c == 0) goto exit; \
|
||||||
|
M_C(name, _push)(rbtree, item); \
|
||||||
|
} while (c == M_GET_SEPARATOR oplist); \
|
||||||
|
M_GET_CLEAR oplist (item); \
|
||||||
|
success = (c == ']'); \
|
||||||
|
exit: \
|
||||||
|
if (endp) *endp = str; \
|
||||||
|
return success; \
|
||||||
|
} \
|
||||||
|
, /* no parse_str */ ) \
|
||||||
|
\
|
||||||
M_IF_METHOD(IN_STR, oplist)( \
|
M_IF_METHOD(IN_STR, oplist)( \
|
||||||
static inline bool \
|
static inline bool \
|
||||||
M_C(name, _in_str)(tree_t rbtree, FILE *file) \
|
M_C(name, _in_str)(tree_t rbtree, FILE *file) \
|
||||||
@@ -961,7 +992,7 @@ typedef enum {
|
|||||||
M_GET_INIT oplist (item); \
|
M_GET_INIT oplist (item); \
|
||||||
do { \
|
do { \
|
||||||
bool b = M_GET_IN_STR oplist (item, file); \
|
bool b = M_GET_IN_STR oplist (item, file); \
|
||||||
c = fgetc(file); \
|
do { c = fgetc(file); } while (isspace(c)); \
|
||||||
if (b == false || c == EOF) break; \
|
if (b == false || c == EOF) break; \
|
||||||
M_C(name, _push)(rbtree, item); \
|
M_C(name, _push)(rbtree, item); \
|
||||||
} while (c == M_GET_SEPARATOR oplist); \
|
} while (c == M_GET_SEPARATOR oplist); \
|
||||||
|
|||||||
+1
-1
@@ -78,7 +78,7 @@ static bool my_mpz_parse_str(my_mpz_t z, const char str[], const char **endptr)
|
|||||||
z->n = 1;
|
z->n = 1;
|
||||||
char *end;
|
char *end;
|
||||||
z->ptr[0] = strtol(str, &end, 0);
|
z->ptr[0] = strtol(str, &end, 0);
|
||||||
*endptr = (const char*) end;
|
if (endptr) { *endptr = (const char*) end; }
|
||||||
return (uintptr_t) end != (uintptr_t) str;
|
return (uintptr_t) end != (uintptr_t) str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-1
@@ -31,10 +31,19 @@ static bool uint_in_str(unsigned int *u, FILE *f)
|
|||||||
return n == 1;
|
return n == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool uint_parse_str(unsigned int *u, const char str[], const char **endptr)
|
||||||
|
{
|
||||||
|
char *end;
|
||||||
|
*u = strtol(str, &end, 0);
|
||||||
|
if (endptr) { *endptr = (const char*) end; }
|
||||||
|
return (uintptr_t) end != (uintptr_t) str;
|
||||||
|
}
|
||||||
|
|
||||||
static void uint_out_str(FILE *f, unsigned int u)
|
static void uint_out_str(FILE *f, unsigned int u)
|
||||||
{
|
{
|
||||||
fprintf(f, "%u", u);
|
fprintf(f, "%u", u);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void uint_get_str(string_t str, unsigned int u, bool append)
|
static void uint_get_str(string_t str, unsigned int u, bool append)
|
||||||
{
|
{
|
||||||
(append ? string_cat_printf : string_printf) (str, "%u", u);
|
(append ? string_cat_printf : string_printf) (str, "%u", u);
|
||||||
@@ -42,7 +51,7 @@ static void uint_get_str(string_t str, unsigned int u, bool append)
|
|||||||
|
|
||||||
#include "coverage.h"
|
#include "coverage.h"
|
||||||
START_COVERAGE
|
START_COVERAGE
|
||||||
RBTREE_DEF(rbtree_uint, unsigned int, M_OPEXTEND(M_DEFAULT_OPLIST, IN_STR(uint_in_str M_IPTR), OUT_STR(uint_out_str), GET_STR(uint_get_str)) )
|
RBTREE_DEF(rbtree_uint, unsigned int, M_OPEXTEND(M_DEFAULT_OPLIST, IN_STR(uint_in_str M_IPTR), OUT_STR(uint_out_str), GET_STR(uint_get_str), PARSE_STR(uint_parse_str M_IPTR)) )
|
||||||
END_COVERAGE
|
END_COVERAGE
|
||||||
|
|
||||||
RBTREE_DEF(rbtree_float, float)
|
RBTREE_DEF(rbtree_float, float)
|
||||||
@@ -219,6 +228,11 @@ static void test_io(void)
|
|||||||
|
|
||||||
rbtree_uint_get_str(str, tree1, false);
|
rbtree_uint_get_str(str, tree1, false);
|
||||||
assert(string_equal_str_p(str, "[]"));
|
assert(string_equal_str_p(str, "[]"));
|
||||||
|
const char *sp;
|
||||||
|
b = rbtree_uint_parse_str(tree2, string_get_cstr(str), &sp);
|
||||||
|
assert(b);
|
||||||
|
assert(*sp == 0);
|
||||||
|
assert(rbtree_uint_equal_p(tree1, tree2));
|
||||||
|
|
||||||
// Fill in data
|
// Fill in data
|
||||||
for(unsigned int i = 0 ; i < 10; i++)
|
for(unsigned int i = 0 ; i < 10; i++)
|
||||||
@@ -238,6 +252,10 @@ static void test_io(void)
|
|||||||
|
|
||||||
rbtree_uint_get_str(str, tree1, false);
|
rbtree_uint_get_str(str, tree1, false);
|
||||||
assert(string_equal_str_p(str, "[0,1,2,3,4,5,6,7,8,9]"));
|
assert(string_equal_str_p(str, "[0,1,2,3,4,5,6,7,8,9]"));
|
||||||
|
b = rbtree_uint_parse_str(tree2, string_get_cstr(str), &sp);
|
||||||
|
assert(b);
|
||||||
|
assert(*sp == 0);
|
||||||
|
assert(rbtree_uint_equal_p(tree1, tree2));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user