diff options
| -rw-r--r-- | docs/ccommon_api.md | 2 | ||||
| -rw-r--r-- | examples/books.c | 4 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 12 |
3 files changed, 8 insertions, 10 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 1cdc56cb..32f2f3dc 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -298,7 +298,7 @@ if (i.ref) printf("%d\n", *i.ref); // Search map for a string containing "hello" and erase it: cmap_str_iter it, it1 = ..., it2 = ...; -c_find_in(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello")); +c_find_if(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello")); if (it.ref) cmap_str_erase_at(&map, it); // Erase all strings containing "hello": diff --git a/examples/books.c b/examples/books.c index eed19540..90d2d529 100644 --- a/examples/books.c +++ b/examples/books.c @@ -42,8 +42,8 @@ int main() // Look up the values associated with some keys. const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland"}; c_forloop (i, c_arraylen(to_find)) { - const cmap_str_value* b; - if ((b = cmap_str_get(&book_reviews, to_find[i]))) + const cmap_str_value* b = cmap_str_get(&book_reviews, to_find[i]); + if (b) printf("%s: %s\n", cstr_str(&b->first), cstr_str(&b->second)); else printf("%s is unreviewed.\n", to_find[i]); diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 0e5b3b22..e3406727 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -238,13 +238,13 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, #define c_drop(C, ...) do { c_forlist (_i, C*, {__VA_ARGS__}) C##_drop(*_i.ref); } while(0) -#define c_find_if(it, C, cnt, pred) do { \ +#define c_find_if(...) c_MACRO_OVERLOAD(c_find_if, __VA_ARGS__) +#define c_find_if4(it, C, cnt, pred) do { \ size_t index = 0; \ for (it = C##_begin(&cnt); it.ref && !(pred); C##_next(&it)) \ ++index; \ } while (0) - -#define c_find_in(it, C, start, end, pred) do { \ +#define c_find_if5(it, C, start, end, pred) do { \ size_t index = 0; \ const C##_value* _endref = (end).ref; \ for (it = start; it.ref != _endref && !(pred); C##_next(&it)) \ @@ -253,12 +253,10 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, } while (0) #define c_erase_if(it, C, cnt, pred) do { \ - size_t index = 0, count = 0; \ C##_iter it = C##_begin(&cnt); \ - while (it.ref) { \ - if (pred) it = C##_erase_at(&cnt, it), ++count; \ + for (size_t index = 0; it.ref; ++index) { \ + if (pred) it = C##_erase_at(&cnt, it); \ else C##_next(&it); \ - ++index; \ } \ } while (0) |
