From c478a22e917cd68b1adcab7022923479809d9cb0 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 27 Mar 2010 16:30:27 +0000 Subject: Add simple iteration API and also add brackets in lwc_string_isequal svn path=/trunk/libwapcaplet/; revision=10163 --- include/libwapcaplet/libwapcaplet.h | 18 +++++++++++++++++- src/libwapcaplet.c | 14 ++++++++++++++ test/basictests.c | 17 +++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/include/libwapcaplet/libwapcaplet.h b/include/libwapcaplet/libwapcaplet.h index b71306c..e5b1844 100644 --- a/include/libwapcaplet/libwapcaplet.h +++ b/include/libwapcaplet/libwapcaplet.h @@ -28,6 +28,14 @@ typedef void *(*lwc_allocator_fn)(void *ptr, size_t size, void *pw); */ typedef struct lwc_string_s lwc_string; +/** + * String iteration function + * + * @param str A string which has been interned. + * @param pw The private pointer for the allocator. + */ +typedef void (*lwc_iteration_callback_fn)(lwc_string *str, void *pw); + /** * Result codes which libwapcaplet might return. */ @@ -149,7 +157,7 @@ extern void lwc_string_unref(lwc_string *str); * by \a ret will not be valid. */ #define lwc_string_isequal(str1, str2, ret) \ - (*ret = (str1 == str2)), lwc_error_ok + ((*(ret) = ((str1) == (str2))), lwc_error_ok) /** * Check if two interned strings are case-insensitively equal. @@ -199,4 +207,12 @@ extern size_t lwc_string_length(lwc_string *str); */ extern uint32_t lwc_string_hash_value(lwc_string *str); +/** + * Iterate the context and return every string in it. + * + * @param cb The callback to give the string to. + * @param pw The private word for the callback. + */ +extern void lwc_iterate_strings(lwc_iteration_callback_fn cb, void *pw); + #endif /* libwapcaplet_h_ */ diff --git a/src/libwapcaplet.c b/src/libwapcaplet.c index c452889..59e8637 100644 --- a/src/libwapcaplet.c +++ b/src/libwapcaplet.c @@ -313,3 +313,17 @@ lwc_string_hash_value(lwc_string *str) return str->hash; } + +/**** Iteration ****/ + +void +lwc_iterate_strings(lwc_iteration_callback_fn cb, void *pw) +{ + lwc_hash n; + lwc_string *str; + + for (n = 0; n < ctx->bucketcount; ++n) { + for (str = ctx->buckets[n]; str != NULL; str = str->next) + cb(str, pw); + } +} diff --git a/test/basictests.c b/test/basictests.c index 210b82c..cd1fa32 100644 --- a/test/basictests.c +++ b/test/basictests.c @@ -426,6 +426,22 @@ START_TEST (test_lwc_substring_is_nul_terminated) } END_TEST +static void +counting_cb(lwc_string *str, void *pw) +{ + UNUSED(str); + + *((int *)pw) += 1; +} + +START_TEST (test_lwc_string_iteration) +{ + int counter = 0; + lwc_iterate_strings(counting_cb, (void*)&counter); + fail_unless(counter == 4, "Incorrect string count"); +} +END_TEST + /**** And the suites are set up here ****/ void @@ -501,6 +517,7 @@ lwc_basic_suite(SRunner *sr) tcase_add_test(tc_basic, test_lwc_substring_is_nul_terminated); tcase_add_test(tc_basic, test_lwc_intern_substring_bad_size); tcase_add_test(tc_basic, test_lwc_intern_substring_bad_offset); + tcase_add_test(tc_basic, test_lwc_string_iteration); suite_add_tcase(s, tc_basic); srunner_add_suite(sr, s); -- cgit v1.2.3