This commit is contained in:
Etienne Rey-bethbeder 2022-10-14 22:43:08 +02:00
parent 7f8788c995
commit b891447a0d
15 changed files with 81 additions and 35 deletions

View file

@ -6,7 +6,7 @@
# By: erey-bet <marvin@42.fr> +#+ +:+ +#+ # # By: erey-bet <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 04:19:30 by erey-bet #+# #+# # # Created: 2022/09/27 04:19:30 by erey-bet #+# #+# #
# Updated: 2022/10/12 16:11:27 by erey-bet ### ########.fr # # Updated: 2022/10/14 22:38:25 by erey-bet ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

View file

@ -12,7 +12,7 @@
#include "libft.h" #include "libft.h"
int ft_isspace(char c) static int ft_isspace(char c)
{ {
return (c == ' ' || c == '\f' return (c == ' ' || c == '\f'
||c == '\n' || c == '\r' || c == '\t' || c == '\v'); ||c == '\n' || c == '\r' || c == '\t' || c == '\v');

View file

@ -12,6 +12,19 @@
#include "libft.h" #include "libft.h"
static void ft_itoa_bis(char *str, int len, int i, long nl)
{
while (len > 1)
{
len--;
str[i] = (nl / ft_power(10, len) % 10) + 48;
i++;
}
str[i] = nl % 10 + 48;
i++;
str[i] = '\0';
}
char *ft_itoa(int n) char *ft_itoa(int n)
{ {
char *str; char *str;
@ -19,24 +32,22 @@ char *ft_itoa(int n)
int i; int i;
long nl; long nl;
i = 0;
nl = n; nl = n;
nl *= (n > 0) - (n < 0); nl *= (n > 0) - (n < 0);
len = ft_get_size(nl); len = ft_get_size(nl);
i = 0;
if (n < 0) if (n < 0)
{ {
str = malloc(len + 2); str = malloc(len + 2);
if (str == NULL) if (str == NULL)
return (NULL); return (NULL);
str[i++] = '-'; str[i] = '-';
i++;
} }
else else
str = malloc(len + 1); str = malloc(len + 1);
if (str == NULL) if (str == NULL)
return (NULL); return (NULL);
while (len > 1) ft_itoa_bis(str, len, i, nl);
str[i++] = (nl / ft_power(10, --len) % 10) + 48;
str[i++] = nl % 10 + 48;
str[i] = '\0';
return (str); return (str);
} }

View file

@ -20,10 +20,13 @@ void *ft_memcpy(void *dest, const void *src, size_t s)
if (dest == NULL && src == NULL) if (dest == NULL && src == NULL)
return (NULL); return (NULL);
i = -1; i = 0;
tmp_dest = dest; tmp_dest = dest;
tmp_src = src; tmp_src = src;
while (++i < s) while (i < s)
{
tmp_dest[i] = tmp_src[i]; tmp_dest[i] = tmp_src[i];
i++;
}
return (dest); return (dest);
} }

View file

@ -27,8 +27,11 @@ void *ft_memmove(void *dest, const void *src, size_t s)
else else
{ {
i = s; i = s;
while (i-- > 0) while (i > 0)
{
i--;
d[i] = sc[i]; d[i] = sc[i];
} }
}
return (dest); return (dest);
} }

View file

@ -20,6 +20,9 @@ void *ft_memset(void *ptr, int v, size_t count)
i = 0; i = 0;
tmp = ptr; tmp = ptr;
while (i < count) while (i < count)
tmp[i++] = v; {
tmp[i] = v;
i++;
}
return (ptr); return (ptr);
} }

View file

@ -21,7 +21,10 @@ int ft_power(int nb, int power)
return (0); return (0);
i = 0; i = 0;
new_nb = 1; new_nb = 1;
while (i++ < power) while (i < power)
{
i++;
new_nb *= nb; new_nb *= nb;
}
return (new_nb); return (new_nb);
} }

View file

@ -17,6 +17,9 @@ void ft_putendl_fd(char *s, int fd)
if (s == NULL) if (s == NULL)
return ; return ;
while (*s) while (*s)
ft_putchar_fd(*s++, fd); {
ft_putchar_fd(*s, fd);
s++;
}
ft_putchar_fd('\n', fd); ft_putchar_fd('\n', fd);
} }

View file

@ -23,6 +23,9 @@ void ft_putnbr_fd(int n, int fd)
if (n < 0) if (n < 0)
ft_putchar_fd('-', fd); ft_putchar_fd('-', fd);
while (len > 1) while (len > 1)
ft_putchar_fd(nl / ft_power(10, --len) % 10 + 48, fd); {
--len;
ft_putchar_fd(nl / ft_power(10, len) % 10 + 48, fd);
}
ft_putchar_fd(nl % 10 + 48, fd); ft_putchar_fd(nl % 10 + 48, fd);
} }

View file

@ -17,5 +17,8 @@ void ft_putstr_fd(char *s, int fd)
if (s == NULL) if (s == NULL)
return ; return ;
while (*s) while (*s)
ft_putchar_fd(*s++, fd); {
ft_putchar_fd(*s, fd);
s++;
}
} }

View file

@ -12,7 +12,7 @@
#include "libft.h" #include "libft.h"
int get_len_all(char const *s, char c) static int get_len_all(char const *s, char c)
{ {
int i; int i;
int check; int check;
@ -35,7 +35,7 @@ int get_len_all(char const *s, char c)
return (count); return (count);
} }
int get_len_next(char const *s, char c, int i) static int get_len_next(char const *s, char c, int i)
{ {
int y; int y;
int count; int count;
@ -49,27 +49,30 @@ int get_len_next(char const *s, char c, int i)
return (count); return (count);
} }
void *ft_malloc_split(char **strs, int *iyx, char const *s, char c) static void *ft_malloc_split(char **strs, int *iyx, char const *s, char c)
{ {
iyx[1]++; iyx[1]++;
strs[iyx[1]] = malloc(get_len_next(s, c, iyx[0]) + 1); strs[iyx[1]] = malloc(get_len_next(s, c, iyx[0]) + 1);
if (strs[iyx[1]--] == NULL) if (strs[iyx[1]] == NULL)
{ {
iyx[1]--;
while (iyx[1] >= 0) while (iyx[1] >= 0)
free(strs[iyx[1]--]); {
free(strs[iyx[1]]);
iyx[1]--;
}
free(strs); free(strs);
return (NULL); return (NULL);
} }
iyx[1]++;
return (""); return ("");
} }
char **ft_split_bis(char const *s, char c, char **strs, int *iyx) static char **ft_split_bis(char const *s, char c, char **strs, int *iyx)
{ {
int boo; int boo;
boo = 0; boo = 0;
while (s[++iyx[0]]) while (s[iyx[0]])
{ {
if (s[iyx[0]] != c) if (s[iyx[0]] != c)
{ {
@ -77,7 +80,8 @@ char **ft_split_bis(char const *s, char c, char **strs, int *iyx)
if (ft_malloc_split(strs, iyx, s, c) == NULL) if (ft_malloc_split(strs, iyx, s, c) == NULL)
return (NULL); return (NULL);
boo = 1; boo = 1;
strs[iyx[1]][iyx[2]++] = s[iyx[0]]; strs[iyx[1]][iyx[2]] = s[iyx[0]];
iyx[2]++;
} }
else if (boo == 1) else if (boo == 1)
{ {
@ -85,6 +89,7 @@ char **ft_split_bis(char const *s, char c, char **strs, int *iyx)
iyx[2] = 0; iyx[2] = 0;
boo = 0; boo = 0;
} }
iyx[0]++;
} }
if (boo == 1) if (boo == 1)
strs[iyx[1]][iyx[2]] = '\0'; strs[iyx[1]][iyx[2]] = '\0';
@ -96,7 +101,7 @@ char **ft_split(char const *s, char c)
char **strs; char **strs;
int iyx[3]; int iyx[3];
iyx[0] = -1; iyx[0] = 0;
iyx[1] = -1; iyx[1] = -1;
iyx[2] = 0; iyx[2] = 0;
if (s == NULL) if (s == NULL)
@ -106,6 +111,7 @@ char **ft_split(char const *s, char c)
return (NULL); return (NULL);
if (ft_split_bis(s, c, strs, iyx) == NULL) if (ft_split_bis(s, c, strs, iyx) == NULL)
return (NULL); return (NULL);
strs[++iyx[1]] = NULL; iyx[1]++;
strs[iyx[1]] = NULL;
return (strs); return (strs);
} }

View file

@ -18,7 +18,10 @@ void ft_striteri(char *s, void (*f)(unsigned int, char*))
if (f == NULL || s == NULL) if (f == NULL || s == NULL)
return ; return ;
i = -1; i = 0;
while (s[++i]) while (s[i])
{
f(i, &s[i]); f(i, &s[i]);
i++;
}
} }

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 13:02:39 by erey-bet #+# #+# */ /* Created: 2022/07/19 13:02:39 by erey-bet #+# #+# */
/* Updated: 2022/10/05 14:37:05 by erey-bet ### ########.fr */ /* Updated: 2022/10/14 17:49:58 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,13 @@
size_t ft_strlcat(char *dest, const char *src, size_t size) size_t ft_strlcat(char *dest, const char *src, size_t size)
{ {
size_t len_dest; size_t len_dest;
size_t len_src;
if (size == 0) if (size == 0)
return (ft_strlen(src)); return (ft_strlen(src));
len_dest = ft_strlen(dest); len_dest = ft_strlen(dest);
if (len_dest >= size) if (len_dest >= size)
return (ft_strlen(src) + (size)); return (ft_strlen(src) + (size));
return (len_dest + ft_strlcpy(dest + len_dest, src, size - len_dest)); len_src = ft_strlcpy(dest + len_dest, src, size - len_dest);
return (len_dest + len_src);
} }

View file

@ -17,13 +17,16 @@ char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
unsigned int i; unsigned int i;
char *str; char *str;
i = -1;
if (s == NULL || f == NULL) if (s == NULL || f == NULL)
return (NULL); return (NULL);
str = ft_strdup(s); str = ft_strdup(s);
if (str == NULL) if (str == NULL)
return (NULL); return (NULL);
while (s[++i]) i = 0;
while (s[i])
{
str[i] = f(i, s[i]); str[i] = f(i, s[i]);
i++;
}
return (str); return (str);
} }

View file

@ -12,7 +12,7 @@
#include "libft.h" #include "libft.h"
int get_start(const char *str, char const *set) static int get_start(const char *str, char const *set)
{ {
int i; int i;
int y; int y;
@ -33,7 +33,7 @@ int get_start(const char *str, char const *set)
return (0); return (0);
} }
int get_end(const char *str, char const *set) static int get_end(const char *str, char const *set)
{ {
int i; int i;
int y; int y;