test
This commit is contained in:
parent
93d2a3ce3b
commit
57bd2453ee
87
ft_split.old
87
ft_split.old
@ -1,87 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nkimel <nkimel@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/28 16:55:02 by nkimel #+# #+# */
|
||||
/* Updated: 2022/09/29 19:13:03 by nkimel ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
static void *free_array(char **a)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (a != NULL)
|
||||
{
|
||||
i = 0;
|
||||
while (a[i] != NULL)
|
||||
free(a[i++]);
|
||||
free(a);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static size_t skip_char(const char **s, char c, bool invert)
|
||||
{
|
||||
size_t i;
|
||||
const char *p;
|
||||
|
||||
i = 0;
|
||||
p = *s;
|
||||
while (((!invert && *p == c) || (invert && *p != c)) && *p != '\0')
|
||||
{
|
||||
p++;
|
||||
i++;
|
||||
}
|
||||
*s = p;
|
||||
return (i);
|
||||
}
|
||||
|
||||
static size_t count_elems(const char *s, char c)
|
||||
{
|
||||
size_t n_elem;
|
||||
|
||||
n_elem = 0;
|
||||
skip_char(&s, c, false);
|
||||
while (*s != '\0')
|
||||
{
|
||||
skip_char(&s, c, true);
|
||||
skip_char(&s, c, false);
|
||||
n_elem++;
|
||||
}
|
||||
return (n_elem);
|
||||
}
|
||||
|
||||
char **ft_split(char const *s, char c)
|
||||
{
|
||||
char **array;
|
||||
size_t n_elem;
|
||||
size_t arr_i;
|
||||
const char *rem_s;
|
||||
size_t len;
|
||||
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
n_elem = count_elems(s, c);
|
||||
array = ft_calloc(n_elem + 1, sizeof(char *));
|
||||
if (array == NULL)
|
||||
return (array);
|
||||
skip_char(&s, c, false);
|
||||
arr_i = 0;
|
||||
while (arr_i < n_elem)
|
||||
{
|
||||
rem_s = s;
|
||||
len = skip_char(&s, c, true);
|
||||
array[arr_i] = ft_substr(rem_s, 0, len);
|
||||
if (array[arr_i++] == NULL)
|
||||
return (free_array(array));
|
||||
skip_char(&s, c, false);
|
||||
}
|
||||
return (array);
|
||||
}
|
Loading…
Reference in New Issue
Block a user