quote removal: handle non-nested quotes

This commit is contained in:
Khaïs COLIN 2025-02-28 14:09:56 +01:00
parent 7b76c1a71f
commit a3d2143b8b
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 43 additions and 6 deletions

View file

@ -6,12 +6,14 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
/*
** 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);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}