changed the split prototypes

This commit is contained in:
Maieul BOYER 2024-07-22 15:41:20 +02:00
parent 4a39152144
commit a00653bd30
No known key found for this signature in database
5 changed files with 158 additions and 122 deletions

View file

@ -6,87 +6,38 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:44:18 by maiboyer ### ########.fr */
/* Updated: 2024/07/22 15:11:14 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
static t_usize local_count_words(t_const_str str, char chr);
static t_str *local_split_inner(t_const_str str, char chr, t_str *out);
static t_str *local_split_freeall(t_str **to_free);
static t_usize local_count_words(t_const_str str, char chr)
t_error str_split(t_const_str str, t_const_str chr, t_vec_str *out)
{
t_usize i;
t_usize out;
t_vec_str ret;
t_usize idx;
t_string buf;
out = 0;
i = 0;
while (str[i])
if (out == NULL || chr == NULL || str == NULL)
return (ERROR);
idx = 0;
buf = string_new(16);
ret = vec_str_new(16, str_free);
while (str[idx] != '\0')
{
while (str[i] && str[i] == chr)
i++;
if (str[i] == 0)
return (out);
out++;
while (str[i] && str[i] != chr)
i++;
while (str[idx] != '\0' && str_find_chr(chr, str[idx]) != NULL)
idx++;
while (str[idx] != '\0' && str_find_chr(chr, str[idx]) == NULL)
string_push_char(&buf, str[idx]);
if (buf.len != 0)
{
vec_str_push(&ret, buf.buf);
buf = string_new(16);
}
}
return (out);
}
static t_str *local_split_freeall(t_str **to_free)
{
while (*to_free)
mem_free(*(to_free++));
return (NULL);
}
static t_str *local_split_inner(t_const_str str, char chr, t_str *out)
{
t_usize str_i;
t_usize sub_i;
t_usize ptr_i;
str_i = 0;
ptr_i = 0;
while (str[str_i])
{
while (str[str_i] && str[str_i] == chr)
str_i++;
if (str[str_i] == 0)
break ;
sub_i = 0;
while (str[str_i + sub_i] && str[str_i + sub_i] != chr)
sub_i++;
out[ptr_i] = mem_alloc(sizeof(char) * (sub_i + 1));
if (out[ptr_i] == NULL)
return (local_split_freeall(&out));
str_l_copy(out[ptr_i++], (t_str)(str + str_i), sub_i + 1);
str_i += sub_i;
}
out[ptr_i] = NULL;
return (out);
}
t_str *str_split(t_const_str str, char chr)
{
t_usize ptr_len;
t_str *out;
if (str == NULL || *str == 0)
{
out = mem_alloc(sizeof(t_str) * 1);
*out = NULL;
return (out);
}
ptr_len = local_count_words(str, chr);
out = mem_alloc_array(sizeof(t_str), (ptr_len + 1));
if (out == NULL)
return (NULL);
return (local_split_inner(str, chr, out));
string_free(buf);
return (*out = ret, NO_ERROR);
}