Expansion: fixed the algo for star expansion.

This commit is contained in:
Jérôme Guélen 2025-03-20 16:57:05 +01:00
parent 41c9548934
commit 00f27ae05c
No known key found for this signature in database
3 changed files with 14 additions and 59 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* wildcard_exp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 15:01/38 by khais #+# #+# */
/* Updated: 2025/03/20 15:01:38 by khais ### ########.fr */
/* Created: 2025/03/20 15:01:38 by jguelen #+# #+# */
/* Updated: 2025/03/20 16:53:31 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -96,7 +96,6 @@ static t_wordlist *expand_star_core(t_worddesc *file_pattern)
new = readdir(current_dir);
while (new)
{
ft_printf("[dbg] read dir entry: %s\n", new->d_name);
if (fits_pattern(new->d_name, file_pattern))
{
if (add_file_to_list(&file_wordlist, new->d_name) == NULL)
@ -128,8 +127,8 @@ char fits_pattern(char *str, t_worddesc *pattern)
size_t i;
char ret;
if (ft_strcmp(".", str) == 0)
ft_printf("[dbg] currently matching .\n");
if (str && str[0] == '.' && pattern->word[0] != '.')
return (0);
pattern_len = ft_strlen(pattern->word);
str_len = ft_strlen(str);
pattern_check = ft_calloc(str_len + 1, sizeof(char *));

View file

@ -3,14 +3,13 @@
/* ::: :::::::: */
/* wildcard_exp_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 15:45/36 by khais #+# #+# */
/* Updated: 2025/03/20 15:45:36 by khais ### ########.fr */
/* Created: 2025/03/20 16:51:26 by jguelen #+# #+# */
/* Updated: 2025/03/20 16:51:57 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "subst.h"
#include <stdbool.h>
@ -48,8 +47,7 @@ static int same_character_or_one_char_wild(char *str, t_worddesc *pattern,
|| (pattern->word[j - 1] == '?'
&& pattern->marker[j - 1] != '\''
&& pattern->marker[j - 1] != '"'
&& pattern->marker[j - 1] != '&'
&& !(i == 1 && str[i - 1] == '.')));
&& pattern->marker[j - 1] != '&'));
}
/*
@ -86,40 +84,6 @@ static void init_pattern_checker(t_worddesc *pattern, char **checker)
}
}
static void checker_debug(char **checker, char *str, char *pattern)
{
size_t i;
size_t j;
size_t str_len;
size_t pattern_len;
str_len = ft_strlen(str);
pattern_len = ft_strlen(pattern);
i = 0;
ft_printf("str=[%s] pattern=[%s]\n", str, pattern);
ft_printf(" %s\n", pattern);
while (i <= str_len)
{
j = 0;
if (i == 0)
ft_printf(" ");
else if (str[i - 1])
ft_printf("%c", str[i - 1]);
else
ft_printf(" ");
while (j <= pattern_len)
{
if (checker[i][j])
ft_printf("");
else
ft_printf("");
j++;
}
ft_printf("\n");
i++;
}
}
/*
** Fills the table which contains in its most low and right cell 0 if
** str does not match the pattern and 1 otherwise.
@ -136,29 +100,21 @@ void build_pattern_checks(char *str, t_worddesc *pattern,
size_t j;
size_t str_len;
size_t pattern_len;
bool only_saw_unquoted_stars;
i = 1;
str_len = ft_strlen(str);
pattern_len = ft_strlen(pattern->word);
init_pattern_checker(pattern, checker);
only_saw_unquoted_stars = true;
while (i <= str_len)
{
j = 1;
while (j <= pattern_len)
{
if (same_character_or_one_char_wild(str, pattern, i, j))
{
only_saw_unquoted_stars = false;
checker[i][j] = checker[i - 1][j - 1];
}
else if (at_star_in_pattern(str, pattern, i, j) && !(only_saw_unquoted_stars || (i == 1 && str[0] == '.')))
else if (at_star_in_pattern(str, pattern, i, j))
checker[i][j] = !!(checker[i - 1][j] + checker[i][j - 1]
+ checker[i - 1][j - 1]);
if (at_star_in_pattern(str, pattern, i, j) == 0)
only_saw_unquoted_stars = false;
ft_printf("at i=%d j=%d\n", i, j);
checker_debug(checker, str, pattern->word);
j++;
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 14:51/50 by khais #+# #+# */
/* Updated: 2025/03/20 14:51:50 by khais ### ########.fr */
/* Updated: 2025/03/20 16:22:15 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -170,9 +170,9 @@ static void test_filename_star_expansion(void)
expanded = expand_star(filepattern);
wordlist_debug(expanded);
assert(wordlist_size(expanded) == 3);
//assert_strequal(".", expanded->word->word);
//assert_strequal("..", expanded->next->word->word);
//assert_strequal(".plop", expanded->next->next->word->word);
assert_strequal(".", expanded->word->word);
assert_strequal("..", expanded->next->word->word);
assert_strequal(".plop", expanded->next->next->word->word);
worddesc_destroy(filepattern);
wordlist_destroy(expanded);
//test zero result