This commit is contained in:
kinou 2020-12-13 19:42:52 +01:00
parent 88349d0da5
commit 47894413ec
5 changed files with 51 additions and 45 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/08 18:20:19 by apommier #+# #+# */ /* Created: 2020/12/08 18:20:19 by apommier #+# #+# */
/* Updated: 2020/12/12 20:54:29 by apommier ### ########.fr */ /* Updated: 2020/12/12 21:58:21 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,7 @@ char *fill(long n, int j, int minus)
char *dest; char *dest;
j += minus; j += minus;
dest = (char*)ft_malloc(sizeof(char) * (j + 1)); dest = (char*)malloc(sizeof(char) * (j + 1));
if (dest == 0) if (dest == 0)
return (0); return (0);
dest[j] = 0; dest[j] = 0;
@ -55,5 +55,7 @@ char *ft_itoa(int n)
i = i * 10; i = i * 10;
j++; j++;
} }
if (k == 9)
j = 1;
return (fill(k, j, minus)); return (fill(k, j, minus));
} }

View File

@ -6,55 +6,49 @@
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/07 00:54:12 by apommier #+# #+# */ /* Created: 2020/12/07 00:54:12 by apommier #+# #+# */
/* Updated: 2020/12/12 11:44:32 by apommier ### ########.fr */ /* Updated: 2020/12/13 18:39:50 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
int fill_tab(char *s, char c, char *dest) static int fill_tab(char *s, char c, char **dest, size_t index)
{ {
int i; int i;
i = 0;
while (s[i] != c && s[i]) while (s[i] != c && s[i])
i++; i++;
dest = (char*)malloc(sizeof(char) * i + 1); dest[index] = (char*)ft_calloc(i + 1, sizeof(char));
if (dest == 0) if (dest[index] == 0)
return (0); return (0);
i = 0; i = 0;
while (s[i] != c && s[i]) while (s[i] != c && s[i])
{ {
dest[i] = s[i]; dest[index][i] = s[i];
i++; i++;
} }
dest[index][i] = 0;
return (1); return (1);
} }
void call(char *s, char c, char **dest, int j) static void call(char *s, char c, char **dest, int j)
{ {
int i; int index;
int k; int k;
k = 0; k = 0;
i = 0; index = 0;
while (s[k] == c) while (j > index)
{
while (s[k] == c && s[k])
k++; k++;
while (j > i) if (!s[k])
{
if (!fill_tab(&s[k], c, dest[i]))
{
while (i - 1)
{
free(dest[i - 1]);
i--;
}
free(dest);
return ; return ;
} fill_tab(s + k, c, dest, index);
while (s[k] != c) index++;
while (s[k] != c && s[k])
k++; k++;
k++;
i++;
} }
} }
@ -68,13 +62,10 @@ char **ft_split(char const *s, char c)
i = 0; i = 0;
while (s[i]) while (s[i])
{ {
if (s[i] == c) while (s[i] == c && s[i])
{
if (j)
j++;
while (s[i] == c)
i++; i++;
} j++;
while (s[i] != c && s[i])
i++; i++;
} }
if (!(dest = (char**)malloc(sizeof(char*) * (j + 1)))) if (!(dest = (char**)malloc(sizeof(char*) * (j + 1))))

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:13:52 by apommier #+# #+# */ /* Created: 2020/11/29 00:13:52 by apommier #+# #+# */
/* Updated: 2020/12/12 18:55:00 by apommier ### ########.fr */ /* Updated: 2020/12/12 19:19:34 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,8 +17,14 @@ char *ft_strrchr(const char *s, int c)
char *str; char *str;
char *last; char *last;
last = 0;
str = (char*)s; str = (char*)s;
if(c == 0)
{
while (*str)
str++;
return (str);
}
last = 0;
while (*str) while (*str)
{ {
if (*str == c) if (*str == c)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 23:52:05 by apommier #+# #+# */ /* Created: 2020/11/29 23:52:05 by apommier #+# #+# */
/* Updated: 2020/12/12 10:09:53 by apommier ### ########.fr */ /* Updated: 2020/12/13 18:57:57 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -41,11 +41,13 @@ char *ft_strtrim(char const *s1, char const *set)
while (is_set(set, s1[len - j - 1])) while (is_set(set, s1[len - j - 1]))
j++; j++;
len = len - i - j; len = len - i - j;
dest = malloc(sizeof(char) * (len + 1)); if (len < 0)
if (dest == 0 || s1 == 0 || (size_t)i == ft_strlen(s1)) len = 0;
dest = calloc(len + 1, 1);
if (dest == 0 || s1 == 0)
return (0); return (0);
j = 0; j = 0;
while (s1[i] && len - j) while (s1[i] && len - j && len > 0)
{ {
dest[j] = s1[i]; dest[j] = s1[i];
i++; i++;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 22:54:40 by apommier #+# #+# */ /* Created: 2020/11/29 22:54:40 by apommier #+# #+# */
/* Updated: 2020/11/29 23:28:20 by apommier ### ########.fr */ /* Updated: 2020/12/13 19:24:11 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,13 +15,18 @@
char *ft_substr(char const *s, unsigned int start, size_t len) char *ft_substr(char const *s, unsigned int start, size_t len)
{ {
char *dest; char *dest;
int i; unsigned int i;
i = 0; i = 0;
dest = malloc(sizeof(char) * len); dest = malloc(1 * len);
if (dest == 0) if (dest == 0)
return (0); return (0);
while (len - 1) if (start > ft_strlen(s) || len == 0)
{
dest[i] = 0;
return (dest);
}
while (len && s[i + start])
{ {
dest[i] = s[i + start]; dest[i] = s[i + start];
len--; len--;