splitv2
This commit is contained in:
111
ft_split.c
111
ft_split.c
@@ -1,111 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_split.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/07/17 12:48:12 by lderidde #+# #+# */
|
|
||||||
/* Updated: 2024/07/25 14:13:12 by lderidde ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
int check_sep(char c, char *charset)
|
|
||||||
{
|
|
||||||
while (*charset)
|
|
||||||
{
|
|
||||||
if (c == *charset)
|
|
||||||
return (1);
|
|
||||||
charset++;
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int count_strings(char *str, char *charset)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int count;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
count = 0;
|
|
||||||
while (str[i])
|
|
||||||
{
|
|
||||||
if (!(check_sep(str[i], charset))
|
|
||||||
&& (check_sep(str[i + 1], charset)
|
|
||||||
|| str[i + 1] == '\0'))
|
|
||||||
count++;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (count);
|
|
||||||
}
|
|
||||||
|
|
||||||
void build_str(char *charset, char *src, char *dest)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (src[i] && !(check_sep(src[i], charset)))
|
|
||||||
{
|
|
||||||
dest[i] = src[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
dest[i] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
void build_tab(char **tab, char *str, char *charset)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int count;
|
|
||||||
int j;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
count = 0;
|
|
||||||
while (str[i])
|
|
||||||
{
|
|
||||||
if (check_sep(str[i], charset))
|
|
||||||
i++;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
j = 0;
|
|
||||||
while (!(check_sep(str[j + i], charset)) && str[j + i])
|
|
||||||
j++;
|
|
||||||
tab[count] = malloc(sizeof(char) * (j + 1));
|
|
||||||
build_str(charset, &str[i], tab[count]);
|
|
||||||
count++;
|
|
||||||
i = i + j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char **ft_split(char *str, char *charset)
|
|
||||||
{
|
|
||||||
int count;
|
|
||||||
char **tab;
|
|
||||||
|
|
||||||
count = count_strings(str, charset);
|
|
||||||
tab = malloc(sizeof(char *) * (count + 1));
|
|
||||||
if (tab == NULL)
|
|
||||||
return (NULL);
|
|
||||||
tab[count] = 0;
|
|
||||||
build_tab(tab, str, charset);
|
|
||||||
return (tab);
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
#include <stdio.h>
|
|
||||||
int main (int argc, char **argv)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
(void)argc;
|
|
||||||
char **res = ft_split(argv[1], argv[2]);
|
|
||||||
int len = count_strings(argv[1], argv[2]);
|
|
||||||
while (i < len)
|
|
||||||
{
|
|
||||||
printf("%s\n", res[i]);
|
|
||||||
free(res[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
free(res);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
@@ -58,7 +58,8 @@ char *ft_itoa(int n);
|
|||||||
void *ft_calloc(size_t count, size_t size);
|
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 c);
|
char **ft_split(const char *s, char *set);
|
||||||
|
int is_charset(char c, char *set);
|
||||||
void free_tab(char **tab);
|
void free_tab(char **tab);
|
||||||
|
|
||||||
void ft_put_c_fd(char c, int fd);
|
void ft_put_c_fd(char c, int fd);
|
||||||
|
|||||||
@@ -12,77 +12,85 @@
|
|||||||
|
|
||||||
#include "../../libft.h"
|
#include "../../libft.h"
|
||||||
|
|
||||||
static int ft_word_count(const char *str, char c)
|
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)
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
if (!str)
|
|
||||||
return (0);
|
|
||||||
count = 0;
|
count = 0;
|
||||||
while (*str)
|
while (*s)
|
||||||
{
|
{
|
||||||
while (*str && *str == c)
|
if (!is_charset(*s, set) && is_charset(*(s + 1), set)
|
||||||
str++;
|
|| *(s + 1) == '\0')
|
||||||
if (*str && *str != c)
|
count ++;
|
||||||
count++;
|
s++;
|
||||||
while (*str && *str != c)
|
|
||||||
str++;
|
|
||||||
}
|
}
|
||||||
return (count);
|
return (count);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *malloccpy(const char *str, char c)
|
static void build_str(char const *src, char *dest, char *set)
|
||||||
{
|
{
|
||||||
char *result;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (str[i] && str[i] != c)
|
while (src[i] && !is_charset(src[i], set))
|
||||||
|
{
|
||||||
|
dest[i] = src[i];
|
||||||
i++;
|
i++;
|
||||||
result = (char *)malloc(sizeof(char) * (i + 1));
|
}
|
||||||
if (!result)
|
dest[i] = '\0';
|
||||||
return (NULL);
|
|
||||||
ft_strlcpy(result, str, i + 1);
|
|
||||||
return (result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_tab(char **tab)
|
static char **build_tab(char **tab, char const *s, char *set)
|
||||||
{
|
{
|
||||||
char **pos;
|
|
||||||
|
|
||||||
if (!tab)
|
|
||||||
return ;
|
|
||||||
pos = tab;
|
|
||||||
while (*pos != NULL)
|
|
||||||
free(*(pos++));
|
|
||||||
free(tab);
|
|
||||||
}
|
|
||||||
|
|
||||||
char **ft_split(const char *s, char c)
|
|
||||||
{
|
|
||||||
char **result;
|
|
||||||
int i;
|
int i;
|
||||||
int strs_len;
|
int j;
|
||||||
|
int count;
|
||||||
|
|
||||||
if (!s)
|
i = 0;
|
||||||
return (NULL);
|
count = 0;
|
||||||
strs_len = ft_word_count(s, c);
|
while (tab && s[i])
|
||||||
result = (char **)malloc(sizeof(char *) * (strs_len + 1));
|
|
||||||
if (!result)
|
|
||||||
return (NULL);
|
|
||||||
i = -1;
|
|
||||||
while (++i < strs_len)
|
|
||||||
{
|
{
|
||||||
while (*s && *s == c)
|
if (is_charset(s[i], set))
|
||||||
s++;
|
i++;
|
||||||
result[i] = malloccpy(s, c);
|
else
|
||||||
if (!result[i])
|
|
||||||
{
|
{
|
||||||
free_tab(result);
|
j = 0;
|
||||||
return (NULL);
|
while (!is_charset(s[i + j], set) && s[i + j])
|
||||||
|
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 = i + j;
|
||||||
}
|
}
|
||||||
s += ft_strlen(result[i]);
|
|
||||||
}
|
}
|
||||||
result[i] = 0;
|
return (tab);
|
||||||
return (result);
|
}
|
||||||
|
|
||||||
|
char **ft_split(char const *s, char *set)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
char **tab;
|
||||||
|
|
||||||
|
count = count_strings(s, set);
|
||||||
|
tab = ft_calloc(count + 1, sizeof(char *));
|
||||||
|
if (!tab)
|
||||||
|
return (NULL);
|
||||||
|
tab[count] = 0;
|
||||||
|
tab = build_tab(tab, s, set);
|
||||||
|
return (tab);
|
||||||
}
|
}
|
||||||
|
|||||||
15
lib/libft/srcs/format/ft_split_utils.c
Normal file
15
lib/libft/srcs/format/ft_split_utils.c
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include "lib/libft/libft.h"
|
||||||
|
|
||||||
|
int is_charset(char c, char *set)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (set[i])
|
||||||
|
{
|
||||||
|
if (c == set[i])
|
||||||
|
return (1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user