aboutsummaryrefslogtreecommitdiffstats
path: root/libft
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-01-24 16:54:01 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-01-24 16:54:01 +0100
commita9182eee769cec9d81f11b19844428da28667022 (patch)
treeef6add01c763a34f618908eddad42efc294ff02c /libft
parentKey defines for compatibility between Linux/Darwin (diff)
download42-cub3d-a9182eee769cec9d81f11b19844428da28667022.tar.gz
42-cub3d-a9182eee769cec9d81f11b19844428da28667022.tar.bz2
42-cub3d-a9182eee769cec9d81f11b19844428da28667022.tar.xz
42-cub3d-a9182eee769cec9d81f11b19844428da28667022.tar.zst
42-cub3d-a9182eee769cec9d81f11b19844428da28667022.zip
ft_split redone
Diffstat (limited to 'libft')
-rw-r--r--libft/Makefile2
-rw-r--r--libft/src/ft_split.c80
-rw-r--r--libft/src/ft_split_redo.c101
3 files changed, 37 insertions, 146 deletions
diff --git a/libft/Makefile b/libft/Makefile
index 04035b9..051fcd7 100644
--- a/libft/Makefile
+++ b/libft/Makefile
@@ -58,7 +58,7 @@ SRCS_NAME += ft_strdup.c
SRCS_NAME += ft_substr.c
SRCS_NAME += ft_strjoin.c
SRCS_NAME += ft_strtrim.c
-SRCS_NAME += ft_split_redo.c
+SRCS_NAME += ft_split.c
SRCS_NAME += ft_itoa.c
SRCS_NAME += ft_itoa_base.c
SRCS_NAME += ft_uitoa.c
diff --git a/libft/src/ft_split.c b/libft/src/ft_split.c
index f0cac95..1d33dda 100644
--- a/libft/src/ft_split.c
+++ b/libft/src/ft_split.c
@@ -1,31 +1,10 @@
-/* ************************************************************************** */
-/* LE - / */
-/* / */
-/* ft_split.c .:: .:/ . .:: */
-/* +:+:+ +: +: +:+:+ */
-/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
-/* #+# #+ #+ #+# */
-/* Created: 2019/10/12 19:24:20 by rbousset #+# ## ## #+# */
-/* Updated: 2019/10/13 08:37:16 by rbousset ### #+. /#+ ###.fr */
-/* / */
-/* / */
-/* ************************************************************************** */
-
#include <libft.h>
#include <stddef.h>
#include <stdlib.h>
#include <inttypes.h>
-static uint8_t
- ft_check(int c, char sep)
-{
- if (c == sep)
- return (1);
- return (0);
-}
-
static size_t
- ft_strlen_plus(const char *str, char c)
+ft_count_words(const char *s, char c)
{
size_t i;
size_t count;
@@ -34,11 +13,11 @@ static size_t
i = 0;
count = 0;
ibool = 1;
- while (str[i])
+ while (s[i])
{
- while (ft_check(str[i], c) && str[i])
+ while (s[i] == c && s[i])
i++;
- while (!ft_check(str[i], c) && str[i])
+ while (s[i] != c && s[i])
{
if (ibool == 1)
count++;
@@ -51,59 +30,72 @@ static size_t
}
static size_t
- ft_strlen_again(const char *str, char c)
+ft_splitlen(const char *str, char c)
{
size_t i;
i = 0;
- while (!ft_check(str[i], c) && str[i])
+ while (str[i] != c && str[i])
i++;
return (i);
}
static char
- *ft_strdup_plus(const char *src, char c)
+ *ft_splitdup(const char *str, char c)
{
+ char *word;
size_t i;
- size_t slen;
- char *nstr;
i = 0;
- slen = ft_strlen_again(src, c) + 1;
- if (!(nstr = (char*)ft_calloc(slen, sizeof(char))))
+ if (!(word = (char*)malloc((ft_splitlen(str, c) + 1) * sizeof(char))))
return (NULL);
- while (!ft_check(src[i], c) && src[i])
+ while (str[i] != c && str[i])
{
- nstr[i] = src[i];
+ word[i] = str[i];
i++;
}
- nstr[i] = '\0';
- return (nstr);
+ word[i] = '\0';
+ return (word);
+}
+
+static void
+ft_splitfree(char **best_split, size_t j)
+{
+ while (j > 0)
+ {
+ free(best_split[j]);
+ j--;
+ }
+ free(best_split);
}
char
**ft_split(const char *s, char c)
{
+ char **best_split;
size_t i;
size_t j;
- char **best_split;
i = 0;
j = 0;
- if (!(best_split = (char **)ft_calloc(ft_strlen_plus(s, c) + 1,
- sizeof(char *))))
+ if (!(best_split = (char **)malloc((ft_count_words(s, c) + 1)
+ * sizeof(char *))))
return (NULL);
while (s[i])
{
- while (ft_check(s[i], c) && s[i])
+ while (s[i] == c && s[i])
i++;
- while (!ft_check(s[i], c) && s[i])
+ while (s[i] != c && s[i])
{
- best_split[j] = ft_strdup_plus(&s[i], c);
- i += ft_strlen_again(&s[i], c);
+ if (!(best_split[j] = ft_splitdup(s + i, c)))
+ {
+ ft_splitfree(best_split, j);
+ return (NULL);
+ }
+ i += ft_splitlen(s + i, c);
j++;
}
}
- best_split[j] = 0;
+ best_split[j] = NULL;
return (best_split);
}
diff --git a/libft/src/ft_split_redo.c b/libft/src/ft_split_redo.c
deleted file mode 100644
index 1d33dda..0000000
--- a/libft/src/ft_split_redo.c
+++ /dev/null
@@ -1,101 +0,0 @@
-#include <libft.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <inttypes.h>
-
-static size_t
-ft_count_words(const char *s, char c)
-{
- size_t i;
- size_t count;
- uint8_t ibool;
-
- i = 0;
- count = 0;
- ibool = 1;
- while (s[i])
- {
- while (s[i] == c && s[i])
- i++;
- while (s[i] != c && s[i])
- {
- if (ibool == 1)
- count++;
- ibool = 0;
- i++;
- }
- ibool = 1;
- }
- return (count);
-}
-
-static size_t
-ft_splitlen(const char *str, char c)
-{
- size_t i;
-
- i = 0;
- while (str[i] != c && str[i])
- i++;
- return (i);
-}
-
-static char
- *ft_splitdup(const char *str, char c)
-{
- char *word;
- size_t i;
-
- i = 0;
- if (!(word = (char*)malloc((ft_splitlen(str, c) + 1) * sizeof(char))))
- return (NULL);
- while (str[i] != c && str[i])
- {
- word[i] = str[i];
- i++;
- }
- word[i] = '\0';
- return (word);
-}
-
-static void
-ft_splitfree(char **best_split, size_t j)
-{
- while (j > 0)
- {
- free(best_split[j]);
- j--;
- }
- free(best_split);
-}
-
-char
- **ft_split(const char *s, char c)
-{
- char **best_split;
- size_t i;
- size_t j;
-
- i = 0;
- j = 0;
- if (!(best_split = (char **)malloc((ft_count_words(s, c) + 1)
- * sizeof(char *))))
- return (NULL);
- while (s[i])
- {
- while (s[i] == c && s[i])
- i++;
- while (s[i] != c && s[i])
- {
- if (!(best_split[j] = ft_splitdup(s + i, c)))
- {
- ft_splitfree(best_split, j);
- return (NULL);
- }
- i += ft_splitlen(s + i, c);
- j++;
- }
- }
- best_split[j] = NULL;
- return (best_split);
-}