Add _parse_str interface.
Rewrite _set_str to use the _parse_str
This commit is contained in:
1 parent
2ad8ae3451
commit
6b24842704
2 files changed
+19
-7
No files matched your search
+14
-4
@@ -523,22 +523,32 @@ bitset_in_str(bitset_t set, FILE *file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline bool
|
static inline bool
|
||||||
bitset_set_str(bitset_t set, const char str[])
|
bitset_parse_str(bitset_t set, const char str[], const char **endptr)
|
||||||
{
|
{
|
||||||
BITSETI_CONTRACT (set);
|
BITSETI_CONTRACT (set);
|
||||||
assert(str != NULL);
|
assert(str != NULL);
|
||||||
|
bool success = false;
|
||||||
bitset_clean(set);
|
bitset_clean(set);
|
||||||
char c = *str++;
|
char c = *str++;
|
||||||
if (c != '[') return false;
|
if (c != '[') goto exit;
|
||||||
c = *str++;
|
c = *str++;
|
||||||
do {
|
do {
|
||||||
if (c != '0' && c != '1') return false;
|
if (c != '0' && c != '1') goto exit;
|
||||||
const bool b = (c == '1');
|
const bool b = (c == '1');
|
||||||
bitset_push_back (set, b);
|
bitset_push_back (set, b);
|
||||||
c = *str++;
|
c = *str++;
|
||||||
} while (c != ']' && c != 0);
|
} while (c != ']' && c != 0);
|
||||||
BITSETI_CONTRACT (set);
|
BITSETI_CONTRACT (set);
|
||||||
return c == ']';
|
success = (c == ']');
|
||||||
|
exit:
|
||||||
|
if (endptr) *endptr = str;
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool
|
||||||
|
bitset_set_str(bitset_t dest, const char str[])
|
||||||
|
{
|
||||||
|
return bitset_parse_str(dest, str, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
|
|||||||
@@ -105,15 +105,17 @@ static void test1(void)
|
|||||||
assert (b == false);
|
assert (b == false);
|
||||||
assert (bitset_size(set) == 196);
|
assert (bitset_size(set) == 196);
|
||||||
|
|
||||||
bitset_set_str(set, "[1010101010101010101000000000000000000000000000000000000000000000000000001111]");
|
bitset_parse_str(set, "[1010101010101010101000000000000000000000000000000000000000000000000000001111]", NULL);
|
||||||
bitset_push_at(set, 23, true);
|
bitset_push_at(set, 23, true);
|
||||||
bitset_t set3;
|
bitset_t set3;
|
||||||
bitset_init(set3);
|
bitset_init(set3);
|
||||||
bitset_set_str(set3, "[10101010101010101010000100000000000000000000000000000000000000000000000001111]");
|
bitset_parse_str(set3, "[10101010101010101010000100000000000000000000000000000000000000000000000001111]", NULL);
|
||||||
assert(bitset_equal_p(set, set3));
|
assert(bitset_equal_p(set, set3));
|
||||||
bitset_push_at(set, 25, true);
|
bitset_push_at(set, 25, true);
|
||||||
bitset_set_str(set3, "[101010101010101010100001010000000000000000000000000000000000000000000000001111]");
|
const char *sp;
|
||||||
|
bitset_parse_str(set3, "[101010101010101010100001010000000000000000000000000000000000000000000000001111]", &sp);
|
||||||
assert(bitset_equal_p(set, set3));
|
assert(bitset_equal_p(set, set3));
|
||||||
|
assert (*sp == 0);
|
||||||
bitset_clear(set3);
|
bitset_clear(set3);
|
||||||
|
|
||||||
bitset_clean(set);
|
bitset_clean(set);
|
||||||
|
|||||||
Reference in new issue
Block a user