Add function _parse_str for list.

This commit is contained in:
Patrick Pelissier committed 2018-05-03 17:14:10 +02:00
1 parent 9f178208f2
commit 4efa8c1ef9
2 files changed
+50

No files matched your search

+31
View File
@@ -81,6 +81,7 @@
,OPLIST(oplist) \
,M_IF_METHOD(GET_STR, oplist)(GET_STR(M_C(name, _get_str)),) \
,M_IF_METHOD(OUT_STR, oplist)(OUT_STR(M_C(name, _out_str)),) \
,M_IF_METHOD(PARSE_STR, oplist)(PARSE_STR(M_C(name, _parse_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(HASH, oplist)(HASH(M_C(name, _hash)),) \
@@ -549,6 +550,36 @@
} \
, /* no out_str */ ) \
\
M_IF_METHOD(PARSE_STR, oplist)( \
static inline bool \
M_C(name, _parse_str)(list_t list, const char str[], const char **endp) \
{ \
assert (str != NULL && list != NULL); \
M_C(name,_clean)(list); \
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); \
c = *str++; \
if (b == false || c == 0) { goto exit; } \
M_C(name, _push_back)(list, item); \
} while (c == M_GET_SEPARATOR oplist); \
M_GET_CLEAR oplist (item); \
M_C(name, _reverse)(list); \
success = (c == ']'); \
exit: \
if (endp) *endp = str; \
return success; \
} \
, /* no parse_str */ ) \
\
M_IF_METHOD(IN_STR, oplist)( \
static inline bool \
M_C(name, _in_str)(list_t list, FILE *file) \
+19
View File
@@ -237,14 +237,33 @@ static void test_mpz(void)
M_LET(str, STRING_OPLIST) {
list_mpz_get_str(str, v, false);
assert (string_cmp_str (str, "[]") == 0);
const char *sp;
b = list_mpz_parse_str(list2, string_get_cstr(str), &sp);
assert(b);
assert(*sp == 0);
assert(list_mpz_equal_p(v, list2));
my_mpz_set_ui (z, 17);
list_mpz_push_back (v, z);
list_mpz_get_str(str, v, false);
assert (string_cmp_str (str, "[17]") == 0);
b = list_mpz_parse_str(list2, string_get_cstr(str), &sp);
assert(b);
assert(*sp == 0);
assert(list_mpz_equal_p(v, list2));
my_mpz_set_ui (z, 42);
list_mpz_push_back (v, z);
list_mpz_get_str(str, v, true);
assert (string_cmp_str (str, "[17][42,17]") == 0);
b = list_mpz_parse_str(list2, string_get_cstr(str), &sp);
assert(b);
assert(strcmp(sp, "[42,17]") == 0);
assert(!list_mpz_equal_p(v, list2));
b = list_mpz_parse_str(list2, sp, &sp);
assert(b);
assert(strcmp(sp, "") == 0);
assert(list_mpz_equal_p(v, list2));
}
list_mpz_clear(v);