split_keep

This commit is contained in:
Nathan Lebrun
2025-01-21 12:02:30 +01:00
parent 2f97574a37
commit 281c8c0f63
4 changed files with 135 additions and 19 deletions

View File

@@ -59,8 +59,10 @@ void *ft_calloc(size_t count, size_t size);
void ft_bzero(void *s, size_t n); void ft_bzero(void *s, size_t n);
char **ft_split(const char *s, char *set); char **ft_split(const char *s, char *set);
char **ft_split_keep(const char *s, char *set);
int is_charset(char c, char *set); int is_charset(char c, char *set);
void free_tab(char **tab); void free_tab(char **tab);
char **free_all(char **tab, int count);
void ft_put_c_fd(char c, int fd); void ft_put_c_fd(char c, int fd);
void ft_put_s_fd(char *s, int fd); void ft_put_s_fd(char *s, int fd);

View File

@@ -5,27 +5,13 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */ /* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/11 16:15:47 by nalebrun #+# #+# */ /* Created: 2025/01/21 11:31:07 by nalebrun #+# #+# */
/* Updated: 2024/11/27 12:58:41 by nalebrun ### ########.fr */ /* Updated: 2025/01/21 11:31:07 by nalebrun ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../libft.h" #include "../../libft.h"
static char **free_all(char **tab, int count)
{
int i;
i = 0;
while (i < count)
{
free(tab[i]);
i++;
}
free(tab);
return (NULL);
}
static int count_strings(char const *s, char *set) static int count_strings(char const *s, char *set)
{ {
int count; int count;

View File

@@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_keep.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/21 11:31:48 by nalebrun #+# #+# */
/* Updated: 2025/01/21 11:31:48 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft.h"
static int count_strings(char const *s, char *set)
{
int count;
int in_word;
count = 0;
in_word = 0;
while (*s)
{
if (!is_charset(*s, set) && !in_word)
{
count++;
in_word = 1;
}
else if (is_charset(*s, set))
in_word = 0;
s++;
}
return (count);
}
static void build_str(char const *src, char *dest, char *set)
{
int i;
i = 0;
while (src[i] && !is_charset(src[i], set))
{
dest[i] = src[i];
i++;
}
while (src[i] && is_charset(src[i], set))
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
static char **alloc_tab(char const *s, char *set)
{
int count;
char **tab;
count = count_strings(s, set);
tab = ft_calloc(count + 1, sizeof(char *));
return (tab);
}
static char **build_tab(char **tab, char const *s, char *set)
{
int i;
int j;
int count;
i = 0;
count = 0;
while (tab && s[i])
{
if (is_charset(s[i], set))
i++;
else
{
j = 0;
while (s[i + j] && !is_charset(s[i + j], set))
j++;
while (s[i + j] && is_charset(s[i + j], set))
j++;
tab[count] = ft_calloc((j + 1), sizeof(char));
if (!tab[count])
return (free_all(tab, count));
build_str(&s[i], tab[count++], set);
i += j;
}
}
return (tab);
}
char **ft_split_keep(char const *s, char *set)
{
char **tab;
tab = alloc_tab(s, set);
if (!tab)
return (NULL);
tab = build_tab(tab, s, set);
return (tab);
}

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/21 11:31:30 by nalebrun #+# #+# */
/* Updated: 2025/01/21 11:31:30 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft.h" #include "../../libft.h"
int is_charset(char c, char *set) int is_charset(char c, char *set)
@@ -26,3 +38,17 @@ void free_tab(char **tab)
} }
free(tab); free(tab);
} }
char **free_all(char **tab, int count)
{
int i;
i = 0;
while (i < count)
{
free(tab[i]);
i++;
}
free(tab);
return (NULL);
}