├── .gitignore ├── Intra Projects Libft.pdf ├── subjects ├── fr.libft_sujet.pdf └── en.libft_subject.pdf ├── ft_putchar_fd.c ├── ft_isascii.c ├── ft_isdigit.c ├── ft_isprint.c ├── ft_tolower.c ├── ft_toupper.c ├── ft_isalnum.c ├── ft_lstlast_bonus.c ├── ft_isalpha.c ├── ft_strlen.c ├── ft_putstr_fd.c ├── ft_lstiter_bonus.c ├── ft_lstadd_front_bonus.c ├── ft_bzero.c ├── ft_lstdelone_bonus.c ├── ft_putendl_fd.c ├── ft_lstsize_bonus.c ├── ft_striteri.c ├── ft_lstadd_back_bonus.c ├── ft_lstnew_bonus.c ├── ft_memcpy.c ├── ft_memset.c ├── ft_lstclear_bonus.c ├── ft_memchr.c ├── ft_strchr.c ├── ft_strncmp.c ├── ft_strrchr.c ├── ft_strdup.c ├── ft_memcmp.c ├── ft_strmapi.c ├── ft_strlcpy.c ├── Makefile ├── ft_putnbr_fd.c ├── ft_strjoin.c ├── ft_atoi.c ├── ft_calloc.c ├── ft_memmove.c ├── ft_lstmap_bonus.c ├── ft_substr.c ├── ft_strlcat.c ├── ft_strnstr.c ├── ft_itoa.c ├── ft_split.c ├── ft_strtrim.c └── libft.h /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .vscode 3 | -------------------------------------------------------------------------------- /Intra Projects Libft.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titouanck/42-Libft/HEAD/Intra Projects Libft.pdf -------------------------------------------------------------------------------- /subjects/fr.libft_sujet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titouanck/42-Libft/HEAD/subjects/fr.libft_sujet.pdf -------------------------------------------------------------------------------- /subjects/en.libft_subject.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titouanck/42-Libft/HEAD/subjects/en.libft_subject.pdf -------------------------------------------------------------------------------- /ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/08/01 18:38:45 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 01:17:31 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | if (c >= 0 && c <= 127) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 00:52:07 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | if (c >= '0' && c <= '9') 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 01:26:38 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | if (c >= 32 && 126 >= c) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 01:43:20 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:33:15 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (c >= 'A' && 'Z' >= c) 18 | return (c + 'a' - 'A'); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 01:34:13 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:33:19 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (c >= 'a' && 'z' >= c) 18 | return (c - 'a' + 'A'); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 01:12:14 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | if (ft_isalpha(c) || ft_isdigit(c)) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /ft_lstlast_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 16:27:04 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/07 17:24:48 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | while (lst && lst->next) 18 | lst = lst->next; 19 | return (lst); 20 | } 21 | -------------------------------------------------------------------------------- /ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 00:52:07 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/09 15:16:57 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | i++; 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 18:35:20 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char const *s, int fd) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | i++; 22 | write(fd, s, i); 23 | } 24 | -------------------------------------------------------------------------------- /ft_lstiter_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 17:09:25 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 05:07:42 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | while (lst && f) 18 | { 19 | f(lst->content); 20 | lst = lst->next; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ft_lstadd_front_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 16:13:53 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 05:07:42 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **alst, t_list *new) 16 | { 17 | if (!alst || !new) 18 | return ; 19 | new->next = *alst; 20 | *alst = new; 21 | } 22 | -------------------------------------------------------------------------------- /ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/09 14:45:14 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (i < n) 21 | { 22 | *(unsigned char *)(s + i) = 0; 23 | i++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ft_lstdelone_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 16:48:21 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 05:07:42 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 16 | { 17 | if (!lst) 18 | return ; 19 | if (del) 20 | del(lst->content); 21 | free(lst); 22 | } 23 | -------------------------------------------------------------------------------- /ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 18:37:18 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char const *s, int fd) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | i++; 22 | write(fd, s, i); 23 | write(fd, "\n", 1); 24 | } 25 | -------------------------------------------------------------------------------- /ft_lstsize_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 16:21:49 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 05:07:42 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list *lst) 16 | { 17 | int size; 18 | 19 | size = 0; 20 | while (lst) 21 | { 22 | size++; 23 | lst = lst->next; 24 | } 25 | return (size); 26 | } 27 | -------------------------------------------------------------------------------- /ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 11:52:07 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/08 17:23:49 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char *)) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | { 22 | (*f)(i, s + i); 23 | i++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ft_lstadd_back_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 16:45:15 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 05:07:42 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list **alst, t_list *new) 16 | { 17 | if (!alst || !new) 18 | return ; 19 | if (*alst) 20 | ft_lstlast(*alst)->next = new; 21 | else 22 | *alst = new; 23 | } 24 | -------------------------------------------------------------------------------- /ft_lstnew_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 16:04:08 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 05:07:42 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void *content) 16 | { 17 | t_list *new; 18 | 19 | new = malloc(sizeof(t_list)); 20 | if (!new) 21 | return (NULL); 22 | new->content = content; 23 | new->next = NULL; 24 | return (new); 25 | } 26 | -------------------------------------------------------------------------------- /ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/09 15:00:16 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (i < n) 21 | { 22 | *(unsigned char *)(dst + i) = *(unsigned char *)(src + i); 23 | i++; 24 | } 25 | return (dst); 26 | } 27 | -------------------------------------------------------------------------------- /ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/09 14:20:37 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | size_t i; 18 | 19 | if (!b) 20 | return (NULL); 21 | i = 0; 22 | while (i < len) 23 | { 24 | *(unsigned char *)(b + i) = (unsigned char) c; 25 | i++; 26 | } 27 | return (b); 28 | } 29 | -------------------------------------------------------------------------------- /ft_lstclear_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 16:59:36 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 05:07:42 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list **lst, void (*del)(void *)) 16 | { 17 | if (!lst || !(*lst) || !del) 18 | return ; 19 | if ((*lst)->next) 20 | ft_lstclear((&(*lst)->next), del); 21 | del((*lst)->content); 22 | free(*lst); 23 | *lst = NULL; 24 | } 25 | -------------------------------------------------------------------------------- /ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/09 16:30:51 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (i < n) 21 | { 22 | if (*(unsigned char *)(s + i) == (unsigned char)c) 23 | return ((void *)s + i); 24 | i++; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/10 18:04:25 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | { 22 | if (s[i] == (char) c) 23 | break ; 24 | i++; 25 | } 26 | if (s[i] == (char) c) 27 | return ((char *) s + i); 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 00:07:34 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/09 15:34:31 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (s1[i] && s1[i] == s2[i] && i < n) 21 | i++; 22 | if (i < n) 23 | return ((unsigned char) s1[i] - (unsigned char) s2[i]); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/10 18:25:56 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/07 15:56:22 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | if (!s) 20 | return (NULL); 21 | i = ft_strlen(s); 22 | while (i >= 0) 23 | { 24 | if (s[i] == (char) c) 25 | return ((char *) s + i); 26 | i--; 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/10 16:37:35 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | char *new; 18 | size_t i; 19 | 20 | new = malloc(sizeof(char) * (ft_strlen(s1) + 1)); 21 | if (!new) 22 | return (NULL); 23 | i = 0; 24 | while (s1[i]) 25 | { 26 | new[i] = s1[i]; 27 | i++; 28 | } 29 | new[i] = '\0'; 30 | return (new); 31 | } 32 | -------------------------------------------------------------------------------- /ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/09 16:55:13 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (i < n && *(unsigned char *)(s1 + i) == *(unsigned char *)(s2 + i)) 21 | i++; 22 | if (i < n) 23 | return (*(unsigned char *)(s1 + i) - *(unsigned char *)(s2 + i)); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 12:07:16 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | size_t i; 18 | char *new; 19 | 20 | new = malloc(sizeof(char) * (ft_strlen(s) + 1)); 21 | if (!new) 22 | return (NULL); 23 | i = 0; 24 | while (s[i]) 25 | { 26 | new[i] = (*f)(i, s[i]); 27 | i++; 28 | } 29 | new[i] = '\0'; 30 | return (new); 31 | } 32 | -------------------------------------------------------------------------------- /ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/13 14:28:03 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | if (!dst || !src) 21 | return (0); 22 | while (src[i] && i + 1 < dstsize) 23 | { 24 | dst[i] = src[i]; 25 | i++; 26 | } 27 | if (dstsize > 0) 28 | { 29 | dst[i] = '\0'; 30 | i++; 31 | } 32 | return (ft_strlen(src)); 33 | } 34 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | LIBC = ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c \ 2 | ft_isascii.c ft_isdigit.c ft_isprint.c ft_memchr.c \ 3 | ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_strchr.c \ 4 | ft_strdup.c ft_strlcat.c ft_strlcpy.c ft_strlen.c ft_strncmp.c \ 5 | ft_strnstr.c ft_strrchr.c ft_tolower.c ft_toupper.c 6 | 7 | ADDITIONAL = ft_itoa.c ft_putchar_fd.c ft_putendl_fd.c ft_putnbr_fd.c ft_putstr_fd.c \ 8 | ft_split.c ft_strjoin.c ft_strmapi.c ft_strtrim.c ft_substr.c ft_striteri.c 9 | 10 | BONUS = ft_lstadd_back_bonus.c ft_lstadd_front_bonus.c ft_lstclear_bonus.c \ 11 | ft_lstdelone_bonus.c ft_lstiter_bonus.c ft_lstlast_bonus.c \ 12 | ft_lstmap_bonus.c ft_lstnew_bonus.c ft_lstsize_bonus.c 13 | 14 | SRCS = ${LIBC} ${ADDITIONAL} 15 | 16 | SRCSALL = ${LIBC} ${ADDITIONAL} ${BONUS} 17 | 18 | OBJS = ${SRCS:.c=.o} 19 | 20 | OBJSALL = ${SRCSALL:.c=.o} 21 | 22 | LIB = libft.a 23 | 24 | CC = gcc 25 | 26 | CFLAGS = -Wall -Werror -Wextra -I ./ 27 | 28 | .c.o: 29 | ${CC} ${CFLAGS} -c $< -o ${<:.c=.o} 30 | 31 | ${LIB}: ${OBJS} 32 | ar -rsc ${LIB} ${OBJS} 33 | 34 | bonus: ${OBJSALL} 35 | ar -rsc ${LIB} ${OBJSALL} 36 | 37 | all: ${LIB} 38 | 39 | clean: 40 | rm -f ${OBJSALL} 41 | 42 | fclean: clean; 43 | rm -f ${LIB} 44 | 45 | re: fclean all 46 | 47 | .PHONY: all clean fclean re bonus -------------------------------------------------------------------------------- /ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 18:39:18 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static void print_nb(long nb, int fd) 16 | { 17 | if (nb / 10) 18 | { 19 | print_nb(nb / 10, fd); 20 | print_nb(nb % 10, fd); 21 | } 22 | else 23 | ft_putchar_fd(nb + '0', fd); 24 | } 25 | 26 | void ft_putnbr_fd(int n, int fd) 27 | { 28 | long nb; 29 | 30 | nb = n; 31 | if (nb < 0) 32 | { 33 | write(fd, "-", 1); 34 | nb = -nb; 35 | } 36 | print_nb(nb, fd); 37 | } 38 | -------------------------------------------------------------------------------- /ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 12:48:12 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | char *new; 18 | size_t i; 19 | size_t j; 20 | 21 | new = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); 22 | if (!new) 23 | return (NULL); 24 | i = 0; 25 | while (s1[i]) 26 | { 27 | new[i] = s1[i]; 28 | i++; 29 | } 30 | j = 0; 31 | while (s2[j]) 32 | { 33 | new[i + j] = s2[j]; 34 | j++; 35 | } 36 | new[i + j] = '\0'; 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/11 00:44:51 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | int nbr; 18 | int sign; 19 | size_t i; 20 | 21 | nbr = 0; 22 | sign = 1; 23 | i = 0; 24 | while (str[i] == ' ' || ('\t' <= str[i] && str[i] <= '\r')) 25 | i++; 26 | if (str[i] == '+') 27 | i++; 28 | else if (str[i] == '-') 29 | { 30 | sign *= -1; 31 | i++; 32 | } 33 | while ('0' <= str[i] && str[i] <= '9') 34 | { 35 | nbr = nbr * 10 + str[i] - '0'; 36 | i++; 37 | } 38 | return (nbr * sign); 39 | } 40 | -------------------------------------------------------------------------------- /ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/13 12:53:43 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/08 18:20:30 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static void *ft_memalloc(size_t size) 16 | { 17 | void *new; 18 | size_t i; 19 | 20 | new = (void *) malloc(size); 21 | if (!new) 22 | return (NULL); 23 | i = 0; 24 | while (i < size) 25 | { 26 | *(unsigned char *)(new + i) = 0; 27 | i++; 28 | } 29 | return (new); 30 | } 31 | 32 | void *ft_calloc(size_t count, size_t size) 33 | { 34 | if (size != 0 && count > ((size_t) -1 / size)) 35 | return (NULL); 36 | return (ft_memalloc(count * size)); 37 | } 38 | -------------------------------------------------------------------------------- /ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/13 15:56:21 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | long i; 18 | 19 | if (dst < src) 20 | { 21 | i = 0; 22 | while ((size_t)i < len) 23 | { 24 | *(unsigned char *)(dst + i) = *(unsigned char *)(src + i); 25 | i++; 26 | } 27 | return (dst); 28 | } 29 | else 30 | { 31 | i = len - 1; 32 | while (i >= 0) 33 | { 34 | *(unsigned char *)(dst + i) = *(unsigned char *)(src + i); 35 | i--; 36 | } 37 | return (dst); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ft_lstmap_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/19 17:16:14 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 05:07:42 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 16 | { 17 | t_list *start; 18 | t_list *current; 19 | 20 | if (!lst || !f || !del) 21 | return (NULL); 22 | start = ft_lstnew(f(lst->content)); 23 | current = start; 24 | while (lst && lst->next) 25 | { 26 | if (!current) 27 | return (NULL); 28 | current->next = ft_lstnew(f(lst->next->content)); 29 | current = current->next; 30 | lst = lst->next; 31 | } 32 | return (start); 33 | } 34 | -------------------------------------------------------------------------------- /ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 12:39:00 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/07 16:18:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_substr(char const *s, unsigned int start, size_t len) 16 | { 17 | char *new; 18 | unsigned int i; 19 | 20 | if (start >= ft_strlen(s)) 21 | len = 0; 22 | else if (len > ft_strlen(s + start)) 23 | len = ft_strlen(s + start); 24 | new = malloc(sizeof(char) * (len + 1)); 25 | if (!new) 26 | return (NULL); 27 | i = 0; 28 | while (start + i < ft_strlen(s) && i < len) 29 | { 30 | new[i] = s[start + i]; 31 | i++; 32 | } 33 | new[i] = '\0'; 34 | return (new); 35 | } 36 | -------------------------------------------------------------------------------- /ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/10 17:32:38 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *d, const char *s, size_t dstsize) 16 | { 17 | size_t dst_len; 18 | size_t index; 19 | size_t i; 20 | 21 | dst_len = ft_strlen(d); 22 | index = 0; 23 | while (d[index]) 24 | index++; 25 | i = 0; 26 | while (s[i] && (i + index + 1) < (dstsize)) 27 | { 28 | d[index + i] = s[i]; 29 | i++; 30 | } 31 | if (i < dstsize) 32 | d[index + i] = '\0'; 33 | if (dstsize <= dst_len) 34 | return (ft_strlen(s) + dstsize); 35 | else 36 | return (ft_strlen(s) + dst_len); 37 | } 38 | -------------------------------------------------------------------------------- /ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/10 23:52:11 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/10 14:24:53 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 | { 17 | size_t len_n; 18 | size_t i; 19 | size_t j; 20 | 21 | if (!haystack || !needle) 22 | return (NULL); 23 | if (ft_strlen(needle) == 0) 24 | return ((char *) haystack); 25 | len_n = ft_strlen(needle); 26 | i = 0; 27 | while (haystack[i] && i < len) 28 | { 29 | j = 0; 30 | while (haystack[i + j] && haystack[i + j] == needle[j] && i + j < len) 31 | j++; 32 | if (j == len_n) 33 | return ((char *) haystack + i); 34 | i++; 35 | } 36 | return (NULL); 37 | } 38 | -------------------------------------------------------------------------------- /ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 17:53:25 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static size_t count_size(long nb) 16 | { 17 | size_t size; 18 | 19 | size = 0; 20 | if (nb < 0) 21 | { 22 | nb = nb * (-1); 23 | size = 1; 24 | } 25 | if (nb == 0) 26 | size = 1; 27 | else 28 | { 29 | while (nb) 30 | { 31 | nb = nb / 10; 32 | size++; 33 | } 34 | } 35 | return (size); 36 | } 37 | 38 | char *ft_itoa(int n) 39 | { 40 | size_t size; 41 | long nb; 42 | char *str; 43 | int is_negative; 44 | 45 | size = count_size((long) n); 46 | str = (char *) malloc(sizeof(char) * (size + 1)); 47 | if (str == NULL) 48 | return (NULL); 49 | nb = (long) n; 50 | is_negative = 0; 51 | if (nb < 0) 52 | { 53 | nb = nb * (-1); 54 | str[0] = '-'; 55 | is_negative = 1; 56 | } 57 | str[size] = '\0'; 58 | while (size > (size_t) is_negative) 59 | { 60 | str[size - 1] = nb % 10 + '0'; 61 | nb = nb / 10; 62 | size--; 63 | } 64 | return (str); 65 | } 66 | -------------------------------------------------------------------------------- /ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 16:56:46 by tchevrie #+# #+# */ 9 | /* Updated: 2022/10/05 04:32:47 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static size_t count_words(char const *s, char c) 16 | { 17 | size_t words; 18 | size_t i; 19 | 20 | words = 0; 21 | i = 0; 22 | while (s[i]) 23 | { 24 | if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0')) 25 | words++; 26 | i++; 27 | } 28 | return (words); 29 | } 30 | 31 | static void fill_tab(char *new, char const *s, char c) 32 | { 33 | size_t i; 34 | 35 | i = 0; 36 | while (s[i] && s[i] != c) 37 | { 38 | new[i] = s[i]; 39 | i++; 40 | } 41 | new[i] = '\0'; 42 | } 43 | 44 | static void set_mem(char **tab, char const *s, char c) 45 | { 46 | size_t count; 47 | size_t index; 48 | size_t i; 49 | 50 | index = 0; 51 | i = 0; 52 | while (s[index]) 53 | { 54 | count = 0; 55 | while (s[index + count] && s[index + count] != c) 56 | count++; 57 | if (count > 0) 58 | { 59 | tab[i] = malloc(sizeof(char) * (count + 1)); 60 | if (!tab[i]) 61 | return ; 62 | fill_tab(tab[i], (s + index), c); 63 | i++; 64 | index = index + count; 65 | } 66 | else 67 | index++; 68 | } 69 | tab[i] = 0; 70 | } 71 | 72 | char **ft_split(char const *s, char c) 73 | { 74 | size_t words; 75 | char **tab; 76 | 77 | words = count_words(s, c); 78 | tab = malloc(sizeof(char *) * (words + 1)); 79 | if (!tab) 80 | return (NULL); 81 | set_mem(tab, s, c); 82 | return (tab); 83 | } 84 | -------------------------------------------------------------------------------- /ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/12 16:23:06 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/07 16:36:04 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *find_begin(char const *s1, char const *set) 16 | { 17 | size_t i; 18 | size_t j; 19 | int in_set; 20 | 21 | in_set = 0; 22 | i = 0; 23 | j = 0; 24 | while (s1[i]) 25 | { 26 | in_set = 0; 27 | j = 0; 28 | while (set[j]) 29 | { 30 | if (s1[i] == set[j]) 31 | in_set = 1; 32 | j++; 33 | } 34 | if (!in_set) 35 | break ; 36 | i++; 37 | } 38 | return ((char *) s1 + i); 39 | } 40 | 41 | static char *find_end(char const *s1, char const *set, char const *begin) 42 | { 43 | size_t i; 44 | size_t j; 45 | int in_set; 46 | 47 | in_set = 0; 48 | i = ft_strlen(s1) - 1; 49 | j = 0; 50 | while (s1 + i >= begin) 51 | { 52 | in_set = 0; 53 | j = 0; 54 | while (set[j]) 55 | { 56 | if (s1[i] == set[j]) 57 | in_set = 1; 58 | j++; 59 | } 60 | if (!in_set) 61 | break ; 62 | i--; 63 | } 64 | if (s1 + i < begin) 65 | return ((char *) begin); 66 | return ((char *) s1 + i); 67 | } 68 | 69 | static char *fill_str(char const *begin, char const *end) 70 | { 71 | char *new; 72 | size_t i; 73 | 74 | new = malloc(sizeof(char) * (end - begin + 2)); 75 | if (!new) 76 | return (NULL); 77 | i = 0; 78 | while (begin + i <= end) 79 | { 80 | new[i] = begin[i]; 81 | i++; 82 | } 83 | new[i] = '\0'; 84 | return (new); 85 | } 86 | 87 | char *ft_strtrim(char const *s1, char const *set) 88 | { 89 | char *begin; 90 | char *end; 91 | char *new; 92 | 93 | begin = find_begin(s1, set); 94 | end = find_end(s1, set, s1); 95 | if (!s1[0] || end < begin) 96 | { 97 | new = malloc(sizeof(char) * 1); 98 | if (!new) 99 | return (NULL); 100 | new[0] = '\0'; 101 | } 102 | else 103 | new = fill_str(begin, end); 104 | if (!new) 105 | return (NULL); 106 | return (new); 107 | } 108 | -------------------------------------------------------------------------------- /libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tchevrie +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/09 15:13:35 by tchevrie #+# #+# */ 9 | /* Updated: 2022/11/08 11:03:58 by tchevrie ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | # include 21 | # include 22 | # include 23 | # include 24 | 25 | /* libc functions */ 26 | void *ft_memset(void *b, int c, size_t len); 27 | void ft_bzero(void *s, size_t n); 28 | void *ft_memcpy(void *dst, const void *src, size_t n); 29 | void *ft_memccpy(void *d, const void *s, int c, size_t n); 30 | void *ft_memmove(void *dst, const void *src, size_t len); 31 | void *ft_memchr(const void *s, int c, size_t n); 32 | int ft_memcmp(const void *s1, const void *s2, size_t n); 33 | size_t ft_strlen(const char *s); 34 | int ft_isalpha(int c); 35 | int ft_isdigit(int c); 36 | int ft_isalnum(int c); 37 | int ft_isascii(int c); 38 | int ft_isprint(int c); 39 | int ft_toupper(int c); 40 | int ft_tolower(int c); 41 | char *ft_strchr(const char *s, int c); 42 | char *ft_strrchr(const char *s, int c); 43 | int ft_strncmp(const char *s1, const char *s2, size_t n); 44 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); 45 | size_t ft_strlcat(char *d, const char *s, size_t dstsize); 46 | char *ft_strnstr(const char *haystack, const char *needle, size_t len); 47 | int ft_atoi(const char *str); 48 | void *ft_calloc(size_t count, size_t size); 49 | char *ft_strdup(const char *s1); 50 | 51 | /* additional functions */ 52 | char *ft_substr(char const *s, unsigned int start, size_t len); 53 | char *ft_strjoin(char const *s1, char const *s2); 54 | char *ft_strtrim(char const *s1, char const *set); 55 | char **ft_split(char const *s, char c); 56 | char *ft_itoa(int n); 57 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 58 | void ft_putchar_fd(char c, int fd); 59 | void ft_putstr_fd(char const *s, int fd); 60 | void ft_putendl_fd(char const *s, int fd); 61 | void ft_putnbr_fd(int n, int fd); 62 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 63 | 64 | /* Bonus */ 65 | typedef struct s_list 66 | { 67 | void *content; 68 | struct s_list *next; 69 | } t_list; 70 | 71 | t_list *ft_lstnew(void *content); 72 | void ft_lstadd_front(t_list **alst, t_list *new); 73 | int ft_lstsize(t_list *lst); 74 | t_list *ft_lstlast(t_list *lst); 75 | void ft_lstadd_back(t_list **alst, t_list *new); 76 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 77 | void ft_lstclear(t_list **lst, void (*del)(void *)); 78 | void ft_lstiter(t_list *lst, void (*f)(void *)); 79 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); 80 | 81 | #endif --------------------------------------------------------------------------------