diff --git a/lib/xy.h b/lib/xy.h index 40fbeb8..a5a55a8 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1356,14 +1356,14 @@ xy_seq_pop (XySeq_t *seq) * @flavor Ruby: Array#each */ void -xy_seq_each (XySeq_t *seq, void (*func)(void *)) +xy_seq_each (XySeq_t *seq, void (*func) (void *, void *), void *user_data) { xy_cant_be_null (seq); xy_cant_be_null (func); for (XySeqItem_t *it = seq->first_item; it; it = it->next) { - func (it->data); + func (it->data, user_data); } } @@ -1371,14 +1371,14 @@ xy_seq_each (XySeq_t *seq, void (*func)(void *)) * @flavor Ruby: Enumerable#find */ void * -xy_seq_find (XySeq_t *seq, bool (*func)(void *)) +xy_seq_find (XySeq_t *seq, bool (*func) (void *, void *), void *user_data) { xy_cant_be_null (seq); xy_cant_be_null (func); for (XySeqItem_t *it = seq->first_item; it; it = it->next) { - if (func (it->data)) + if (func (it->data, user_data)) { return it->data; } @@ -1503,7 +1503,10 @@ xy_map_get (XyMap_t *map, const char *key) * @flavor Ruby: Hash#each */ void -xy_map_each (XyMap_t *map, void (*func)(const char *key, void *value)) +xy_map_each ( + XyMap_t *map, + void (*func) (const char *key, void *value, void *user_data), + void *user_data) { xy_cant_be_null (map); xy_cant_be_null (func); @@ -1513,7 +1516,7 @@ xy_map_each (XyMap_t *map, void (*func)(const char *key, void *value)) struct _XyHashBucket_t *bucket = map->buckets[i]; while (bucket) { - func (bucket->key, bucket->value); + func (bucket->key, bucket->value, user_data); bucket = bucket->next; } } diff --git a/test/xy.c b/test/xy.c index 26f0826..315a70c 100644 --- a/test/xy.c +++ b/test/xy.c @@ -5,7 +5,7 @@ * Test Authors : 曾奥然 * Contributors : Mikachu2333 * Created On : <2023-08-30> - * Last Modified : <2025-08-21> + * Last Modified : <2025-08-22> * * Test xy.h * ------------------------------------------------------------*/ @@ -13,13 +13,13 @@ #include "xy.h" void -print_str_for_seq (void *str) +print_str_for_seq (void *str, void *NOUSE) { print ((char *) str); } void -print_str_for_map (const char *key, void *value) +print_str_for_map (const char *key, void *value, void *NOUSE) { println (xy_strcat (4, " ", key, " => ", (char *) value)); } @@ -150,7 +150,7 @@ main (int argc, char const *argv[]) xy_seq_push (seq, "World"); assert_str ("Hello", xy_seq_at (seq, 1)); assert_str ("World", xy_seq_at (seq, 2)); - xy_seq_each (seq, print_str_for_seq); br(); + xy_seq_each (seq, print_str_for_seq, NULL); br(); xy_seq_pop (seq); assert (1 == xy_seq_len (seq)); @@ -164,7 +164,7 @@ main (int argc, char const *argv[]) assert_str ("chsrc", xy_map_get (map, "Hello")); assert (2 == xy_map_len (map)); echo ("{"); - xy_map_each (map, print_str_for_map); + xy_map_each (map, print_str_for_map, NULL); echo ("}"); xy_succ ("测试完成", "xy.h 测试全部通过");