]> git.ipfire.org Git - pakfire.git/commitdiff
strings: Add a function to pop the last element of an array
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Mar 2025 11:26:59 +0000 (11:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Mar 2025 11:26:59 +0000 (11:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/string.c
src/pakfire/string.h
tests/libpakfire/string.c

index 17e65f35fd0330df6ff35f2f24525c255b275047..0513543df9d7f8efcb78b2a9490eaccafef6782f 100644 (file)
@@ -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.
 */
index 45875f43c5020c85114da9cb2294ed99aee1123c..63b6526ddc3f4e8c45cc14a06e9b7fa306b07198 100644 (file)
@@ -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);
 
index 32e79bd1daddfd11c7eb38bd81ddbb83f55feff4..46d418df6b697f5a3766add12be713e88084655b 100644 (file)
@@ -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);
 }