diff --git a/src/parser/remove_quotes/remove_quotes.c b/src/parser/remove_quotes/remove_quotes.c index 97f17fb..31c0da7 100644 --- a/src/parser/remove_quotes/remove_quotes.c +++ b/src/parser/remove_quotes/remove_quotes.c @@ -6,12 +6,14 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/28 13:52:02 by khais #+# #+# */ -/* Updated: 2025/03/07 11:09:30 by khais ### ########.fr */ +/* Updated: 2025/03/07 11:13:49 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "remove_quotes.h" -#include "libft.h" +#include "../../buffer/buffer.h" +#include "../matchers/quote.h" +#include /* ** use the marker in the given worddesc to create a new worddesc by removing @@ -20,15 +22,28 @@ ** The new worddesc will have the marker set to NULL. ** ** If word is null return null. +** +** In case of allocation failure, return null */ t_worddesc *remove_quotes(t_worddesc *word) { t_worddesc *output; + t_buffer *buf; + size_t i; if (word == NULL) return (NULL); - output = worddesc_create(ft_strdup(word->word), word->flags, NULL); - if (output->word == NULL) - return (worddesc_destroy(output), NULL); + buf = ft_buffer_new(); + i = 0; + while (word->word[i] != '\0') + { + if (!is_quote(word->word[i])) + buf = ft_buffer_pushchar(buf, word->word[i]); + i++; + } + if (buf == NULL) + return (NULL); + output = worddesc_create(buf->buffer, word->flags, NULL); + free(buf); return (output); } diff --git a/tests/quote_removal.c b/tests/quote_removal.c index 47ced45..6568b5e 100644 --- a/tests/quote_removal.c +++ b/tests/quote_removal.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/28 13:46:56 by khais #+# #+# */ -/* Updated: 2025/03/07 11:07:59 by khais ### ########.fr */ +/* Updated: 2025/03/07 11:14:48 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,8 +42,30 @@ static void test_quote_removal_null(void) assert(got_word == NULL); } +static void test_quote_removal_single_quotes(void) +{ + t_worddesc *word = create_single_word("'word'"); + t_worddesc *got_word = remove_quotes(word); + assert_strequal("word", got_word->word); + assert(got_word->marker == NULL); + worddesc_destroy(word); + worddesc_destroy(got_word); +} + +static void test_quote_removal_double_quotes(void) +{ + t_worddesc *word = create_single_word("\"word\""); + t_worddesc *got_word = remove_quotes(word); + assert_strequal("word", got_word->word); + assert(got_word->marker == NULL); + worddesc_destroy(word); + worddesc_destroy(got_word); +} + int main(void) { test_quote_removal_no_quotes_single_word(); test_quote_removal_null(); + test_quote_removal_single_quotes(); + test_quote_removal_double_quotes(); return (0); }