aboutsummaryrefslogtreecommitdiff
path: root/ft_split.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-17 09:09:41 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-17 09:09:41 +0200
commit6cb01d2fd8a6b07ef3ddaa8bb322f30c545316e7 (patch)
treeb4ff52e57f5cbef2a4ea578e0401a2b206318d4a /ft_split.c
parent8a4d4c806e9896228f016baa62c5c7e219acf655 (diff)
downloadlibft-6cb01d2fd8a6b07ef3ddaa8bb322f30c545316e7.tar.gz
libft-6cb01d2fd8a6b07ef3ddaa8bb322f30c545316e7.tar.bz2
libft-6cb01d2fd8a6b07ef3ddaa8bb322f30c545316e7.zip
More protection
- substr if start > str length - all list functions check for NULL reference - not modifying const pointers
Diffstat (limited to 'ft_split.c')
-rw-r--r--ft_split.c42
1 files changed, 21 insertions, 21 deletions
diff --git a/ft_split.c b/ft_split.c
index 74703fb..62ba476 100644
--- a/ft_split.c
+++ b/ft_split.c
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_strsplit.c :+: :+: :+: */
+/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
-/* Created: 2019/10/07 10:22:09 by cacharle #+# #+# */
-/* Updated: 2019/10/14 12:51:34 by cacharle ### ########.fr */
+/* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */
+/* Updated: 2019/10/17 08:39:40 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,18 +15,20 @@
static size_t count_segment(char const *s, char c)
{
size_t counter;
+ int i;
counter = 0;
- while (*s)
+ i = 0;
+ while (s[i])
{
- if (*s == c)
+ if (s[i] == c)
{
- s++;
+ i++;
continue ;
}
counter++;
- while (*s && *s != c)
- s++;
+ while (s[i] && s[i] != c)
+ i++;
}
return (counter);
}
@@ -62,28 +64,26 @@ static void *destroy_strs(char **strs)
char **ft_split(char const *s, char c)
{
char **strs;
- size_t size;
+ size_t tab_counter;
size_t i;
size_t j;
- size = count_segment(s, c);
- if ((strs = (char**)malloc(sizeof(char*) * (size + 1))) == NULL)
+ tab_counter = count_segment(s, c);
+ if ((strs = (char**)malloc(sizeof(char*) * (tab_counter + 1))) == NULL)
return (NULL);
- j = 0;
- while (*s)
+ tab_counter = 0;
+ j = -1;
+ while (s[++j])
{
- if (*s == c)
- {
- s++;
+ if (s[j] == c)
continue ;
- }
i = 0;
- while (s[i] && s[i] != c)
+ while (s[j + i] && s[j + i] != c)
i++;
- if ((strs[j++] = ft_strndup(s, i)) == NULL)
+ if ((strs[tab_counter++] = ft_strndup(&s[j], i)) == NULL)
return (destroy_strs(strs));
- s += i;
+ j += i - 1;
}
- strs[j] = NULL;
+ strs[tab_counter] = NULL;
return (strs);
}