From: Michael Tremer Date: Sat, 15 Mar 2025 11:26:59 +0000 (+0000) Subject: strings: Add a function to pop the last element of an array X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=928c43eefbb912a38b4db92eea6a50c8dc8671c1;p=pakfire.git strings: Add a function to pop the last element of an array Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/string.c b/src/pakfire/string.c index 17e65f35..0513543d 100644 --- a/src/pakfire/string.c +++ b/src/pakfire/string.c @@ -581,6 +581,28 @@ int pakfire_strings_appendm(char*** array, const char** strings) { return r; } +void pakfire_strings_pop(char*** array) { + char* s = NULL; + + if (!array) + return; + + // Fetch the length + size_t length = pakfire_strings_length(*array); + + // Return if the array is empty + if (!length) + return; + + // Free the string + s = (*array)[length - 1]; + if (s) + free(s); + + // Reset the pointer + (*array)[length - 1] = NULL; +} + /* This function dumps the string array to the console. This is useful for debugging only. */ diff --git a/src/pakfire/string.h b/src/pakfire/string.h index 45875f43..63b6526d 100644 --- a/src/pakfire/string.h +++ b/src/pakfire/string.h @@ -113,6 +113,7 @@ int pakfire_strings_append(char*** array, const char* s); int pakfire_strings_appendf(char*** array, const char* format, ...) __attribute__((format(printf, 2, 3))); int pakfire_strings_appendm(char*** array, const char** strings); +void pakfire_strings_pop(char*** array); int pakfire_strings_dump(char** array); diff --git a/tests/libpakfire/string.c b/tests/libpakfire/string.c index 32e79bd1..46d418df 100644 --- a/tests/libpakfire/string.c +++ b/tests/libpakfire/string.c @@ -475,6 +475,46 @@ FAIL: return r; } +static int test_strings_pop(const struct test* t) { + char** array = NULL; + int r = EXIT_FAILURE; + + // Append a few strings + ASSERT_SUCCESS(pakfire_strings_append(&array, "a")); + ASSERT_SUCCESS(pakfire_strings_append(&array, "b")); + ASSERT_SUCCESS(pakfire_strings_append(&array, "c")); + + // The length should now be three + ASSERT_EQUALS(pakfire_strings_length(array), 3); + + // Let's pop something + pakfire_strings_pop(&array); + + // Now this should be shorter + ASSERT_EQUALS(pakfire_strings_length(array), 2); + + // Let's pop something more + pakfire_strings_pop(&array); + + // Now this should be even shorter + ASSERT_EQUALS(pakfire_strings_length(array), 1); + + // Let's pop once more + pakfire_strings_pop(&array); + + // Now this should be empty + ASSERT_EQUALS(pakfire_strings_length(array), 0); + + // Pop the empty array + pakfire_strings_pop(&array); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + return r; +} + int main(int argc, const char* argv[]) { testsuite_add_test(test_string_set, 0); testsuite_add_test(test_string_setn, 0); @@ -492,6 +532,7 @@ int main(int argc, const char* argv[]) { testsuite_add_test(test_string_contains_whitespace, 0); testsuite_add_test(test_string_search, 0); testsuite_add_test(test_truncate, 0); + testsuite_add_test(test_strings_pop, 0); return testsuite_run(argc, argv); }