narnaud
3 years ago
45 changed files with 1458 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||||
|
NAME = libft.a |
||||
|
|
||||
|
SRCS = ft_isalpha.c ft_isdigit.c ft_isascii.c ft_isprint.c ft_isalnum.c \
|
||||
|
ft_strlen.c ft_strlcpy.c ft_strlcat.c ft_strchr.c ft_strrchr.c ft_strncmp.c \
|
||||
|
ft_strnstr.c ft_strdup.c ft_toupper.c ft_tolower.c ft_atoi.c ft_bzero.c \
|
||||
|
ft_memset.c ft_memchr.c ft_memcpy.c ft_memcmp.c ft_memmove.c ft_calloc.c \
|
||||
|
ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c \
|
||||
|
ft_striteri.c ft_putchar_fd.c ft_putnbr_fd.c ft_putendl_fd.c ft_putstr_fd.c |
||||
|
|
||||
|
SRCS_BONUS = ft_lstsize_bonus.c ft_lstnew_bonus.c ft_lstlast_bonus.c \
|
||||
|
ft_lstadd_back_bonus.c ft_lstadd_front_bonus.c ft_lstdelone_bonus.c \
|
||||
|
ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c |
||||
|
|
||||
|
OBJS = $(SRCS:.c=.o) |
||||
|
OBJS_BONUS = $(SRCS_BONUS:.c=.o) |
||||
|
|
||||
|
CC = gcc |
||||
|
AR = ar -rcs |
||||
|
RM = rm -rf |
||||
|
|
||||
|
CFLAGS = -Wall -Wextra -Werror |
||||
|
|
||||
|
.c.o: |
||||
|
${CC} ${CFLAGS} -c $< -o ${<:.c=.o} |
||||
|
|
||||
|
all : $(NAME) |
||||
|
|
||||
|
$(NAME): $(OBJS) |
||||
|
${AR} ${NAME} ${OBJS} |
||||
|
|
||||
|
|
||||
|
|
||||
|
bonus: $(OBJS) $(OBJS_BONUS) |
||||
|
${AR} ${NAME} ${OBJS} ${OBJS_BONUS} |
||||
|
|
||||
|
clean: |
||||
|
${RM} ${OBJS} |
||||
|
${RM} ${OBJS_BONUS} |
||||
|
|
||||
|
fclean: clean |
||||
|
${RM} ${NAME} |
||||
|
|
||||
|
re: fclean all |
||||
|
|
||||
|
rebonus: fclean bonus |
||||
|
|
||||
|
.PHONY: all bonus re rebonus clean fclean |
@ -0,0 +1,38 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_atoi.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 17:06:25 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 10:01:15 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_atoi(const char *str) |
||||
|
{ |
||||
|
int sign; |
||||
|
int nb; |
||||
|
|
||||
|
nb = 0; |
||||
|
sign = 1; |
||||
|
while (*str == ' ' || (*str >= '\t' && *str <= '\r')) |
||||
|
str++; |
||||
|
if (*str == '-' && ++str) |
||||
|
sign = -1; |
||||
|
else if (*str == '+') |
||||
|
str++; |
||||
|
while (*str >= '0' && *str <= '9') |
||||
|
{ |
||||
|
if (nb < 0 && sign > 0) |
||||
|
return (-1); |
||||
|
else if (nb < 0 && sign < 0) |
||||
|
return (0); |
||||
|
nb = nb * 10 + (*str - '0') % 10; |
||||
|
str++; |
||||
|
} |
||||
|
return ((int)(sign * nb)); |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_bzero.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/19 11:46:58 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/02 16:57:06 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_bzero(void *s, size_t n) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
while (i < n) |
||||
|
*(char *)(s + i++) = 0; |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_calloc.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/19 14:11:11 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/02 16:58:09 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void *ft_calloc(size_t count, size_t size) |
||||
|
{ |
||||
|
void *ret; |
||||
|
|
||||
|
ret = malloc(count * size); |
||||
|
if (!ret) |
||||
|
return (NULL); |
||||
|
ft_bzero(ret, count * size); |
||||
|
return (ret); |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_isalnum.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:13:39 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/22 15:13:55 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_isalnum(int ch) |
||||
|
{ |
||||
|
if (ft_isalpha(ch) || ft_isdigit(ch)) |
||||
|
return (1); |
||||
|
else |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_isalpha.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 15:38:41 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/23 13:26:03 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_isalpha(int ch) |
||||
|
{ |
||||
|
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) |
||||
|
return (1); |
||||
|
else |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_isascii.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:14:38 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/02 17:20:47 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_isascii(int ch) |
||||
|
{ |
||||
|
if (ch >= 0 && ch < 128) |
||||
|
return (1); |
||||
|
else |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_isdigit.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:13:16 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/02 17:04:02 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_isdigit(int ch) |
||||
|
{ |
||||
|
if (ch >= '0' && ch <= '9') |
||||
|
return (1); |
||||
|
else |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_isprint.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:14:02 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/02 17:03:59 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_isprint(int ch) |
||||
|
{ |
||||
|
if (ch > 31 && ch < 127) |
||||
|
return (1); |
||||
|
else |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,75 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_itoa.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 15:50:47 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 12:10:47 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
static void ft_strrev(char **str, unsigned int neg) |
||||
|
{ |
||||
|
char ch; |
||||
|
size_t size; |
||||
|
size_t i; |
||||
|
|
||||
|
size = ft_strlen(*str); |
||||
|
if (neg) |
||||
|
(*str)[size++] = '-'; |
||||
|
i = 0; |
||||
|
while (i < size / 2) |
||||
|
{ |
||||
|
ch = (*str)[i]; |
||||
|
(*str)[i] = (*str)[size - i - 1]; |
||||
|
(*str)[size - i - 1] = ch; |
||||
|
i++; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static size_t get_nbrlength(int nbr) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
if (nbr == 0) |
||||
|
return (1); |
||||
|
else if (nbr < 0) |
||||
|
i++; |
||||
|
while (nbr && ++i) |
||||
|
nbr = nbr / 10; |
||||
|
return (i); |
||||
|
} |
||||
|
|
||||
|
char *ft_itoa(int n) |
||||
|
{ |
||||
|
char *ret; |
||||
|
unsigned int i; |
||||
|
unsigned int neg; |
||||
|
|
||||
|
if (n == -2147483648) |
||||
|
return (ft_strdup("-2147483648")); |
||||
|
if (n == 0) |
||||
|
return (ft_strdup("0")); |
||||
|
i = 0; |
||||
|
neg = 0; |
||||
|
ret = ft_calloc(get_nbrlength(n) + 1, sizeof(char)); |
||||
|
if (!ret) |
||||
|
return (NULL); |
||||
|
if (n < 0) |
||||
|
{ |
||||
|
n = -n; |
||||
|
neg = 1; |
||||
|
} |
||||
|
while (n) |
||||
|
{ |
||||
|
ret[i++] = (n % 10) + '0'; |
||||
|
n = n / 10; |
||||
|
} |
||||
|
ft_strrev(&ret, neg); |
||||
|
return (ret); |
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstadd_back_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 14:51:11 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:07:12 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_lstadd_back(t_list **alst, t_list *new) |
||||
|
{ |
||||
|
t_list *i_cell; |
||||
|
|
||||
|
if (!alst || !new) |
||||
|
return ; |
||||
|
if (*alst) |
||||
|
{ |
||||
|
i_cell = ft_lstlast(*alst); |
||||
|
i_cell->next = new; |
||||
|
} |
||||
|
else |
||||
|
*alst = new; |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstadd_front_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 14:48:11 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:07:10 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_lstadd_front(t_list **alst, t_list *new) |
||||
|
{ |
||||
|
if (!alst || !new) |
||||
|
return ; |
||||
|
if (*alst) |
||||
|
new->next = *alst; |
||||
|
*alst = new; |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstclear_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 14:53:04 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:07:08 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_lstclear(t_list **lst, void (*del)(void *)) |
||||
|
{ |
||||
|
t_list *next_cell; |
||||
|
|
||||
|
if (!del) |
||||
|
return ; |
||||
|
while (lst && *lst) |
||||
|
{ |
||||
|
next_cell = (*lst)->next; |
||||
|
ft_lstdelone(*lst, del); |
||||
|
*lst = next_cell; |
||||
|
} |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstdelone_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 14:51:53 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:07:06 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_lstdelone(t_list *lst, void (*del)(void *)) |
||||
|
{ |
||||
|
if (!lst || !del) |
||||
|
return ; |
||||
|
(*del)(lst->content); |
||||
|
free(lst); |
||||
|
lst = NULL; |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstiter_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 14:54:03 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:07:04 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_lstiter(t_list *lst, void (*f)(void *)) |
||||
|
{ |
||||
|
if (!lst || !f) |
||||
|
return ; |
||||
|
(*f)(lst->content); |
||||
|
if (lst->next) |
||||
|
ft_lstiter(lst->next, f); |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstlast_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 14:50:21 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:07:01 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
t_list *ft_lstlast(t_list *lst) |
||||
|
{ |
||||
|
if (!lst) |
||||
|
return (NULL); |
||||
|
while (lst->next) |
||||
|
lst = lst->next; |
||||
|
return (lst); |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstmap_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 14:54:31 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:06:55 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) |
||||
|
{ |
||||
|
t_list *new_lst; |
||||
|
|
||||
|
if (!lst || !f) |
||||
|
return (NULL); |
||||
|
new_lst = ft_lstnew((*f)(lst->content)); |
||||
|
if (!new_lst) |
||||
|
{ |
||||
|
ft_lstclear(&new_lst, del); |
||||
|
return (NULL); |
||||
|
} |
||||
|
new_lst->next = ft_lstmap(lst->next, f, del); |
||||
|
return (new_lst); |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstnew_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 17:36:09 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:06:52 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
t_list *ft_lstnew(void *content) |
||||
|
{ |
||||
|
t_list *i_cell; |
||||
|
|
||||
|
i_cell = (t_list *)malloc(sizeof(*i_cell)); |
||||
|
if (!i_cell) |
||||
|
return (NULL); |
||||
|
i_cell->content = content; |
||||
|
i_cell->next = NULL; |
||||
|
return (i_cell); |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_lstsize_bonus.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 14:49:32 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:06:39 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_lstsize(t_list *lst) |
||||
|
{ |
||||
|
size_t lst_size; |
||||
|
|
||||
|
lst_size = 0; |
||||
|
while (lst && ++lst_size) |
||||
|
lst = lst->next; |
||||
|
return (lst_size); |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_memchr.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:15:55 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/02 17:36:30 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void *ft_memchr(const void *b, int c, size_t n) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
while (i < n) |
||||
|
{ |
||||
|
if (*(unsigned char *)(b + i) == (unsigned char)c) |
||||
|
return ((void *)(b + i)); |
||||
|
i++; |
||||
|
} |
||||
|
return (NULL); |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_memcmp.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:16:23 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/02 17:37:36 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_memcmp(const void *s1, const void *s2, size_t n) |
||||
|
{ |
||||
|
size_t i; |
||||
|
int diff; |
||||
|
|
||||
|
i = 0; |
||||
|
while (i < n) |
||||
|
{ |
||||
|
diff = *(unsigned char *)(s1 + i) - *(unsigned char *)(s2 + i); |
||||
|
if (diff) |
||||
|
return (diff); |
||||
|
i++; |
||||
|
} |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_memcpy.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:16:47 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 09:00:15 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void *ft_memcpy(void *dst, const void *src, size_t n) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
if (!src && !dst) |
||||
|
return (NULL); |
||||
|
while (i < n) |
||||
|
{ |
||||
|
*(char *)(dst + i) = *(char *)(src + i); |
||||
|
i++; |
||||
|
} |
||||
|
return (dst); |
||||
|
} |
@ -0,0 +1,33 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_memmove.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:17:21 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 09:01:11 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void *ft_memmove(void *dst, const void *src, size_t n) |
||||
|
{ |
||||
|
size_t i; |
||||
|
unsigned char *ptr_dst; |
||||
|
unsigned char *ptr_src; |
||||
|
|
||||
|
ptr_dst = (unsigned char *)dst; |
||||
|
ptr_src = (unsigned char *)src; |
||||
|
i = 0; |
||||
|
if (!dst && !src) |
||||
|
return (NULL); |
||||
|
if (ptr_src < ptr_dst) |
||||
|
while (++i <= n) |
||||
|
ptr_dst[n - i] = ptr_src[n - i]; |
||||
|
else |
||||
|
while (n--) |
||||
|
*(ptr_dst++) = *(ptr_src++); |
||||
|
return (dst); |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_memset.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/19 11:43:44 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 08:56:04 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void *ft_memset(void *b, int c, size_t len) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
while (i < len) |
||||
|
*(unsigned char *)(b + i++) = (unsigned char)c; |
||||
|
return ((void *)b); |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_putchar_fd.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 16:32:52 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/22 15:11:44 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_putchar_fd(char c, int fd) |
||||
|
{ |
||||
|
write(fd, &c, 1); |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_putendl_fd.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:11:11 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 10:54:08 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_putendl_fd(char *s, int fd) |
||||
|
{ |
||||
|
if (!s) |
||||
|
return ; |
||||
|
ft_putstr_fd(s, fd); |
||||
|
ft_putchar_fd('\n', fd); |
||||
|
} |
@ -0,0 +1,61 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_putnbr_fd.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:12:01 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 12:43:19 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
static size_t get_nbrlength(int nbr) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
if (nbr == 0) |
||||
|
return (1); |
||||
|
while (nbr && ++i) |
||||
|
nbr = nbr / 10; |
||||
|
return (i); |
||||
|
} |
||||
|
|
||||
|
static int pow_10(size_t n) |
||||
|
{ |
||||
|
int ret; |
||||
|
|
||||
|
if (!n) |
||||
|
return (1); |
||||
|
ret = 10; |
||||
|
while (--n) |
||||
|
ret *= 10; |
||||
|
return (ret); |
||||
|
} |
||||
|
|
||||
|
void ft_putnbr_fd(int n, int fd) |
||||
|
{ |
||||
|
int fact; |
||||
|
|
||||
|
if (n == -2147483648) |
||||
|
{ |
||||
|
ft_putstr_fd("-2147483648", fd); |
||||
|
return ; |
||||
|
} |
||||
|
if (n < 0) |
||||
|
{ |
||||
|
ft_putchar_fd('-', fd); |
||||
|
n = -n; |
||||
|
} |
||||
|
fact = pow_10(get_nbrlength(n) - 1); |
||||
|
while (fact >= 10) |
||||
|
{ |
||||
|
ft_putchar_fd(n / fact + '0', fd); |
||||
|
n = n % fact; |
||||
|
fact /= 10; |
||||
|
} |
||||
|
ft_putchar_fd(n + '0', fd); |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_putstr_fd.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/22 15:10:39 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 10:54:25 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_putstr_fd(char *s, int fd) |
||||
|
{ |
||||
|
if (!s) |
||||
|
return ; |
||||
|
while (*s) |
||||
|
ft_putchar_fd(*(s++), fd); |
||||
|
} |
@ -0,0 +1,103 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_split.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 15:43:48 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:27:27 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
static int ft_strlen_to(char const *str, int i, char ch, int lf_not) |
||||
|
{ |
||||
|
if (!str[i]) |
||||
|
return (i); |
||||
|
if ((!lf_not && str[i] == ch) || (lf_not && !(str[i] == ch))) |
||||
|
return (i); |
||||
|
return (ft_strlen_to(str, ++i, ch, lf_not)); |
||||
|
} |
||||
|
|
||||
|
static int goto_cur_word(char const *str, char ch, int *words_len, int j_word) |
||||
|
{ |
||||
|
int i_word; |
||||
|
int i; |
||||
|
|
||||
|
i_word = 0; |
||||
|
i = 0; |
||||
|
i += ft_strlen_to(str, i, ch, 1) - i; |
||||
|
while (i_word < j_word) |
||||
|
{ |
||||
|
i += words_len[i_word++]; |
||||
|
i += ft_strlen_to(str, i, ch, 1) - i; |
||||
|
} |
||||
|
return (i); |
||||
|
} |
||||
|
|
||||
|
static char *ft_get_word(char const *str, char ch, int *words_len, int j_word) |
||||
|
{ |
||||
|
int i; |
||||
|
int j; |
||||
|
char *output; |
||||
|
|
||||
|
output = ft_calloc((words_len[j_word] + 1), sizeof(char)); |
||||
|
if (output == NULL) |
||||
|
return (output); |
||||
|
i = goto_cur_word(str, ch, words_len, j_word); |
||||
|
j = 0; |
||||
|
while (j < words_len[j_word]) |
||||
|
{ |
||||
|
i += ft_strlen_to(str, i, ch, 1) - i; |
||||
|
output[j] = str[i + j]; |
||||
|
j++; |
||||
|
} |
||||
|
output[j] = '\0'; |
||||
|
return (output); |
||||
|
} |
||||
|
|
||||
|
int get_words_len(int **words_len, char const *str, char ch) |
||||
|
{ |
||||
|
int i; |
||||
|
int strlen; |
||||
|
int words_count; |
||||
|
|
||||
|
i = 0; |
||||
|
words_count = 0; |
||||
|
strlen = ft_strlen_to(str, 0, '\0', 0); |
||||
|
while (i < strlen) |
||||
|
{ |
||||
|
(*words_len)[words_count] = ft_strlen_to(str, i, ch, 0) - i; |
||||
|
i += (*words_len)[words_count] + 1; |
||||
|
if ((*words_len)[words_count] && ++words_count) |
||||
|
(*words_len)[words_count] = 0; |
||||
|
} |
||||
|
return (words_count); |
||||
|
} |
||||
|
|
||||
|
char **ft_split(char const *str, char ch) |
||||
|
{ |
||||
|
int i; |
||||
|
int words_count; |
||||
|
int *words_len; |
||||
|
char **output; |
||||
|
|
||||
|
if (!str) |
||||
|
return (NULL); |
||||
|
i = -1; |
||||
|
words_len = ft_calloc(200, sizeof(int)); |
||||
|
if (!words_len) |
||||
|
return (NULL); |
||||
|
words_count = get_words_len(&words_len, str, ch); |
||||
|
output = ft_calloc(words_count + 1, sizeof(char *)); |
||||
|
if (!output) |
||||
|
return (NULL); |
||||
|
i = -1; |
||||
|
while (++i < words_count) |
||||
|
output[i] = ft_get_word(str, ch, words_len, i); |
||||
|
output[i] = NULL; |
||||
|
free(words_len); |
||||
|
return (output); |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strchr.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 16:21:06 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/29 14:21:30 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_strchr(const char *s, int c) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
while (s[i] || c == '\0') |
||||
|
{ |
||||
|
if (s[i] == (c % 256)) |
||||
|
return ((char *)(s + i)); |
||||
|
i++; |
||||
|
} |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strdup.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/19 14:07:50 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/29 14:39:45 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_strdup(const char *src) |
||||
|
{ |
||||
|
char *dup; |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
dup = malloc((ft_strlen(src) + 1) * sizeof(char)); |
||||
|
if (!dup) |
||||
|
return (NULL); |
||||
|
while (src[i]) |
||||
|
{ |
||||
|
dup[i] = src[i]; |
||||
|
i++; |
||||
|
} |
||||
|
dup[i] = '\0'; |
||||
|
return (dup); |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_striteri.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 16:23:41 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 11:28:48 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
void ft_striteri(char *s, void (*f)(unsigned int, char*)) |
||||
|
{ |
||||
|
unsigned int i; |
||||
|
|
||||
|
i = 0; |
||||
|
if (!s || !f) |
||||
|
return ; |
||||
|
while (s[i]) |
||||
|
{ |
||||
|
f(i, s + i); |
||||
|
i++; |
||||
|
} |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strjoin.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 06:49:25 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 10:06:33 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_strjoin(const char *s1, const char *s2) |
||||
|
{ |
||||
|
char *ret; |
||||
|
size_t i; |
||||
|
size_t j; |
||||
|
|
||||
|
if (!s1 || !s2) |
||||
|
return (NULL); |
||||
|
ret = malloc(ft_strlen(s1) + ft_strlen(s2) + 1); |
||||
|
if (ret == NULL) |
||||
|
return (NULL); |
||||
|
i = 0; |
||||
|
while (s1[i]) |
||||
|
{ |
||||
|
ret[i] = s1[i]; |
||||
|
i++; |
||||
|
} |
||||
|
j = 0; |
||||
|
while (s2[j]) |
||||
|
{ |
||||
|
ret[i] = s2[j]; |
||||
|
i++; |
||||
|
j++; |
||||
|
} |
||||
|
ret[i] = '\0'; |
||||
|
return (ret); |
||||
|
} |
@ -0,0 +1,37 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* strlcat.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 16:13:02 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/19 15:24:26 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
size_t ft_strlcat(char *dst, const char *src, size_t dstsize) |
||||
|
{ |
||||
|
size_t i; |
||||
|
size_t j; |
||||
|
|
||||
|
i = 0; |
||||
|
j = 0; |
||||
|
while (dst[i]) |
||||
|
i++; |
||||
|
while (dstsize && i + j < dstsize - 1 && src[j]) |
||||
|
{ |
||||
|
dst[i + j] = src[j]; |
||||
|
j++; |
||||
|
} |
||||
|
if (dstsize) |
||||
|
dst[i + j] = '\0'; |
||||
|
while (src[j]) |
||||
|
j++; |
||||
|
if (i > dstsize) |
||||
|
return (dstsize + j); |
||||
|
else |
||||
|
return (i + j); |
||||
|
} |
@ -0,0 +1,30 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* strlcpy.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 16:08:31 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/19 15:24:21 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
while (dstsize && i < dstsize - 1 && src[i]) |
||||
|
{ |
||||
|
dst[i] = src[i]; |
||||
|
i++; |
||||
|
} |
||||
|
if (dstsize) |
||||
|
dst[i] = '\0'; |
||||
|
while (src[i]) |
||||
|
i++; |
||||
|
return (i); |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strlen.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 16:04:47 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/02 17:42:12 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
size_t ft_strlen(const char *s) |
||||
|
{ |
||||
|
size_t i; |
||||
|
|
||||
|
i = 0; |
||||
|
while (s[i] != '\0') |
||||
|
{ |
||||
|
i++; |
||||
|
} |
||||
|
return (i); |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strmapi.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 16:12:32 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 10:52:59 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) |
||||
|
{ |
||||
|
unsigned int i; |
||||
|
char *ret; |
||||
|
|
||||
|
if (!s) |
||||
|
return (NULL); |
||||
|
ret = (char *)ft_calloc(ft_strlen(s) + 1, sizeof(char)); |
||||
|
if (!ret) |
||||
|
return (NULL); |
||||
|
i = 0; |
||||
|
while (s[i]) |
||||
|
{ |
||||
|
ret[i] = (*f)(i, s[i]); |
||||
|
i++; |
||||
|
} |
||||
|
return (ret); |
||||
|
} |
@ -0,0 +1,34 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strncmp.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 16:16:37 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 09:55:28 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
int ft_strncmp(const char *s1, const char *s2, size_t n) |
||||
|
{ |
||||
|
size_t i; |
||||
|
int ret; |
||||
|
unsigned char *s1cp; |
||||
|
unsigned char *s2cp; |
||||
|
|
||||
|
if (!n) |
||||
|
return (0); |
||||
|
i = 0; |
||||
|
s1cp = (unsigned char *)s1; |
||||
|
s2cp = (unsigned char *)s2; |
||||
|
ret = s1cp[i] - s2cp[i]; |
||||
|
while ((!ret && i < n) && (s1[i] || s2[i])) |
||||
|
{ |
||||
|
ret = s1cp[i] - s2cp[i]; |
||||
|
i++; |
||||
|
} |
||||
|
return (ret); |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strnstr.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 16:52:56 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/23 14:22:48 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_strnstr(const char *haystack, const char *needle, size_t len) |
||||
|
{ |
||||
|
size_t i; |
||||
|
size_t j; |
||||
|
int found; |
||||
|
|
||||
|
i = 0; |
||||
|
j = 0; |
||||
|
found = 0; |
||||
|
while (haystack[i] && i < len && needle[j]) |
||||
|
{ |
||||
|
if (haystack[i] == needle[j]) |
||||
|
{ |
||||
|
found = 1; |
||||
|
j++; |
||||
|
} |
||||
|
else if (found) |
||||
|
{ |
||||
|
found = 0; |
||||
|
i -= j; |
||||
|
j = 0; |
||||
|
} |
||||
|
i++; |
||||
|
} |
||||
|
if (!needle[j]) |
||||
|
return ((char *)(haystack + i - j)); |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,33 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strrchr.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 16:47:21 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 09:01:44 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_strrchr(const char *s, int c) |
||||
|
{ |
||||
|
size_t i; |
||||
|
int found; |
||||
|
|
||||
|
i = 0; |
||||
|
found = -1; |
||||
|
while (s[i]) |
||||
|
{ |
||||
|
if (s[i] == (c % 256)) |
||||
|
found = i; |
||||
|
i++; |
||||
|
} |
||||
|
if (c == '\0') |
||||
|
return ((char *)(s + i)); |
||||
|
if (found >= 0) |
||||
|
return ((char *)(s + found)); |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,53 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_strtrim.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 13:49:37 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 10:07:32 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
static int ft_search_char(char c, char const *set) |
||||
|
{ |
||||
|
size_t i; |
||||
|
size_t set_len; |
||||
|
|
||||
|
if (!c) |
||||
|
return (0); |
||||
|
i = 0; |
||||
|
set_len = ft_strlen(set); |
||||
|
while (i < set_len) |
||||
|
if (c == set[i++]) |
||||
|
return (1); |
||||
|
return (0); |
||||
|
} |
||||
|
|
||||
|
char *ft_strtrim(char const *s1, char const *set) |
||||
|
{ |
||||
|
unsigned int i; |
||||
|
unsigned int b_out; |
||||
|
unsigned int e_out; |
||||
|
char *ret; |
||||
|
|
||||
|
if (!s1) |
||||
|
return (NULL); |
||||
|
i = 0; |
||||
|
b_out = 0; |
||||
|
e_out = ft_strlen(s1); |
||||
|
while (ft_search_char(s1[b_out], set)) |
||||
|
b_out++; |
||||
|
while (e_out > b_out && ft_search_char(s1[e_out - 1], set)) |
||||
|
e_out--; |
||||
|
ret = malloc((e_out - b_out + 1) * sizeof(char)); |
||||
|
if (!ret) |
||||
|
return (NULL); |
||||
|
while (b_out < e_out) |
||||
|
ret[i++] = s1[b_out++]; |
||||
|
ret[i] = '\0'; |
||||
|
return (ret); |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_substr.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/20 06:45:32 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 11:12:02 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "libft.h" |
||||
|
|
||||
|
char *ft_substr(char const *s, unsigned int start, size_t len) |
||||
|
{ |
||||
|
size_t i; |
||||
|
char *ret; |
||||
|
size_t s_size; |
||||
|
|
||||
|
if (!s) |
||||
|
return (NULL); |
||||
|
s_size = ft_strlen(s); |
||||
|
if (start > s_size) |
||||
|
{ |
||||
|
start = s_size; |
||||
|
len = 0; |
||||
|
} |
||||
|
else if (s_size < start + len) |
||||
|
len = s_size - start; |
||||
|
ret = ft_calloc(len + 1, sizeof(char)); |
||||
|
if (!ret) |
||||
|
return (NULL); |
||||
|
i = 0; |
||||
|
while (i < len && s[start + i]) |
||||
|
{ |
||||
|
ret[i] = s[start + i]; |
||||
|
i++; |
||||
|
} |
||||
|
ret[i] = '\0'; |
||||
|
return (ret); |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_tolower.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 17:02:25 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/19 12:35:58 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
int ft_tolower(int c) |
||||
|
{ |
||||
|
if (c > 64 && c < 91) |
||||
|
return (c + 32); |
||||
|
else |
||||
|
return (c); |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* ft_toupper.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/18 17:02:28 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/10/19 12:36:28 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
int ft_toupper(int c) |
||||
|
{ |
||||
|
if (c > 96 && c < 123) |
||||
|
return (c - 32); |
||||
|
else |
||||
|
return (c); |
||||
|
} |
@ -0,0 +1,71 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* libft.h :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ |
||||
|
/* Updated: 2021/11/15 13:07:44 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#ifndef LIBFT_H |
||||
|
# define LIBFT_H |
||||
|
|
||||
|
# include <stddef.h> |
||||
|
# include <stdlib.h> |
||||
|
# include <unistd.h> |
||||
|
|
||||
|
typedef struct s_list |
||||
|
{ |
||||
|
void *content; |
||||
|
struct s_list *next; |
||||
|
} t_list; |
||||
|
|
||||
|
int ft_atoi(const char *str); |
||||
|
void ft_bzero(void *s, size_t n); |
||||
|
void *ft_calloc( size_t num, size_t size ); |
||||
|
int ft_isalpha(int ch); |
||||
|
int ft_isascii(int ch); |
||||
|
int ft_isdigit(int ch); |
||||
|
int ft_isprint(int ch); |
||||
|
int ft_isalnum(int ch); |
||||
|
void *ft_memchr(const void *b, int c, size_t n); |
||||
|
int ft_memcmp(const void *s1, const void *s2, size_t n); |
||||
|
void *ft_memcpy(void *dst, const void *src, size_t n); |
||||
|
void *ft_memmove(void *dst, const void *src, size_t n); |
||||
|
void *ft_memset(void *b, int c, size_t len); |
||||
|
char *ft_strchr(const char *s, int c); |
||||
|
char *ft_strdup(const char *src); |
||||
|
size_t ft_strlcat(char *dst, const char *src, size_t dstsize); |
||||
|
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); |
||||
|
size_t ft_strlen(const char *s); |
||||
|
int ft_strncmp(const char *s1, const char *s2, size_t n); |
||||
|
char *ft_strnstr(const char *haystack, const char *needle, size_t len); |
||||
|
char *ft_strrchr(const char *s, int c); |
||||
|
int ft_tolower(int c); |
||||
|
int ft_toupper(int c); |
||||
|
char *ft_substr(char const *s, unsigned int start, size_t len); |
||||
|
char *ft_strjoin(const char *s1, const char *s2); |
||||
|
char *ft_strtrim(char const *s1, char const *set); |
||||
|
char **ft_split(char const *str, char ch); |
||||
|
char *ft_itoa(int n); |
||||
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); |
||||
|
void ft_striteri(char *s, void (*f)(unsigned int, char*)); |
||||
|
void ft_putchar_fd(char c, int fd); |
||||
|
void ft_putstr_fd(char *s, int fd); |
||||
|
void ft_putendl_fd(char *s, int fd); |
||||
|
void ft_putnbr_fd(int n, int fd); |
||||
|
|
||||
|
t_list *ft_lstnew(void *content); |
||||
|
void ft_lstadd_front(t_list **alst, t_list *new); |
||||
|
int ft_lstsize(t_list *lst); |
||||
|
t_list *ft_lstlast(t_list *lst); |
||||
|
void ft_lstadd_back(t_list **alst, t_list *new); |
||||
|
void ft_lstdelone(t_list *lst, void (*del)(void *)); |
||||
|
void ft_lstclear(t_list **lst, void (*del)(void *)); |
||||
|
void ft_lstiter(t_list *lst, void (*f)(void *)); |
||||
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); |
||||
|
|
||||
|
#endif |
Loading…
Reference in new issue