🔨」 fix(libft): replaced libft by a better one :D

This commit is contained in:
y-syo
2024-04-29 13:53:00 +02:00
parent 77786466e3
commit 73bb992b01
77 changed files with 1511 additions and 1320 deletions

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:00:43 by mmoussou #+# #+# */
/* Updated: 2024/04/10 12:31:38 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}

View File

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 12:35:36 by mmoussou #+# #+# */
/* Updated: 2024/04/10 12:30:54 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isalpha(int c)
{
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}

View File

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:00:55 by mmoussou #+# #+# */
/* Updated: 2024/04/10 12:31:24 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isascii(int c)
{
return (c >= 0 && c <= 127);
}

View File

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:01:07 by mmoussou #+# #+# */
/* Updated: 2024/04/10 12:31:42 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}

View File

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:01:15 by mmoussou #+# #+# */
/* Updated: 2024/04/10 12:31:47 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
return (c >= 32 && c <= 126);
}

18
libft/src/int/ft_abs.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_abs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/10 10:49:03 by mmoussou #+# #+# */
/* Updated: 2024/04/10 10:57:38 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
int ft_abs(int n)
{
if (n < 0)
return (-n);
return (n);
}

18
libft/src/int/ft_max.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_max.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 01:02:38 by mmoussou #+# #+# */
/* Updated: 2024/04/10 12:32:15 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
int ft_max(int a, int b)
{
if (a > b)
return (a);
return (b);
}

18
libft/src/int/ft_min.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_min.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 01:02:38 by mmoussou #+# #+# */
/* Updated: 2024/04/10 12:32:11 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
int ft_min(int a, int b)
{
if (a < b)
return (a);
return (b);
}

83
libft/src/io/ft_printf.c Normal file
View File

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 16:46:39 by mmoussou #+# #+# */
/* Updated: 2024/02/05 14:14:59 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_putptr_fd(unsigned long long value, int fd)
{
if (!value)
return (write(1, "(nil)", 5));
write(1, "0x", 2);
return (ft_putuhex_fd(value, "0123456789abcdef", fd) + 2);
}
static int ft_putstack_fd(t_stack *stack, int fd)
{
if (stack)
{
return (ft_printf("nb_init:%d | nb:%d\n", stack->nb_init, stack->nb)
+ ft_putstack_fd(stack->next, fd));
}
return (0);
}
static void print_arg(va_list argsl, char type, size_t *l)
{
if (type == 'c')
*l += ft_putchar_fd(va_arg(argsl, int), 1);
else if (type == 's')
*l += ft_putstr_fd(va_arg(argsl, char *), 1);
else if (type == 'p')
*l += ft_putptr_fd(va_arg(argsl, unsigned long long), 1);
else if (type == 'd' || type == 'i')
*l += ft_putnbr_fd(va_arg(argsl, int), 1);
else if (type == 'u')
*l += ft_putnbr_fd(va_arg(argsl, unsigned int), 1);
else if (type == 'x')
*l += ft_putuhex_fd(va_arg(argsl, unsigned int), "0123456789abcdef", 1);
else if (type == 'X')
*l += ft_putuhex_fd(va_arg(argsl, unsigned int), "0123456789ABCDEF", 1);
else if (type == 'S')
*l += ft_putstack_fd(va_arg(argsl, t_stack *), 1);
else if (type)
{
*l += ft_putchar_fd('%', 1);
if (type != '%')
*l += ft_putchar_fd(type, 1);
}
else
*l = -1;
}
int ft_printf(const char *str, ...)
{
va_list argsl;
size_t i;
size_t l;
if (!str)
return (-1);
va_start(argsl, str);
i = 0;
l = 0;
while (str[i])
{
if (str[i] == '%')
print_arg(argsl, str[++i], &l);
else
l += ft_putchar_fd(str[i], 1);
if (str[i])
i++;
}
va_end(argsl);
return (l);
}

View File

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 16:46:39 by mmoussou #+# #+# */
/* Updated: 2024/02/05 14:17:15 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_putptr_fd(unsigned long long value, int fd)
{
if (!value)
return (write(1, "(nil)", 5));
write(1, "0x", 2);
return (ft_putuhex_fd(value, "0123456789abcdef", fd) + 2);
}
static int ft_putstack_fd(t_stack *s, int fd)
{
if (s)
{
return (ft_printf_fd(fd, "nb_init:%d | nb:%d\n", s->nb_init, s->nb)
+ ft_putstack_fd(s->next, fd));
}
return (0);
}
static void print_arg_fd(va_list argsl, char type, size_t *l, int fd)
{
if (type == 'c')
*l += ft_putchar_fd(va_arg(argsl, int), fd);
else if (type == 's')
*l += ft_putstr_fd(va_arg(argsl, char *), fd);
else if (type == 'p')
*l += ft_putptr_fd(va_arg(argsl, unsigned long long), fd);
else if (type == 'd' || type == 'i')
*l += ft_putnbr_fd(va_arg(argsl, int), fd);
else if (type == 'u')
*l += ft_putnbr_fd(va_arg(argsl, uint32_t), fd);
else if (type == 'x')
*l += ft_putuhex_fd(va_arg(argsl, uint32_t), "0123456789abcdef", fd);
else if (type == 'X')
*l += ft_putuhex_fd(va_arg(argsl, uint32_t), "0123456789ABCDEF", fd);
else if (type == 'S')
*l += ft_putstack_fd(va_arg(argsl, t_stack *), fd);
else if (type)
{
*l += ft_putchar_fd('%', 1);
if (type != '%')
*l += ft_putchar_fd(type, 1);
}
else
*l = -1;
}
int ft_printf_fd(int fd, const char *str, ...)
{
va_list argsl;
size_t i;
size_t l;
if (!str)
return (-1);
va_start(argsl, str);
i = 0;
l = 0;
while (str[i])
{
if (str[i] == '%')
print_arg_fd(argsl, str[++i], &l, fd);
else
l += ft_putchar_fd(str[i], fd);
if (str[i])
i++;
}
va_end(argsl);
return (l);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 14:31:22 by mmoussou #+# #+# */
/* Updated: 2024/01/03 21:50:11 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_putchar_fd(char c, int fd)
{
return (write(fd, &c, 1));
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 14:37:21 by mmoussou #+# #+# */
/* Updated: 2024/01/03 21:51:46 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_putendl_fd(char *s, int fd)
{
int l;
l = ft_putstr_fd(s, fd);
l += ft_putchar_fd('\n', fd);
return (l);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 14:38:33 by mmoussou #+# #+# */
/* Updated: 2024/01/03 21:53:28 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_putnbr_fd(int n, int fd)
{
if (n == -2147483648)
return (ft_putstr_fd("-2147483648", fd));
if (n < 0)
{
ft_putchar_fd('-', fd);
return (ft_putnbr_fd(-n, fd) + 1);
}
if (n < 10)
return (ft_putchar_fd(n + '0', fd));
return (ft_putnbr_fd(n / 10, fd) + ft_putchar_fd((n % 10) + '0', fd));
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 14:33:48 by mmoussou #+# #+# */
/* Updated: 2024/01/03 22:00:20 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_putstr_fd(char *s, int fd)
{
if (!s)
return (write(1, "(null)", 6));
return (write(fd, s, ft_strlen(s)));
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putuhex_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 21:48:12 by mmoussou #+# #+# */
/* Updated: 2024/01/03 22:02:08 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_putuhex_fd(unsigned long long nbr, char *base, int fd)
{
if (nbr < 16)
return (ft_putchar_fd(base[nbr], 1));
else
{
return (ft_putuhex_fd(nbr / 16, base, fd)
+ ft_putchar_fd(base[nbr % 16], 1));
}
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 17:42:57 by mmoussou #+# #+# */
/* Updated: 2023/11/08 20:39:15 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *tmp;
if (!lst || !new)
return ;
if (!*lst)
{
*lst = new;
return ;
}
tmp = *lst;
tmp = ft_lstlast(*lst);
tmp->next = new;
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 17:03:15 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:31:32 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
t_list *tmp;
tmp = *lst;
*lst = new;
if (!new || !lst)
return ;
while (new->next)
new = new->next;
new->next = tmp;
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 21:44:04 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:31:49 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *tmp;
if (!lst || !del)
return ;
while (*lst)
{
tmp = (*lst)->next;
ft_lstdelone(*lst, del);
*lst = tmp;
}
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 20:40:46 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:32:04 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
del(lst->content);
free(lst);
lst = NULL;
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yosyo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 12:49:19 by yosyo #+# #+# */
/* Updated: 2023/11/13 19:32:17 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!lst || !f)
return ;
while (lst->next)
{
f(lst->content);
lst = lst->next;
}
f(lst->content);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 17:38:47 by mmoussou #+# #+# */
/* Updated: 2023/11/08 20:09:47 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yosyo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 12:53:23 by yosyo #+# #+# */
/* Updated: 2023/11/13 19:33:20 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *r;
t_list *tmp;
r = NULL;
while (lst && f && del)
{
tmp = ft_lstnew(f(lst->content));
if (!tmp)
{
ft_lstclear(&tmp, del);
return (NULL);
}
ft_lstadd_back(&r, tmp);
lst = lst->next;
}
return (r);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:02:40 by mmoussou #+# #+# */
/* Updated: 2023/11/08 17:02:57 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *r;
r = malloc(sizeof(t_list));
if (!r)
return (NULL);
r->content = content;
r->next = NULL;
return (r);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 17:33:51 by mmoussou #+# #+# */
/* Updated: 2024/04/11 14:33:42 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
uint ft_lstsize(t_list *lst)
{
uint i;
i = 1;
if (!lst)
return (0);
while (lst->next)
{
lst = lst->next;
i++;
}
return (i);
}

18
libft/src/mem/ft_bzero.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:00:28 by mmoussou #+# #+# */
/* Updated: 2023/11/01 22:00:32 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
ft_memset(s, 0, n);
}

30
libft/src/mem/ft_calloc.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:55:05 by mmoussou #+# #+# */
/* Updated: 2023/11/06 13:58:47 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t n, size_t elsize)
{
void *t;
size_t size;
if (n <= 0 || elsize <= 0)
return (malloc(0));
if ((elsize * n / n) != elsize)
return (NULL);
size = n * elsize;
t = malloc(size);
if (!t)
return (NULL);
ft_bzero(t, size);
return (t);
}

72
libft/src/mem/ft_free.c Normal file
View File

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 19:27:31 by mmoussou #+# #+# */
/* Updated: 2024/01/18 01:05:44 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void free_str(char **str)
{
free(*str);
*str = NULL;
}
void free_stack(t_stack **stack)
{
if (*stack && (*stack)->next)
free_stack(&(*stack)->next);
free(*stack);
*stack = NULL;
}
void free_list(t_list **lst)
{
if (*lst && (*lst)->next)
free_list(&(*lst)->next);
free(*lst);
*lst = NULL;
}
void free_tab(char ***arr)
{
int i;
i = 0;
while ((*arr)[i])
{
free((*arr)[i]);
i++;
}
free(*arr);
*arr = NULL;
}
void ft_free(const char *str, ...)
{
va_list args;
size_t i;
if (!str)
return ;
va_start(args, str);
i = 0;
while (str[i])
{
if (str[i] == 'c')
free_str(va_arg(args, char **));
if (str[i] == 's')
free_stack(va_arg(args, t_stack **));
if (str[i] == 'l')
free_list(va_arg(args, t_list **));
if (str[i] == 'a')
free_tab(va_arg(args, char ***));
i++;
}
}

31
libft/src/mem/ft_memchr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:52:59 by mmoussou #+# #+# */
/* Updated: 2023/11/10 17:01:28 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
size_t i;
if (!s || !n)
return (NULL);
i = 0;
while (i < n)
{
if (((unsigned char *)s)[i] == (unsigned char)c)
return ((void *)s + i);
i++;
}
if ((unsigned char)c == 0)
return ((void *)s + i);
return (NULL);
}

33
libft/src/mem/ft_memcmp.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:57:12 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:47:03 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *ptr1;
unsigned char *ptr2;
size_t i;
i = 0;
if (!n)
return (0);
ptr1 = (unsigned char *) s1;
ptr2 = (unsigned char *) s2;
while (i < (n - 1) && *ptr1 == *ptr2)
{
ptr1++;
ptr2++;
i++;
}
return (*ptr1 - *ptr2);
}

33
libft/src/mem/ft_memcpy.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:02:34 by mmoussou #+# #+# */
/* Updated: 2023/11/01 22:08:52 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
char *ptr1;
char *ptr2;
size_t i;
i = 0;
if (dst && src)
{
ptr1 = (char *)dst;
ptr2 = (char *)src;
while (i < n)
{
ptr1[i] = ptr2[i];
i++;
}
}
return (dst);
}

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:58:13 by mmoussou #+# #+# */
/* Updated: 2023/11/11 11:43:35 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dst, const void *src, size_t n)
{
char *ptr1;
char *ptr2;
size_t i;
i = 0;
if (dst && src)
{
ptr1 = (char *)dst;
ptr2 = (char *)src;
if (ptr1 > ptr2)
while (n--)
ptr1[n] = ptr2[n];
else
{
while (i < n)
{
ptr1[i] = ptr2[i];
i++;
}
}
}
return (dst);
}

31
libft/src/mem/ft_memset.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:03:03 by mmoussou #+# #+# */
/* Updated: 2023/11/01 22:03:04 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
char *ptr;
size_t i;
i = 0;
if (s)
{
ptr = (char *)s;
while (i < n)
{
ptr[i] = c;
i++;
}
}
return (s);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_stackadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 00:36:32 by mmoussou #+# #+# */
/* Updated: 2024/01/18 00:39:38 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_stackadd_back(t_stack **stack, t_stack *new)
{
t_stack *tmp;
if (!stack || !new)
return ;
if (!*stack)
{
*stack = new;
return ;
}
tmp = *stack;
tmp = ft_stacklast(*stack);
tmp->next = new;
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_stackadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 00:36:50 by mmoussou #+# #+# */
/* Updated: 2024/01/18 00:37:05 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_stackadd_front(t_stack **stack, t_stack *new)
{
t_stack *tmp;
tmp = *stack;
*stack = new;
if (!new || !stack)
return ;
while (new->next)
new = new->next;
new->next = tmp;
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_stacklast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 00:36:13 by mmoussou #+# #+# */
/* Updated: 2024/01/18 00:36:15 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_stack *ft_stacklast(t_stack *stack)
{
if (stack)
{
while (stack->next)
stack = stack->next;
return (stack);
}
return (NULL);
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_stacknew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 00:35:18 by mmoussou #+# #+# */
/* Updated: 2024/01/18 00:35:30 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_stack *ft_stacknew(int content)
{
t_stack *new_element;
new_element = malloc(sizeof(t_stack));
if (!new_element)
return (NULL);
new_element->nb_init = content;
new_element->nb = -1;
new_element->next = NULL;
return (new_element);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_stacksize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 00:35:55 by mmoussou #+# #+# */
/* Updated: 2024/04/11 14:35:24 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
uint ft_stacksize(t_stack *stack)
{
uint i;
i = 0;
if (stack)
{
i++;
while (stack->next)
{
stack = stack->next;
i++;
}
}
return (i);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:00:18 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:29:30 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int r;
int i;
int s;
r = 0;
i = 0;
s = 1;
if (!str)
return (0);
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
s *= -1;
i++;
}
while (str[i] && (str[i] >= '0' && str[i] <= '9'))
{
r = (r * 10) + (str[i] - '0');
i++;
}
return (r * s);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:00:18 by mmoussou #+# #+# */
/* Updated: 2024/01/18 00:39:13 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
long long ft_atol(const char *str)
{
long long r;
int i;
char s;
r = 0;
i = 0;
s = 1;
if (!str)
return (0);
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
s *= -1;
i++;
}
while (str[i] && (str[i] >= '0' && str[i] <= '9'))
{
r = (r * 10) + (str[i] - '0');
i++;
}
return (r * s);
}

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 12:45:53 by mmoussou #+# #+# */
/* Updated: 2023/11/11 11:41:53 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int n_size(int n)
{
int s;
s = 0;
if (n <= 0)
{
s++;
n *= -1;
}
while (n >= 1)
{
s++;
n /= 10;
}
return (s);
}
static size_t pwten(int n)
{
int i;
i = 1;
while (n)
{
i *= 10;
n--;
}
return (i);
}
static char *itoa_rec(char *r, int n)
{
int i;
if (n > 9)
{
r[0] = n / (pwten(n_size(n) - 1)) + '0';
i = 1;
while (n_size(n) - (n_size(n % pwten(n_size(n) - 1))) != i)
{
r[i] = '0';
i++;
}
itoa_rec(&r[n_size(n) - (n_size(n % pwten(n_size(n) - 1)))], \
n % pwten(n_size(n) - 1));
return (r);
}
r[0] = n + '0';
return (r);
}
char *ft_itoa(int n)
{
char *r;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
r = ft_calloc(sizeof(char), n_size(n) + 1);
if (!r)
return (NULL);
if (n == 0)
{
r[0] = '0';
return (r);
}
if (n < 0)
{
r[0] = '-';
itoa_rec(&r[1], n * -1);
return (r);
}
itoa_rec(r, n);
return (r);
}

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 12:45:53 by mmoussou #+# #+# */
/* Updated: 2024/01/12 00:52:12 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int n_size(int n)
{
int s;
s = 0;
if (n <= 0)
{
s++;
n *= -1;
}
while (n >= 1)
{
s++;
n /= 10;
}
return (s);
}
static size_t pwten(int n)
{
int i;
i = 1;
while (n)
{
i *= 10;
n--;
}
return (i);
}
static char *itoa_rec(char *r, int n)
{
int i;
if (n > 9)
{
r[0] = n / (pwten(n_size(n) - 1)) + '0';
i = 1;
while (n_size(n) - (n_size(n % pwten(n_size(n) - 1))) != i)
{
r[i] = '0';
i++;
}
itoa_rec(&r[n_size(n) - (n_size(n % pwten(n_size(n) - 1)))], \
n % pwten(n_size(n) - 1));
return (r);
}
r[0] = n + '0';
return (r);
}
char *ft_ltoa(long long n)
{
char *r;
if (n == -9223372036854775807)
return (ft_strdup("-9223372036854775807"));
r = ft_calloc(sizeof(char), n_size(n) + 1);
if (!r)
return (NULL);
if (n == 0)
{
r[0] = '0';
return (r);
}
if (n < 0)
{
r[0] = '-';
itoa_rec(&r[1], n * -1);
return (r);
}
itoa_rec(r, n);
return (r);
}

119
libft/src/str/ft_split.c Normal file
View File

@ -0,0 +1,119 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 17:59:11 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:39:13 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int free_list(char **result, int j)
{
while (j)
{
free(result[j]);
j--;
}
return (1);
}
static int word_counter(const char *str, char ch)
{
int i;
int c;
int bool_c;
i = 0;
c = 0;
bool_c = 1;
while (str[i])
{
if (str[i] == ch)
{
bool_c = 1;
}
else if (str[i] != ch && bool_c)
{
c++;
bool_c = 0;
}
i++;
}
return (c);
}
static int per_word_fill(char **result, const char *str, char ch)
{
int i;
int j;
int c;
i = 0;
j = 0;
while (str[i])
{
c = 0;
while (str[i] != ch && str[i])
{
c++;
i++;
}
if (c != 0)
{
result[j] = ft_calloc(sizeof(char), c + 1);
if (result[j++] == NULL)
return (free_list(result, j - 2));
}
if (str[i])
i++;
}
result[j] = NULL;
return (0);
}
static void ft_split_resolver(char **result, const char *str, char ch)
{
int i;
int j;
int wi;
int bool_w;
i = 0;
j = 0;
wi = 0;
bool_w = 0;
while (str[i])
{
if (str[i] == ch && bool_w)
{
bool_w = 0;
wi = 0;
j++;
}
else if (str[i] != ch)
{
bool_w = 1;
result[j][wi] = str[i];
wi++;
}
i++;
}
}
char **ft_split(const char *str, char c)
{
char **result;
result = malloc(sizeof(char *) * (word_counter(str, c) + 1));
if (result == NULL)
return (NULL);
if (per_word_fill(result, str, c))
return (NULL);
ft_split_resolver(result, str, c);
return (result);
}

31
libft/src/str/ft_strchr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:01:50 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:39:47 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
size_t i;
i = 0;
if (!s)
return (NULL);
while (s[i])
{
if (s[i] == (unsigned char)c)
return (&((char *)s)[i]);
i++;
}
if (!c)
return (&((char *)s)[i]);
return (NULL);
}

27
libft/src/str/ft_strcmp.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:03:09 by mmoussou #+# #+# */
/* Updated: 2024/01/11 18:03:15 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strcmp(const char *s1, const char *s2)
{
unsigned int i;
i = 0;
while ((s1[i] || s2[i]))
{
if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
i++;
}
return (0);
}

33
libft/src/str/ft_strdup.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:02:20 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:41:29 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(char *src)
{
char *rst;
int i;
i = 0;
if (!src)
return (NULL);
rst = malloc(sizeof(char) * (ft_strlen(src) + 1));
if (!rst)
return (NULL);
while (src[i])
{
rst[i] = src[i];
i++;
}
rst[i] = 0;
return (rst);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 13:59:56 by mmoussou #+# #+# */
/* Updated: 2023/11/11 11:45:06 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
size_t i;
if (!s || !f)
return ;
i = 0;
while (s[i])
{
f(i, s + i);
i++;
}
}

View File

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 19:35:01 by mmoussou #+# #+# */
/* Updated: 2024/02/26 03:07:16 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(const char *s1, const char *s2)
{
char *fstr;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
fstr = ft_calloc(sizeof(char), ft_strlen(s1) + ft_strlen(s2) + 1);
if (!fstr)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
return (fstr);
}
char *ft_strjoin_free_s1(char *s1, const char *s2)
{
char *fstr;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
fstr = ft_calloc(sizeof(char), ft_strlen(s1) + ft_strlen(s2) + 1);
if (!fstr)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
if (s1[0])
free(s1);
return (fstr);
}
char *ft_strjoin_free_s2(const char *s1, char *s2)
{
char *fstr;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
fstr = ft_calloc(sizeof(char), ft_strlen(s1) + ft_strlen(s2) + 1);
if (!fstr)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
free(s2);
return (fstr);
}
char *ft_strjoin_free(char *s1, char *s2)
{
char *fstr;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
fstr = ft_calloc(sizeof(char), ft_strlen(s1) + ft_strlen(s2) + 1);
if (!fstr)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
free(s1);
free(s2);
return (fstr);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:02:48 by mmoussou #+# #+# */
/* Updated: 2023/11/11 13:27:31 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t dl;
if (!size)
return (ft_strlen(src));
dl = ft_strlen(dst);
if (size <= dl)
return (size + ft_strlen(src));
return (dl + ft_strlcpy(dst + dl, src, size - dl));
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:02:43 by mmoussou #+# #+# */
/* Updated: 2023/11/03 16:25:20 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
while (i + 1 < size && src[i])
{
dst[i] = src[i];
i++;
}
if (i < size)
dst[i] = 0;
while (src[i])
i++;
return (i);
}

21
libft/src/str/ft_strlen.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 12:42:36 by mmoussou #+# #+# */
/* Updated: 2024/04/16 18:09:42 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
uint ft_strlen(const char *s)
{
const char *endptr;
endptr = s;
while (*endptr)
endptr++;
return (endptr - s);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 13:53:45 by mmoussou #+# #+# */
/* Updated: 2023/11/07 15:36:32 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(const char *s, char (*f)(unsigned int, char))
{
char *r;
int i;
if (!s || !f)
return ((char *)s);
r = ft_calloc(sizeof(char), ft_strlen(s) + 1);
if (!r)
return (NULL);
i = 0;
while (s[i])
{
r[i] = (*f)(i, s[i]);
i++;
}
return (r);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:03:09 by mmoussou #+# #+# */
/* Updated: 2023/11/11 13:19:18 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, unsigned int n)
{
unsigned int i;
i = 0;
while ((s1[i] || s2[i]) && i < n)
{
if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
i++;
}
return (0);
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:55:45 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:46:01 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
if (!little || !little[0] || big == little)
return ((char *)big);
if (!len || !big)
return (NULL);
while (i < len && big[i])
{
j = 0;
while (i + j < len && big[i + j] == little[j])
j++;
if (j == (size_t)ft_strlen(little))
return (&((char *)big)[i]);
i++;
}
return (NULL);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:57:57 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:46:30 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
size_t i;
if (!s)
return (NULL);
i = ft_strlen(s);
if (!c)
return (&((char *)s)[i]);
while (i + 1)
{
if (s[i] == (unsigned char)c)
return (&((char *)s)[i]);
i--;
}
return (NULL);
}

View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 13:39:05 by mmoussou #+# #+# */
/* Updated: 2023/11/12 00:25:11 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_charset(char c, char const *set)
{
size_t i;
i = 0;
while (set[i])
{
if (c == set[i])
return (1);
i++;
}
return (0);
}
char *ft_strtrim(const char *s1, const char *set)
{
size_t o;
int i;
char *r;
if (!s1 || !set)
return ((char *)s1);
o = 0;
while (s1[o] && is_charset(s1[o], set))
o++;
i = ft_strlen(s1) - o - 1;
while (i > 0 && is_charset(s1[o + i], set))
i--;
if (o == ft_strlen(s1) - 1)
return (malloc(0));
r = ft_calloc(sizeof(char), i + 2);
if (!r)
return (NULL);
while (i + 1)
{
r[i] = s1[o + i];
i--;
}
return (r);
}

39
libft/src/str/ft_substr.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 17:46:28 by mmoussou #+# #+# */
/* Updated: 2023/11/11 11:45:52 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t l;
size_t nl;
char *r;
if (!s)
return (NULL);
l = ft_strlen(s);
if (start > l)
{
r = ft_calloc(1, 1);
return (r);
}
nl = ft_strlen(s + start);
if (nl > len)
nl = len;
r = malloc((nl + 1) * sizeof(char));
if (!r)
return (NULL);
r[nl] = 0;
while (nl--)
r[nl] = s[start + nl];
return (r);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:56:42 by mmoussou #+# #+# */
/* Updated: 2023/11/01 21:56:43 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
return (c + ('a' - 'A'));
return (c);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:58:40 by mmoussou #+# #+# */
/* Updated: 2023/11/01 21:58:55 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
return (c - ('a' - 'A'));
return (c);
}