├── .gitignore ├── README.md ├── .DS_Store ├── Makefile ├── ft_putchar_fd.c ├── ft_isascii.c ├── ft_isdigit.c ├── ft_isprint.c ├── ft_lstdelone.c ├── ft_lstadd_front.c ├── ft_putstr_fd.c ├── ft_tolower.c ├── ft_toupper.c ├── ft_isalpha.c ├── ft_strlen.c ├── ft_putendl_fd.c ├── ft_lstlast.c ├── ft_isalnum.c ├── ft_bzero.c ├── ft_lstsize.c ├── ft_lstiter.c ├── ft_memset.c ├── ft_striteri.c ├── ft_calloc.c ├── ft_lstnew.c ├── ft_lstadd_back.c ├── ft_memcpy.c ├── ft_lstclear.c ├── ft_strncmp.c ├── ft_memcmp.c ├── ft_memchr.c ├── ft_strchr.c ├── ft_strdup.c ├── ft_strlcpy.c ├── ft_strrchr.c ├── ft_putnbr_fd.c ├── ft_memmove.c ├── ft_strmapi.c ├── ft_strlcat.c ├── ft_strjoin.c ├── ft_lstmap.c ├── ft_atoi.c ├── ft_strnstr.c ├── ft_substr.c ├── ft_itoa.c ├── ft_strtrim.c ├── ft_split.c └── libft.h /.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libft-42 -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anasjaidi/libft-42/HEAD/.DS_Store -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SRCS = ft_itoa.c ft_split.c ft_strmapi.c ft_putchar_fd.c ft_putstr_fd.c ft_striteri.c ft_putendl_fd.c ft_putnbr_fd.c ft_strtrim.c ft_strjoin.c ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c ft_isprint.c ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_strchr.c ft_strdup.c ft_strlcat.c ft_strlcpy.c ft_strlen.c ft_strncmp.c ft_strnstr.c ft_strrchr.c ft_tolower.c ft_toupper.c ft_substr.c 2 | SRCS_B = ft_lstmap.c ft_lstnew.c ft_lstiter.c ft_lstsize.c ft_lstlast.c ft_lstadd_front.c ft_lstadd_back.c ft_lstclear.c ft_lstdelone.c 3 | OBJS = $(SRCS:.c=.o) 4 | OBJS_B = $(SRCS_B:.c=.o) 5 | CC = gcc 6 | CFLAGC = -Wall -Wextra -Werror 7 | NAME = libft.a 8 | 9 | .PHONY: all clean fclean re bonus 10 | 11 | all: $(NAME) 12 | 13 | $(NAME): $(OBJS) 14 | ar -crs $@ $^ 15 | bonus: $(OBJS_B) $(OBJS) 16 | ar -crs $(NAME) $^ 17 | 18 | %.o: %.c 19 | $(CC) $(CFLAGS) -c $^ -o $@ 20 | 21 | clean: 22 | rm -rf $(OBJS) $(OBJS_B) 23 | fclean: clean 24 | rm -rf $(NAME) 25 | re: fclean all 26 | -------------------------------------------------------------------------------- /ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/06 17:03:54 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/12 22:45:00 by ajaidi ### ########.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: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 11:54:24 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/01 11:59:57 by ajaidi ### ########.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: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 11:33:58 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/01 11:37:53 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | if (c >= 48 && c <= 57) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 12:06:35 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/01 12:10:39 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | if (c >= 32 && c < 127) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 21:58:52 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/11 21:58:56 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list *lst, void (*del)(void*)) 16 | { 17 | del(lst->content); 18 | del(lst); 19 | } 20 | -------------------------------------------------------------------------------- /ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 21:24:43 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/11 21:24:44 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **lst, t_list *new) 16 | { 17 | new->next = *lst; 18 | *lst = new; 19 | } 20 | -------------------------------------------------------------------------------- /ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/06 17:24:19 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/07 20:38:15 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char *s, int fd) 16 | { 17 | if (!s) 18 | return ; 19 | while (*s) 20 | write(fd, s++, 1); 21 | } 22 | -------------------------------------------------------------------------------- /ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 13:24:02 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/02 13:24:27 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (c >= 65 && c <= 90) 18 | { 19 | return (c + 32); 20 | } 21 | return (c); 22 | } 23 | -------------------------------------------------------------------------------- /ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 13:17:10 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/02 13:17:11 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (c >= 97 && c <= 122) 18 | { 19 | return (c - 32); 20 | } 21 | return (c); 22 | } 23 | -------------------------------------------------------------------------------- /ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 11:16:29 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/01 11:24:31 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 12:11:43 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/01 12:15:19 by ajaidi ### ########.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_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/06 17:31:47 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/07 20:37:58 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char *s, int fd) 16 | { 17 | if (!s) 18 | return ; 19 | ft_putstr_fd(s, fd); 20 | ft_putchar_fd('\n', fd); 21 | } 22 | -------------------------------------------------------------------------------- /ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 21:35:37 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/11 21:35:38 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | if (!lst) 18 | return (NULL); 19 | while (lst->next) 20 | lst = lst->next; 21 | return (lst); 22 | } 23 | -------------------------------------------------------------------------------- /ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 11:43:16 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/01 11:49:14 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 13:31:53 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/01 13:37:35 by ajaidi ### ########.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 | if (!n) 21 | return ; 22 | while (i < n) 23 | *(char *)(s + i++) = 0; 24 | } 25 | -------------------------------------------------------------------------------- /ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 21:30:55 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/11 21:30:56 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list *lst) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (lst) 21 | { 22 | lst = lst->next; 23 | i++; 24 | } 25 | return (i); 26 | } 27 | -------------------------------------------------------------------------------- /ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 23:06:35 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/11 23:06:36 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | if (!lst) 18 | return ; 19 | while (lst) 20 | { 21 | f(lst->content); 22 | lst = lst->next; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 13:14:59 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/01 14:08:48 by ajaidi ### ########.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 | i = 0; 20 | while (i < len) 21 | *(char *)(b + i++) = (unsigned int)c; 22 | return (b); 23 | } 24 | -------------------------------------------------------------------------------- /ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/07 18:24:11 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/07 19:47:10 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char*)) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (!s) 21 | return ; 22 | while (s[i]) 23 | { 24 | f(i, (s + i)); 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 14:19:06 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/03 14:19:08 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc(size_t count, size_t size) 16 | { 17 | void *p; 18 | 19 | p = malloc(count * size); 20 | if (!p) 21 | { 22 | return (NULL); 23 | } 24 | ft_bzero(p, size * count); 25 | return (p); 26 | } 27 | -------------------------------------------------------------------------------- /ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 20:08:24 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/11 20:08:25 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void *content) 16 | { 17 | t_list *new; 18 | 19 | new = (t_list *)malloc(sizeof(t_list)); 20 | if (!new) 21 | return (NULL); 22 | new->content = content; 23 | new->next = NULL; 24 | return (new); 25 | } 26 | -------------------------------------------------------------------------------- /ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 21:43:14 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/11 21:43:16 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list **lst, t_list *new) 16 | { 17 | t_list *p; 18 | 19 | if (!(*lst)) 20 | { 21 | *lst = new; 22 | return ; 23 | } 24 | p = *lst; 25 | while (p->next) 26 | p = p->next; 27 | p->next = new; 28 | } 29 | -------------------------------------------------------------------------------- /ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/01 13:41:42 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/06 09:30:39 by ajaidi ### ########.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 | if (!dst && !src) 20 | return (NULL); 21 | i = -1; 22 | while (++i < n) 23 | *(char *)(dst + i) = *(char *)(src + i); 24 | return (dst); 25 | } 26 | -------------------------------------------------------------------------------- /ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 22:04:28 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/11 22:04:28 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list **lst, void (*del)(void*)) 16 | { 17 | t_list *p; 18 | t_list *q; 19 | 20 | p = *lst; 21 | if (!p) 22 | return ; 23 | while (p) 24 | { 25 | q = p; 26 | p = p->next; 27 | del(q->content); 28 | free(q); 29 | } 30 | *lst = NULL; 31 | } 32 | -------------------------------------------------------------------------------- /ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 14:17:30 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/05 18:35:48 by ajaidi ### ########.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 | if (n == 0) 21 | return (0); 22 | while (s1[i] && s2[i] && (s1[i] == s2[i]) && i < n - 1) 23 | i++; 24 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 25 | } 26 | -------------------------------------------------------------------------------- /ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 17:06:48 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/02 17:06:54 by ajaidi ### ########.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 | if (!n) 21 | return (0); 22 | while (*(char *)(s1 + i) == *(char *)(s2 + i) && i < n - 1) 23 | i++; 24 | return (*(unsigned char *)(s1 + i) - *(unsigned char *)(s2 + i)); 25 | } 26 | -------------------------------------------------------------------------------- /ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 15:38:59 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/02 15:46:56 by ajaidi ### ########.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 | if (!n) 21 | return (NULL); 22 | while (n--) 23 | { 24 | if (*(unsigned char *)(s + i) == (unsigned char)c) 25 | return ((void *)(s + i)); 26 | i++; 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 13:32:57 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/02 13:32:59 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | { 22 | if (s[i] == (unsigned char)c) 23 | return ((char *)(s + i)); 24 | i++; 25 | } 26 | if (s[i] == (unsigned char)c) 27 | return ((char *)(s + i)); 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 15:29:09 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/05 16:38:36 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | int i; 18 | char *ptr; 19 | 20 | i = 0; 21 | while (s1[i]) 22 | i++; 23 | ptr = (char *)malloc(i * sizeof(char) + 1); 24 | if (!ptr) 25 | return (NULL); 26 | i = -1; 27 | while (s1[++i]) 28 | ptr[i] = s1[i]; 29 | ptr[i] = 0; 30 | return (ptr); 31 | } 32 | -------------------------------------------------------------------------------- /ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 11:18:45 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/02 11:18:47 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, 16 | const char *src, size_t dstsize) 17 | { 18 | size_t i; 19 | size_t j; 20 | 21 | i = 0; 22 | j = -1; 23 | i = ft_strlen(src); 24 | if (!dstsize) 25 | return (i); 26 | while (*src && ++j < dstsize - 1) 27 | *dst++ = *src++; 28 | *dst = 0; 29 | return (i); 30 | } 31 | -------------------------------------------------------------------------------- /ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 13:41:48 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/05 18:22:11 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | i++; 22 | i++; 23 | while (--i) 24 | { 25 | if (s[i] == (unsigned char)c) 26 | return ((char *)(s + i)); 27 | } 28 | if (s[0] == (unsigned char)c) 29 | return ((char *)s); 30 | return (NULL); 31 | } 32 | -------------------------------------------------------------------------------- /ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/07 18:37:35 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/07 19:55:23 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | long nbr; 18 | 19 | nbr = n; 20 | if (nbr < 0) 21 | { 22 | write(fd, "-", 1); 23 | nbr *= -1; 24 | } 25 | if (nbr >= 10) 26 | { 27 | ft_putnbr_fd(nbr / 10, fd); 28 | ft_putnbr_fd(nbr % 10, fd); 29 | } 30 | else if (nbr < 10) 31 | { 32 | ft_putchar_fd(nbr + 48, fd); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 11:14:50 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/04 15:16:23 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | if (dst > src) 21 | { 22 | while (len--) 23 | *(char *)(dst + len) = *(char *)(src + len); 24 | } 25 | else if (src > dst) 26 | { 27 | while (++i < len) 28 | *(char *)(dst + i) = *(char *)(src + i); 29 | } 30 | return (dst); 31 | } 32 | -------------------------------------------------------------------------------- /ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/07 17:54:25 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/13 17:16:41 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | char *r; 18 | int i; 19 | 20 | if (!s || !f) 21 | return (NULL); 22 | i = 0; 23 | r = (char *)malloc(ft_strlen(s) * sizeof(char) + 1); 24 | if (!r) 25 | return (NULL); 26 | while (s[i]) 27 | { 28 | r[i] = f(i, s[i]); 29 | i++; 30 | } 31 | r[i] = 0; 32 | return (r); 33 | } 34 | -------------------------------------------------------------------------------- /ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 11:49:07 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/05 18:44:56 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t d; 18 | size_t s; 19 | size_t rd; 20 | 21 | d = ft_strlen(dst); 22 | rd = d; 23 | s = ft_strlen(src); 24 | if (d >= dstsize || !dstsize) 25 | return (dstsize + s); 26 | while (*src && d < dstsize - 1) 27 | { 28 | dst[d] = *src; 29 | d++; 30 | src++; 31 | } 32 | dst[d] = '\0'; 33 | return (rd + s); 34 | } 35 | -------------------------------------------------------------------------------- /ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/05 23:07:57 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/07 20:31:39 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | int i1; 18 | int i2; 19 | char *ptr; 20 | 21 | if (!s1 || !s2) 22 | return (NULL); 23 | i1 = ft_strlen(s1); 24 | i2 = ft_strlen(s2); 25 | ptr = (char *)malloc((i1 + i2) * sizeof(char) + 1); 26 | if (!ptr) 27 | return (NULL); 28 | ft_memmove(ptr, s1, i1); 29 | ft_memmove(ptr + i1, s2, i2 + 1); 30 | ptr[i1 + i2] = 0; 31 | return (ptr); 32 | } 33 | -------------------------------------------------------------------------------- /ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/11 23:21:24 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/13 04:05:42 by ajaidi ### ########.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 *root; 18 | t_list *node; 19 | 20 | if (!lst) 21 | return (NULL); 22 | root = NULL; 23 | while (lst) 24 | { 25 | node = ft_lstnew(f(lst->content)); 26 | if (!node) 27 | { 28 | ft_lstclear(&root, del); 29 | return (NULL); 30 | } 31 | ft_lstadd_back(&root, node); 32 | lst = lst->next; 33 | } 34 | return (root); 35 | } 36 | -------------------------------------------------------------------------------- /ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 17:38:16 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/03 11:09:06 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | int i; 18 | int s; 19 | int r; 20 | 21 | i = 0; 22 | s = 1; 23 | r = 0; 24 | while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32) 25 | i++; 26 | if (str[i] == '-' || str[i] == '+') 27 | { 28 | if (str[i] == '-') 29 | s *= -1; 30 | i++; 31 | } 32 | while (str[i] >= 48 && str[i] <= 57) 33 | { 34 | r *= 10; 35 | r += str[i] - 48; 36 | i++; 37 | } 38 | return (r * s); 39 | } 40 | -------------------------------------------------------------------------------- /ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/02 17:21:47 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/05 17:49:00 by ajaidi ### ########.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 i; 18 | int j; 19 | 20 | i = 0; 21 | if (needle[0] == '\0') 22 | return ((char *)(haystack)); 23 | while (haystack[i] && i < len) 24 | { 25 | j = 0; 26 | while (haystack[i + j] == needle[j] && i + j < len) 27 | { 28 | if (needle[j + 1] == '\0') 29 | return ((char *)(haystack + i)); 30 | j++; 31 | } 32 | i++; 33 | } 34 | return (NULL); 35 | } 36 | -------------------------------------------------------------------------------- /ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/05 22:34:20 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/12 18:07:02 by ajaidi ### ########.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 *ptr; 18 | size_t i; 19 | 20 | i = -1; 21 | if (!s) 22 | return (NULL); 23 | if (start >= ft_strlen(s)) 24 | { 25 | ptr = malloc(1); 26 | ptr[0] = 0; 27 | return (ptr); 28 | } 29 | if (len > (ft_strlen(s) - start)) 30 | ptr = (char *)malloc(ft_strlen(s) - start + 1); 31 | else 32 | ptr = (char *)malloc(len + 1); 33 | if (!ptr) 34 | return (NULL); 35 | while (s[start + ++i] && i < len) 36 | ptr[i] = s[start + i]; 37 | ptr[i] = 0; 38 | return (ptr); 39 | } 40 | -------------------------------------------------------------------------------- /ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/07 22:38:20 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/08 01:53:55 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | #include 16 | 17 | void ft_nbr(long long n, int len, char *r) 18 | { 19 | int i; 20 | 21 | i = 0; 22 | while (n) 23 | { 24 | r[len--] = 48 + (n % 10); 25 | n /= 10; 26 | } 27 | } 28 | 29 | int ft_len(int n) 30 | { 31 | int c; 32 | 33 | c = 0; 34 | if (n < 0) 35 | c++; 36 | while (n) 37 | { 38 | n /= 10; 39 | c++; 40 | } 41 | return (c); 42 | } 43 | 44 | char *ft_itoa(int n) 45 | { 46 | long long nb; 47 | int len; 48 | char *r; 49 | 50 | len = ft_len(n); 51 | if (!len) 52 | { 53 | r = (char *)malloc(2); 54 | r[0] = '0'; 55 | r[1] = 0; 56 | return (r); 57 | } 58 | r = (char *)malloc(len * sizeof(char) + 1); 59 | if (!r) 60 | return (NULL); 61 | r[len--] = 0; 62 | nb = n; 63 | if (nb < 0) 64 | { 65 | nb *= -1; 66 | r[0] = '-'; 67 | } 68 | ft_nbr(nb, len, r); 69 | return (r); 70 | } 71 | -------------------------------------------------------------------------------- /ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/06 00:30:59 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/07 19:56:31 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lens(char *s1, char *s2) 16 | { 17 | int i; 18 | int j; 19 | int c; 20 | 21 | i = -1; 22 | c = 0; 23 | while (s1[++i]) 24 | { 25 | j = -1; 26 | while (s2[++j]) 27 | { 28 | if (s1[i] == s2[j]) 29 | { 30 | c++; 31 | break ; 32 | } 33 | if (s2[j + 1] == '\0') 34 | return (c); 35 | } 36 | } 37 | return (c); 38 | } 39 | 40 | int ft_lene(char *s1, char *s2) 41 | { 42 | int s; 43 | int j; 44 | int c; 45 | 46 | s = ft_strlen(s1); 47 | c = 0; 48 | while (--s) 49 | { 50 | j = -1; 51 | while (s2[++j]) 52 | { 53 | if (s1[s] == s2[j]) 54 | { 55 | c++; 56 | break ; 57 | } 58 | if (s2[j + 1] == '\0') 59 | return (c); 60 | } 61 | } 62 | return (c); 63 | } 64 | 65 | char *ft_strtrim(char const *s1, char const *set) 66 | { 67 | int s; 68 | int c1; 69 | int c2; 70 | char *ptr; 71 | 72 | if (!s1 || !set) 73 | return (NULL); 74 | c1 = ft_lens((char *)s1, (char *)set); 75 | c2 = ft_lene((char *)s1, (char *)set); 76 | s = ft_strlen(s1); 77 | if (s == c1) 78 | return (ft_strdup("")); 79 | ptr = (char *)malloc((s - (c1 + c2)) * sizeof(char) + 1); 80 | if (!ptr) 81 | return (NULL); 82 | ft_strlcpy(ptr, s1 + c1, s - (c1 + c2) + 1); 83 | return (ptr); 84 | } 85 | -------------------------------------------------------------------------------- /ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/07 19:57:03 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/13 01:16:32 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_is_separator(char c, char cr) 16 | { 17 | if (c == cr) 18 | return (1); 19 | if (c == '\0') 20 | return (1); 21 | return (0); 22 | } 23 | 24 | int ft_words(char *str, char c) 25 | { 26 | int i; 27 | int w; 28 | 29 | i = 0; 30 | w = 0; 31 | while (str[i] != '\0') 32 | { 33 | if (ft_is_separator(str[i], c) == 0 34 | && ft_is_separator(str[i + 1], c) == 1) 35 | w++; 36 | i++; 37 | } 38 | return (w); 39 | } 40 | 41 | void ft_write_word(char *dest, char *src, char c) 42 | { 43 | int i; 44 | 45 | i = 0; 46 | while (ft_is_separator(src[i], c) == 0) 47 | { 48 | dest[i] = src[i]; 49 | i++; 50 | } 51 | dest[i] = '\0'; 52 | } 53 | 54 | int ft_write_split(char **split, char *str, char c) 55 | { 56 | int i; 57 | int j; 58 | int w; 59 | 60 | i = 0; 61 | w = 0; 62 | while (str[i] != '\0') 63 | { 64 | if (ft_is_separator(str[i], c) == 1) 65 | i++; 66 | else 67 | { 68 | j = 0; 69 | while (ft_is_separator(str[i + j], c) == 0) 70 | j++; 71 | split[w] = (char *)malloc(sizeof(char) * (j + 1)); 72 | if (!(split + w)) 73 | return (0); 74 | ft_write_word(split[w], str + i, c); 75 | i += j; 76 | w++; 77 | } 78 | } 79 | return (1); 80 | } 81 | 82 | char **ft_split(char const *s, char c) 83 | { 84 | int w; 85 | char **rtn; 86 | 87 | if (!s) 88 | return (NULL); 89 | w = ft_words((char *)s, c); 90 | rtn = (char **)malloc(sizeof(char *) * (w + 1)); 91 | if (!rtn) 92 | return (NULL); 93 | if (!(ft_write_split(rtn, (char *) s, c))) 94 | { 95 | w = -1; 96 | while (rtn + ++w) 97 | free(rtn + w); 98 | free(rtn); 99 | } 100 | rtn[w] = 0; 101 | return (rtn); 102 | } 103 | -------------------------------------------------------------------------------- /libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ajaidi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/04 20:25:55 by ajaidi #+# #+# */ 9 | /* Updated: 2021/11/13 03:56:06 by ajaidi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | typedef struct s_list { 21 | void *content; 22 | struct s_list *next; 23 | } t_list; 24 | void ft_lstdelone(t_list *lst, void (*del)(void*)); 25 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); 26 | void ft_lstiter(t_list *lst, void (*f)(void *)); 27 | void ft_lstadd_front(t_list **lst, t_list *new); 28 | t_list *ft_lstnew(void *content); 29 | int ft_lstsize(t_list *lst); 30 | void ft_lstclear(t_list **lst, void (*del)(void*)); 31 | t_list *ft_lstlast(t_list *lst); 32 | void ft_lstadd_back(t_list **lst, t_list *new); 33 | void ft_striteri(char *s, void (*f)(unsigned int, char*)); 34 | char *ft_itoa(int n); 35 | void ft_bzero(void *s, size_t n); 36 | void ft_putnbr_fd(int n, int fd); 37 | void ft_putstr_fd(char *s, int fd); 38 | void ft_putchar_fd(char c, int fd); 39 | void ft_putendl_fd(char *s, int fd); 40 | char **ft_split(char const *s, char c); 41 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 42 | char *ft_strtrim(char const *s1, char const *set); 43 | int ft_atoi(const char *str); 44 | void *ft_calloc(size_t count, size_t size); 45 | int ft_isalpha(int c); 46 | int ft_isalnum(int c); 47 | int ft_isascii(int c); 48 | int ft_isdigit(int c); 49 | int ft_isprint(int c); 50 | void *ft_memchr(const void *s, int c, size_t n); 51 | int ft_memcmp(const void *s1, const void *s2, size_t n); 52 | void *ft_memcpy(void *dst, const void *src, size_t n); 53 | void *ft_memmove(void *dst, const void *src, size_t len); 54 | void *ft_memset(void *b, int c, size_t len); 55 | char *ft_strchr(const char *s, int c); 56 | char *ft_strdup(const char *s1); 57 | char *ft_substr(char const *s, unsigned int start, size_t len); 58 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize); 59 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); 60 | size_t ft_strlen(const char *s); 61 | int ft_strncmp(const char *s1, const char *s2, size_t n); 62 | char *ft_strnstr(const char *haystack, const char *needle, size_t len); 63 | char *ft_strjoin(char const *s1, char const *s2); 64 | char *ft_strrchr(const char *s, int c); 65 | int ft_tolower(int c); 66 | int ft_toupper(int c); 67 | 68 | #endif 69 | --------------------------------------------------------------------------------