From 4116c2d92fbadd4b9ac62551b8e312a7aa9ac45f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 10 Mar 2025 14:22:58 +0100 Subject: [PATCH] wordlist: add destroy_idx and pop_idx functions --- Makefile | 1 + src/parser/wordlist/wordlist.h | 4 +- src/parser/wordlist/wordlist_idx.c | 56 +++++++++++++++++++++++ tests/Makefile | 1 + tests/test_wordlist_idx.c | 71 ++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/parser/wordlist/wordlist_idx.c create mode 100644 tests/test_wordlist_idx.c diff --git a/Makefile b/Makefile index ea8043e..5a55b5b 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,7 @@ srcs = \ src/parser/wordlist/wordlist.c \ src/parser/wordlist/wordlist_copy.c \ src/parser/wordlist/wordlist_debug.c \ + src/parser/wordlist/wordlist_idx.c \ src/parser/wordsplit/rule_utils.c \ src/parser/wordsplit/tokenizing_1_5.c \ src/parser/wordsplit/tokenizing_6_10.c \ diff --git a/src/parser/wordlist/wordlist.h b/src/parser/wordlist/wordlist.h index 3e41690..13b6e36 100644 --- a/src/parser/wordlist/wordlist.h +++ b/src/parser/wordlist/wordlist.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:46:02 by khais #+# #+# */ -/* Updated: 2025/03/10 14:26:47 by khais ### ########.fr */ +/* Updated: 2025/03/10 14:27:27 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,5 +41,7 @@ t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc); t_worddesc *wordlist_pop(t_wordlist **wordlist); void wordlist_debug(t_wordlist *wordlist); t_wordlist *wordlist_copy(const t_wordlist *wordlist); +void wordlist_destroy_idx(t_wordlist **wordlist, int idx); +t_worddesc *wordlist_pop_idx(t_wordlist **wordlist, int idx); #endif diff --git a/src/parser/wordlist/wordlist_idx.c b/src/parser/wordlist/wordlist_idx.c new file mode 100644 index 0000000..f9044d2 --- /dev/null +++ b/src/parser/wordlist/wordlist_idx.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* wordlist_idx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/09 15:14:27 by khais #+# #+# */ +/* Updated: 2025/03/10 16:08:43 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "wordlist.h" +#include + +/* +** Remove the element at the given index from the wordlist and return it. +** +** return null if out of range or wordlist is null +*/ +t_worddesc *wordlist_pop_idx(t_wordlist **wordlist, int idx) +{ + t_wordlist *iter; + t_wordlist *temp; + t_worddesc *out; + + if (wordlist == NULL || *wordlist == NULL || idx < 0) + return (NULL); + iter = *wordlist; + if (idx == 0) + { + *wordlist = (*wordlist)->next; + out = iter->word; + return (free(iter), out); + } + while (idx-- > 1) + { + iter = iter->next; + if (iter == NULL) + return (NULL); + } + temp = iter->next; + if (temp == NULL) + return (NULL); + out = temp->word; + iter->next = temp->next; + return (free(temp), out); +} + +/* +** Remove the element at the given index from the wordlist and destroy it. +*/ +void wordlist_destroy_idx(t_wordlist **wordlist, int idx) +{ + worddesc_destroy(wordlist_pop_idx(wordlist, idx)); +} diff --git a/tests/Makefile b/tests/Makefile index 8eb475e..8391cd7 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,7 @@ # make gets confused if a file with the same name exists in the sources, so some # file are prefixed with test_ rawtests = \ + test_wordlist_idx \ test_redirection_parsing \ quote_removal \ cmdlist_use_after_free \ diff --git a/tests/test_wordlist_idx.c b/tests/test_wordlist_idx.c new file mode 100644 index 0000000..c8a4d77 --- /dev/null +++ b/tests/test_wordlist_idx.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_wordlist_idx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/10 14:59:19 by khais #+# #+# */ +/* Updated: 2025/03/10 16:02:01 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "testutil.h" +#include "libft.h" +#include "../src/parser/wordlist/wordlist.h" +#include + +static void test_wordlist_idx_pop_null(void) +{ + t_wordlist *list; + + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + assert(NULL == wordlist_pop_idx(NULL, 10)); + list = NULL; + assert(NULL == wordlist_pop_idx(&list, 10)); + do_leak_check(); +} + +static void test_wordlist_idx_pop_single_elem(void) +{ + t_wordlist *list; + t_worddesc *got; + + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + list = wordlist_create(worddesc_create(ft_strdup("hello"), 0, ft_strdup(""))); + assert(NULL == wordlist_pop_idx(&list, 1)); + got = wordlist_pop_idx(&list, 0); + assert(NULL != got); + assert(NULL == list); + assert_strequal("hello", got->word); + assert(NULL == wordlist_pop_idx(&list, 0)); + do_leak_check(); +} + +static void test_wordlist_idx_pop_second_then_first(void) +{ + t_wordlist *list; + t_worddesc *got; + + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + list = wordlist_create(worddesc_create(ft_strdup("hello"), 0, NULL)); + list = wordlist_push(list, worddesc_create(ft_strdup("world"), 0, NULL)); + wordlist_debug(list); + got = wordlist_pop_idx(&list, 1); + assert(NULL != got); + assert(NULL != list); + assert_strequal("world", got->word); + got = wordlist_pop_idx(&list, 0); + assert(NULL != got); + assert(NULL == list); + assert_strequal("hello", got->word); + do_leak_check(); +} + +int main(void) +{ + test_wordlist_idx_pop_null(); + test_wordlist_idx_pop_single_elem(); + test_wordlist_idx_pop_second_then_first(); + return (0); +}