diff --git a/m-bitset.h b/m-bitset.h index 2d92c02d..6a02d94d 100644 --- a/m-bitset.h +++ b/m-bitset.h @@ -523,22 +523,32 @@ bitset_in_str(bitset_t set, FILE *file) } 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); assert(str != NULL); + bool success = false; bitset_clean(set); char c = *str++; - if (c != '[') return false; + if (c != '[') goto exit; c = *str++; do { - if (c != '0' && c != '1') return false; + if (c != '0' && c != '1') goto exit; const bool b = (c == '1'); bitset_push_back (set, b); c = *str++; } while (c != ']' && c != 0); 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 diff --git a/tests/test-mbitset.c b/tests/test-mbitset.c index 0fa44955..36be221a 100644 --- a/tests/test-mbitset.c +++ b/tests/test-mbitset.c @@ -105,15 +105,17 @@ static void test1(void) assert (b == false); assert (bitset_size(set) == 196); - bitset_set_str(set, "[1010101010101010101000000000000000000000000000000000000000000000000000001111]"); + bitset_parse_str(set, "[1010101010101010101000000000000000000000000000000000000000000000000000001111]", NULL); bitset_push_at(set, 23, true); bitset_t set3; bitset_init(set3); - bitset_set_str(set3, "[10101010101010101010000100000000000000000000000000000000000000000000000001111]"); + bitset_parse_str(set3, "[10101010101010101010000100000000000000000000000000000000000000000000000001111]", NULL); assert(bitset_equal_p(set, set3)); 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 (*sp == 0); bitset_clear(set3); bitset_clean(set);