Fix dbuf iterator begin for dbuf.size == 0

This commit is contained in:
jun 2025-02-23 09:45:29 +01:00
parent 13abedd2ad
commit 1f16b0e849
3 changed files with 16 additions and 9 deletions

View File

@ -40,8 +40,8 @@ typedef enum guf_cpy_opt {
#define GUF_TOK_STRINGIFY(x) #x #define GUF_TOK_STRINGIFY(x) #x
#define GUF_STRINGIFY(x) GUF_TOK_STRINGIFY(x) #define GUF_STRINGIFY(x) GUF_TOK_STRINGIFY(x)
#define GUF_CNT_FOREACH(CNT_PTR, CNT_TYPE, IT_NAME) for (GUF_CAT(CNT_TYPE, _iter) IT_NAME = GUF_CAT(CNT_TYPE, _begin)(CNT_PTR); IT_NAME.ptr != GUF_CAT(CNT_TYPE, _end)(CNT_PTR).ptr; it = GUF_CAT(CNT_TYPE, _iter_next)(CNT_PTR, IT_NAME, 1)) #define GUF_CNT_FOREACH(CNT_PTR, CNT_TYPE, IT_NAME) for (GUF_CAT(CNT_TYPE, _iter) IT_NAME = GUF_CAT(CNT_TYPE, _begin)(CNT_PTR); IT_NAME.ptr != GUF_CAT(CNT_TYPE, _end)(CNT_PTR).ptr; IT_NAME = GUF_CAT(CNT_TYPE, _iter_next)(CNT_PTR, IT_NAME, 1))
#define GUF_CNT_FOREACH_STEP(CNT_PTR, CNT_TYPE, IT_NAME, STEP) for (GUF_CAT(CNT_TYPE, _iter) IT_NAME = GUF_CAT(CNT_TYPE, _begin)(CNT_PTR); IT_NAME.ptr != GUF_CAT(CNT_TYPE, _end)(CNT_PTR).ptr; it = GUF_CAT(CNT_TYPE, _iter_next)(CNT_PTR, IT_NAME, STEP)) #define GUF_CNT_FOREACH_STEP(CNT_PTR, CNT_TYPE, IT_NAME, STEP) for (GUF_CAT(CNT_TYPE, _iter) IT_NAME = GUF_CAT(CNT_TYPE, _begin)(CNT_PTR); IT_NAME.ptr != GUF_CAT(CNT_TYPE, _end)(CNT_PTR).ptr; IT_NAME = GUF_CAT(CNT_TYPE, _iter_next)(CNT_PTR, IT_NAME, STEP))
#define GUF_CNT_FOREACH_REVERSE(CNT_PTR, CNT_TYPE, IT_NAME) for (GUF_CAT(CNT_TYPE, _iter) IT_NAME = GUF_CAT(CNT_TYPE, _rbegin)(CNT_PTR); IT_NAME.ptr != GUF_CAT(CNT_TYPE, _rend)(CNT_PTR).ptr; it = GUF_CAT(CNT_TYPE, _iter_next)(CNT_PTR, IT_NAME, 1)) #define GUF_CNT_FOREACH_REVERSE(CNT_PTR, CNT_TYPE, IT_NAME) for (GUF_CAT(CNT_TYPE, _iter) IT_NAME = GUF_CAT(CNT_TYPE, _rbegin)(CNT_PTR); IT_NAME.ptr != GUF_CAT(CNT_TYPE, _rend)(CNT_PTR).ptr; it = GUF_CAT(CNT_TYPE, _iter_next)(CNT_PTR, IT_NAME, 1))
#define GUF_CNT_FOREACH_REVERSE_STEP(CNT_PTR, CNT_TYPE, IT_NAME, STEP) for (GUF_CAT(CNT_TYPE, _iter) IT_NAME = GUF_CAT(CNT_TYPE, _begin)(CNT_PTR); IT_NAME.ptr != GUF_CAT(CNT_TYPE, _end)(CNT_PTR).ptr; it = GUF_CAT(CNT_TYPE, _iter_next)(CNT_PTR, IT_NAME, STEP)) #define GUF_CNT_FOREACH_REVERSE_STEP(CNT_PTR, CNT_TYPE, IT_NAME, STEP) for (GUF_CAT(CNT_TYPE, _iter) IT_NAME = GUF_CAT(CNT_TYPE, _begin)(CNT_PTR); IT_NAME.ptr != GUF_CAT(CNT_TYPE, _end)(CNT_PTR).ptr; it = GUF_CAT(CNT_TYPE, _iter_next)(CNT_PTR, IT_NAME, STEP))

View File

@ -794,8 +794,9 @@ GUF_FN_KEYWORDS void GUF_CAT(GUF_CNT_NAME, _try_shrink_to_fit)(GUF_CNT_NAME *dbu
GUF_FN_KEYWORDS GUF_CAT(GUF_CNT_NAME, _iter) GUF_CAT(GUF_CNT_NAME, _begin)(const GUF_CNT_NAME* dbuf) GUF_FN_KEYWORDS GUF_CAT(GUF_CNT_NAME, _iter) GUF_CAT(GUF_CNT_NAME, _begin)(const GUF_CNT_NAME* dbuf)
{ {
GUF_ASSERT_RELEASE(GUF_CAT(GUF_CNT_NAME, _valid)(dbuf)); GUF_ASSERT_RELEASE(GUF_CAT(GUF_CNT_NAME, _valid)(dbuf));
return (GUF_CAT(GUF_CNT_NAME, _iter)) { return (GUF_CAT(GUF_CNT_NAME, _iter)) {
.ptr = dbuf->data, .ptr = dbuf->data && dbuf->size ? dbuf->data : NULL,
.base = NULL .base = NULL
}; };
} }

View File

@ -51,6 +51,7 @@
int main(void) int main(void)
{ {
printf("libguf test: compiled with C %ld\n", __STDC_VERSION__); printf("libguf test: compiled with C %ld\n", __STDC_VERSION__);
guf_allocator test_allocator = guf_allocator_libc; guf_allocator test_allocator = guf_allocator_libc;
guf_libc_alloc_ctx test_allocator_ctx = {.alloc_type_id = 0, .thread_id = 0, .zero_init = true}; guf_libc_alloc_ctx test_allocator_ctx = {.alloc_type_id = 0, .thread_id = 0, .zero_init = true};
test_allocator.ctx = &test_allocator_ctx; test_allocator.ctx = &test_allocator_ctx;
@ -60,6 +61,11 @@ int main(void)
dict_cstr_int_insert_val_arg(&ht, "Hello", 42, GUF_CPY_VALUE, GUF_CPY_VALUE); dict_cstr_int_insert_val_arg(&ht, "Hello", 42, GUF_CPY_VALUE, GUF_CPY_VALUE);
dict_cstr_int_insert_val_arg(&ht, "World", 64, GUF_CPY_VALUE, GUF_CPY_VALUE); dict_cstr_int_insert_val_arg(&ht, "World", 64, GUF_CPY_VALUE, GUF_CPY_VALUE);
int kv_iter = 0;
GUF_CNT_FOREACH(&ht, dict_cstr_int, kv_it) {
printf("%d: %s -> %d\n", kv_iter++, kv_it.ptr->key, kv_it.ptr->val);
}
guf_cstr_const key = "World"; guf_cstr_const key = "World";
int *res = dict_cstr_int_at_val_arg(&ht, "World"); int *res = dict_cstr_int_at_val_arg(&ht, "World");
@ -69,13 +75,13 @@ int main(void)
printf("key '%s' not found\n", key); printf("key '%s' not found\n", key);
} }
GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "World")); // GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "World"));
GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "Hello")); // GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "Hello"));
GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "hello") == NULL); // GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "hello") == NULL);
GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "") == NULL); // GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "") == NULL);
GUF_ASSERT(dict_cstr_int_contains_val_arg(&ht, "World")); // GUF_ASSERT(dict_cstr_int_contains_val_arg(&ht, "World"));
GUF_ASSERT(dict_cstr_int_contains_val_arg(&ht, "Hello")); // GUF_ASSERT(dict_cstr_int_contains_val_arg(&ht, "Hello"));
dict_cstr_int_free(&ht, NULL); dict_cstr_int_free(&ht, NULL);