├── .gitignore ├── README.md ├── en.subject_libft_v15.pdf └── libft ├── Makefile ├── 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_itoa.c ├── ft_lstadd_back.c ├── ft_lstadd_front.c ├── ft_lstclear.c ├── ft_lstdelone.c ├── ft_lstiter.c ├── ft_lstlast.c ├── ft_lstmap.c ├── ft_lstnew.c ├── ft_lstsize.c ├── ft_memccpy.c ├── ft_memchr.c ├── ft_memcmp.c ├── ft_memcpy.c ├── ft_memmove.c ├── ft_memset.c ├── ft_putchar_fd.c ├── ft_putendl_fd.c ├── ft_putnbr_fd.c ├── ft_putstr_fd.c ├── ft_split.c ├── ft_strchr.c ├── ft_strdup.c ├── ft_striteri.c ├── ft_strjoin.c ├── ft_strlcat.c ├── ft_strlcpy.c ├── ft_strlen.c ├── ft_strmapi.c ├── ft_strncmp.c ├── ft_strnstr.c ├── ft_strrchr.c ├── ft_strtrim.c ├── ft_substr.c ├── ft_tolower.c ├── ft_toupper.c └── libft.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libft 2 | 3 | The first project of the School 42. 4 | 5 | Linted according to the `norminette` v3.3.51 rules. 6 | 7 | ## Testers 8 | 9 | **Внимание**: тестеры пишут люди, в них могут быть ошибки и скорее всего так и есть. 10 | 11 | * [libft-unit-test](https://github.com/alelievr/libft-unit-test) 12 | 13 | * [libftTester](https://github.com/Tripouille/libftTester) 14 | 15 | * [libft-war-machine](https://github.com/ska42/libft-war-machine) 16 | 17 | It is normal (!) if some of your functions are not protected. 18 | 19 | ## Libftx 20 | 21 | [Libftx](https://github.com/stankudrow/libftx) is an extended C library for the School 42 projects. 22 | -------------------------------------------------------------------------------- /en.subject_libft_v15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stankudrow/libft/b24812679dbf41be0d18883c4013d383419bfef9/en.subject_libft_v15.pdf -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | NAME := libft.a 2 | LIBSO := $(NAME:.a=.so) # for some testers 3 | 4 | # Files 5 | HEADER := $(NAME:.a=.h) 6 | 7 | MSRCS := ft_isalpha.c ft_isdigit.c ft_isalnum.c \ 8 | ft_isascii.c ft_isprint.c ft_strlen.c ft_memset.c \ 9 | ft_bzero.c ft_memcpy.c ft_memmove.c ft_strlcpy.c \ 10 | ft_strlcat.c ft_toupper.c ft_tolower.c ft_strchr.c \ 11 | ft_strrchr.c ft_strncmp.c ft_memchr.c ft_memcmp.c \ 12 | ft_strnstr.c ft_atoi.c ft_calloc.c ft_strdup.c 13 | ASRCS := ft_substr.c ft_strjoin.c ft_strtrim.c \ 14 | ft_split.c ft_itoa.c ft_strmapi.c ft_striteri.c \ 15 | ft_putchar_fd.c ft_putendl_fd.c ft_putnbr_fd.c ft_putstr_fd.c \ 16 | ft_memccpy.c 17 | SRCS := $(MSRCS) $(ASRCS) 18 | SRCS_BONUS := ft_lstadd_back.c ft_lstadd_front.c ft_lstclear.c \ 19 | ft_lstdelone.c ft_lstiter.c ft_lstlast.c ft_lstmap.c ft_lstnew.c ft_lstsize.c 20 | 21 | OBJS := $(SRCS:.c=.o) 22 | OBJS_BONUS := $(SRCS_BONUS:.c=.o) 23 | 24 | # Compilation flags 25 | CC := cc 26 | CFLAGS := -Wall -Werror -Wextra 27 | 28 | .PHONY: all clean fclean re so bonus 29 | 30 | all: $(NAME) 31 | 32 | $(NAME): $(OBJS) 33 | ar crs $(NAME) $(OBJS) 34 | 35 | %.o: %.c $(HEADER) 36 | $(CC) $(CFLAGS) -c $< -o $@ 37 | 38 | clean: 39 | rm -f $(OBJS) $(OBJS_BONUS) 40 | 41 | fclean: clean 42 | rm -f $(NAME) 43 | rm -f $(LIBSO) 44 | 45 | re: fclean all 46 | 47 | so: bonus 48 | $(CC) -nostartfiles -shared -o $(LIBSO) -fPIC $(SRCS) $(SRCS_BONUS) 49 | 50 | bonus: $(NAME) $(OBJS_BONUS) 51 | ar crs $(NAME) $(OBJS_BONUS) -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/12 01:00:42 by stanislav #+# #+# */ 9 | /* Updated: 2021/12/26 17:02:24 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include "libft.h" 15 | 16 | static int ft_isspace(int c) 17 | { 18 | return ((c > 8 && c < 14) || c == 32); 19 | } 20 | 21 | static void ft_skip(const char **nptr, char *sign) 22 | { 23 | while (ft_isspace(**nptr)) 24 | (*nptr)++; 25 | if (**nptr == '-' || **nptr == '+') 26 | { 27 | if (**nptr == '-') 28 | *sign = -1; 29 | (*nptr)++; 30 | } 31 | while (**nptr == '0') 32 | (*nptr)++; 33 | } 34 | 35 | // nbr is an always positive number while in the loop of ft_atoi 36 | static int ft_isoverflow(long nbr, unsigned char digit, char sign) 37 | { 38 | if (sign < 0) 39 | return ((LONG_MIN / 10 > nbr) || \ 40 | (LONG_MIN + digit > -10 * nbr)); 41 | if (sign > 0) 42 | return ((LONG_MAX / 10 < nbr) || \ 43 | (LONG_MAX - digit < 10 * nbr)); 44 | return (-1); 45 | } 46 | 47 | // libft-unit-test - that is why such an implementation 48 | int ft_atoi(const char *nptr) 49 | { 50 | long nbr; 51 | char sign; 52 | 53 | sign = 1; 54 | nbr = 0; 55 | ft_skip(&nptr, &sign); 56 | while (ft_isdigit(*nptr)) 57 | { 58 | if (ft_isoverflow(nbr, *nptr - '0', sign)) 59 | { 60 | if (sign == 1) 61 | return (-1); 62 | return (0); 63 | } 64 | nbr = 10 * nbr + (*nptr - '0'); 65 | nptr++; 66 | } 67 | return (sign * nbr); 68 | } 69 | -------------------------------------------------------------------------------- /libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:25:01 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:25:02 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | ft_memset(s, 0, n); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:24:44 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/09 01:29:00 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | // thanks to @jbelinda and @thera for insights 16 | void *ft_calloc(size_t nmemb, size_t size) 17 | { 18 | void *mem; 19 | size_t total; 20 | 21 | if (!nmemb || !size) 22 | { 23 | nmemb = 1; 24 | size = 1; 25 | } 26 | if (size > (size_t)-1 / nmemb) 27 | return (NULL); 28 | total = nmemb * size; 29 | mem = malloc(total); 30 | if (mem) 31 | ft_memset(mem, 0, total); 32 | return (mem); 33 | } 34 | // ft_bzero(mem, total); -> ft_memset(mem, 0, total); -------------------------------------------------------------------------------- /libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:24:38 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:24:39 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | return (ft_isalpha(c) || ft_isdigit(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:24:31 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:24:31 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | static int ft_islower(int c) 14 | { 15 | return (c >= 'a' && c <= 'z'); 16 | } 17 | 18 | static int ft_isupper(int c) 19 | { 20 | return (c >= 'A' && c <= 'Z'); 21 | } 22 | 23 | int ft_isalpha(int c) 24 | { 25 | return (ft_islower(c) || ft_isupper(c)); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:24:21 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:24:21 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_isascii(int c) 14 | { 15 | return (c >= 0 && c <= 127); 16 | } 17 | -------------------------------------------------------------------------------- /libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:24:13 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:24:14 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_isdigit(int c) 14 | { 15 | return (c >= '0' && c <= '9'); 16 | } 17 | -------------------------------------------------------------------------------- /libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:24:08 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:24:08 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_isprint(int c) 14 | { 15 | return (c > 31 && c < 127); 16 | } 17 | -------------------------------------------------------------------------------- /libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:24:01 by stanislav #+# #+# */ 9 | /* Updated: 2022/02/05 22:32:36 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static short ft_get_numsign(int nbr) 16 | { 17 | if (nbr < 0) 18 | return (-1); 19 | return (1); 20 | } 21 | 22 | static size_t ft_get_numlen(int nbr) 23 | { 24 | size_t len; 25 | 26 | len = 0; 27 | if (nbr <= 0) 28 | len++; 29 | while (nbr) 30 | { 31 | nbr /= 10; 32 | len++; 33 | } 34 | return (len); 35 | } 36 | 37 | static void ft_strrev(char *s) 38 | { 39 | char *left; 40 | char tmp; 41 | 42 | left = s; 43 | while (*s) 44 | s++; 45 | while (left < --s) 46 | { 47 | tmp = *left; 48 | *left++ = *s; 49 | *s = tmp; 50 | } 51 | } 52 | 53 | char *ft_itoa(int nbr) 54 | { 55 | char *str; 56 | size_t len; 57 | size_t pos; 58 | short sign; 59 | 60 | sign = ft_get_numsign(nbr); 61 | len = ft_get_numlen(nbr); 62 | str = ft_calloc((len + 1), sizeof(char)); 63 | if (!str) 64 | return (NULL); 65 | pos = 0; 66 | while (nbr) 67 | { 68 | str[pos++] = sign * (nbr % 10) + '0'; 69 | nbr /= 10; 70 | } 71 | if (!(nbr || pos)) 72 | str[pos++] = '0'; 73 | if (sign < 0) 74 | str[pos++] = '-'; 75 | str[pos] = '\0'; 76 | ft_strrev(str); 77 | return (str); 78 | } 79 | -------------------------------------------------------------------------------- /libft/ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:31:10 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:31:11 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list **lst, t_list *new) 16 | { 17 | t_list *last; 18 | 19 | if (*lst) 20 | { 21 | last = ft_lstlast(*lst); 22 | last->next = new; 23 | } 24 | else 25 | *lst = new; 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:31:29 by stanislav #+# #+# */ 9 | /* Updated: 2022/04/04 17:34:47 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **lst, t_list *node) 16 | { 17 | t_list *last; 18 | 19 | if (*lst) 20 | { 21 | last = ft_lstlast(node); 22 | last->next = *lst; 23 | } 24 | *lst = node; 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:32:00 by stanislav #+# #+# */ 9 | /* Updated: 2022/04/04 17:35:25 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list **lst, void (*del)(void *)) 16 | { 17 | t_list *node; 18 | 19 | while (*lst) 20 | { 21 | node = *lst; 22 | *lst = (*lst)->next; 23 | ft_lstdelone(node, del); 24 | node = NULL; 25 | } 26 | *lst = NULL; 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:32:13 by stanislav #+# #+# */ 9 | /* Updated: 2022/04/04 17:35:43 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 16 | { 17 | if (del) 18 | (*del)(lst->content); 19 | free(lst); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:32:46 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:32:47 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | while (lst) 18 | { 19 | (*f)(lst->content); 20 | lst = lst->next; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:33:04 by stanislav #+# #+# */ 9 | /* Updated: 2022/04/04 17:40:05 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | if (lst) 18 | { 19 | while (lst->next) 20 | lst = lst->next; 21 | } 22 | return (lst); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:33:15 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:33:16 by stanislav ### ########.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 *newlst; 18 | t_list *node; 19 | 20 | newlst = NULL; 21 | while (lst) 22 | { 23 | node = ft_lstnew((*f)(lst->content)); 24 | if (!node) 25 | { 26 | ft_lstclear(&newlst, del); 27 | return (NULL); 28 | } 29 | ft_lstadd_back(&newlst, node); 30 | lst = lst->next; 31 | } 32 | return (newlst); 33 | } 34 | -------------------------------------------------------------------------------- /libft/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:33:40 by stanislav #+# #+# */ 9 | /* Updated: 2022/02/05 22:32:50 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void *content) 16 | { 17 | t_list *node; 18 | 19 | node = ft_calloc(1, sizeof(t_list)); 20 | if (node) 21 | { 22 | node->content = content; 23 | node->next = NULL; 24 | } 25 | return (node); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:33:49 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:33:49 by stanislav ### ########.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 | lst = lst->next; 23 | size++; 24 | } 25 | return (size); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/02/23 19:48:47 by stanislav #+# #+# */ 9 | /* Updated: 2022/04/04 17:41:43 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 16 | { 17 | unsigned char *dest; 18 | const unsigned char *source; 19 | unsigned char chr; 20 | 21 | if (dst == src) 22 | return (dst); 23 | dest = dst; 24 | source = src; 25 | chr = c; 26 | while (n--) 27 | { 28 | *dest++ = *source++; 29 | if (*(dest - 1) == chr) 30 | return ((void *)dest); 31 | } 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:18:07 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/09 22:28:58 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | const unsigned char *ma; 18 | unsigned char chr; 19 | 20 | ma = s; 21 | chr = c; 22 | while (n--) 23 | if (*ma++ == chr) 24 | return ((void *)--ma); 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:17:58 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/09 22:30:19 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | const unsigned char *ma1; 18 | const unsigned char *ma2; 19 | 20 | ma1 = s1; 21 | ma2 = s2; 22 | while (n--) 23 | if (*ma1++ != *ma2++) 24 | return (*--ma1 - *--ma2); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:17:52 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:17:52 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | unsigned char *dest; 18 | const unsigned char *source; 19 | 20 | if (dst == src) 21 | return (dst); 22 | dest = dst; 23 | source = src; 24 | while (n--) 25 | *dest++ = *source++; 26 | return (dst); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:17:45 by stanislav #+# #+# */ 9 | /* Updated: 2022/05/03 12:44:04 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t n) 16 | { 17 | unsigned char *dest; 18 | const unsigned char *source; 19 | 20 | if (dst == src) 21 | return (dst); 22 | dest = dst; 23 | source = src; 24 | if (src < dst && dst - src < (long long int) n) 25 | while (n--) 26 | dest[n] = source[n]; 27 | else 28 | ft_memcpy(dst, src, n); 29 | return (dst); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:17:37 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:17:37 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *s, int c, size_t n) 16 | { 17 | unsigned char *ma; 18 | unsigned char chr; 19 | 20 | ma = s; 21 | chr = c; 22 | while (n--) 23 | *ma++ = chr; 24 | return (s); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:17:30 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/11 01:51:56 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include "libft.h" 15 | 16 | void ft_putchar_fd(char c, int fd) 17 | { 18 | write(fd, &c, 1); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:17:24 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:17:24 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char *s, int fd) 16 | { 17 | ft_putstr_fd(s, fd); 18 | ft_putchar_fd('\n', fd); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:17:16 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:17:17 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int nbr, int fd) 16 | { 17 | if (nbr < 0) 18 | { 19 | ft_putchar_fd('-', fd); 20 | if (nbr <= -10) 21 | ft_putnbr_fd(nbr / -10, fd); 22 | ft_putchar_fd(-(nbr % 10) + '0', fd); 23 | } 24 | else 25 | { 26 | if (nbr >= 10) 27 | ft_putnbr_fd(nbr / 10, fd); 28 | ft_putchar_fd((nbr % 10) + '0', fd); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:17:10 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:17:11 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char *s, int fd) 16 | { 17 | while (*s) 18 | ft_putchar_fd(*s++, fd); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:10:32 by stanislav #+# #+# */ 9 | /* Updated: 2022/02/05 22:33:27 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static size_t ft_count_words(const char *str, char sep) 16 | { 17 | size_t words; 18 | 19 | words = 0; 20 | while (*str) 21 | { 22 | while (*str && *str == sep) 23 | str++; 24 | while (*str && *str != sep) 25 | { 26 | if (*(str + 1) == sep || !*(str + 1)) 27 | words++; 28 | str++; 29 | } 30 | } 31 | return (words); 32 | } 33 | 34 | static char *ft_wordnew(char const *str, unsigned int *start, char sep) 35 | { 36 | size_t len; 37 | 38 | while (str[*start] && str[*start] == sep) 39 | (*start)++; 40 | len = 0; 41 | while (str[*start] && str[*start] != sep) 42 | { 43 | (*start)++; 44 | len++; 45 | } 46 | return (ft_substr(str, *start - len, len)); 47 | } 48 | 49 | static void ft_free_words(char **strs, size_t iword) 50 | { 51 | size_t iw; 52 | 53 | iw = 0; 54 | while (iw < iword) 55 | free(strs[iw++]); 56 | free(strs); 57 | } 58 | 59 | char **ft_split(char const *s, char c) 60 | { 61 | char **words; 62 | size_t wsc; 63 | size_t iword; 64 | unsigned int start; 65 | 66 | wsc = ft_count_words(s, c); 67 | if (wsc == (size_t)-1) 68 | return (NULL); 69 | words = ft_calloc(wsc + 1, sizeof(char *)); 70 | if (!words) 71 | return (NULL); 72 | iword = -1; 73 | start = 0; 74 | while (++iword < wsc) 75 | { 76 | words[iword] = ft_wordnew(s, &start, c); 77 | if (!words[iword]) 78 | { 79 | ft_free_words(words, iword); 80 | return (NULL); 81 | } 82 | } 83 | words[iword] = NULL; 84 | return (words); 85 | } 86 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:10:42 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:10:42 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | unsigned char chr; 18 | 19 | chr = c; 20 | while (*s) 21 | { 22 | if (*s == chr) 23 | return ((char *)s); 24 | s++; 25 | } 26 | if (*s == chr) 27 | return ((char *)s); 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:10:54 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/11 01:47:48 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s) 16 | { 17 | void *str; 18 | size_t slen; 19 | 20 | slen = ft_strlen(s); 21 | if (slen == (size_t)-1) 22 | return (NULL); 23 | str = ft_calloc(slen + 1, sizeof(char)); 24 | if (str) 25 | ft_memmove(str, s, slen + 1); 26 | return (str); 27 | } 28 | // ft_strlcpy(str, s, len + 1); -> ft_memmove(str, s, len + 1); -------------------------------------------------------------------------------- /libft/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:11:05 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:11:06 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char *)) 16 | { 17 | unsigned int i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | { 22 | (*f)(i, s + i); 23 | i++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:11:25 by stanislav #+# #+# */ 9 | /* Updated: 2022/02/05 22:33:50 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_ismemoverflow(size_t len1, size_t len2) 16 | { 17 | size_t max; 18 | 19 | max = -1; 20 | if ((len1 > max - 1) || (len2 > max - 1)) 21 | return (1); 22 | if (max - (len1 + 1) < len2) 23 | return (1); 24 | return (0); 25 | } 26 | 27 | char *ft_strjoin(char const *s1, char const *s2) 28 | { 29 | char *str; 30 | size_t len1; 31 | size_t len2; 32 | 33 | len1 = ft_strlen(s1); 34 | len2 = ft_strlen(s2); 35 | if (ft_ismemoverflow(len1, len2)) 36 | return (NULL); 37 | str = ft_calloc(len1 + len2 + 1, sizeof(char)); 38 | if (str) 39 | { 40 | ft_memmove(str, s1, len1 + 1); 41 | ft_memmove(str + len1, s2, len2 + 1); 42 | } 43 | return (str); 44 | } 45 | // ft_strlcat(str, s2, len1 + len2 + 1) -> ft_strlcpy(str + len1, s2, len2 + 1); 46 | // ft_strlcpy(str, s1, len1 + 1); -> ft_memove(str, s1, len1 + 1); -------------------------------------------------------------------------------- /libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mhorton +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:11:33 by stanislav #+# #+# */ 9 | /* Updated: 2022/09/17 14:54:27 by mhorton ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t size) 16 | { 17 | size_t dlen; 18 | size_t slen; 19 | 20 | if (!size) 21 | return (ft_strlen(src)); 22 | dlen = ft_strlen(dst); 23 | slen = ft_strlen(src); 24 | if (size < dlen) 25 | return (slen + size); 26 | ft_strlcpy(dst + dlen, src, size - dlen); 27 | return (dlen + slen); 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:11:40 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:11:41 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, const char *src, size_t size) 16 | { 17 | size_t slen; 18 | 19 | slen = 0; 20 | if (size) 21 | { 22 | size -= 1; 23 | while (src[slen] && (slen < size)) 24 | { 25 | dst[slen] = src[slen]; 26 | slen++; 27 | } 28 | dst[slen] = '\0'; 29 | } 30 | while (src[slen]) 31 | slen++; 32 | return (slen); 33 | } 34 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:11:46 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:11:47 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | while (*s++) 21 | len++; 22 | return (len); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:11:52 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/11 01:43:16 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | char *str; 18 | size_t slen; 19 | unsigned int i; 20 | 21 | slen = ft_strlen(s); 22 | if (slen == (size_t)-1) 23 | return (NULL); 24 | str = (char *)ft_calloc(slen + 1, sizeof(char)); 25 | if (str) 26 | { 27 | i = 0; 28 | while (s[i]) 29 | { 30 | str[i] = (*f)(i, s[i]); 31 | i++; 32 | } 33 | } 34 | return (str); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/12 01:00:02 by stanislav #+# #+# */ 9 | /* Updated: 2021/12/12 01:00:03 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | const unsigned char *str1; 18 | const unsigned char *str2; 19 | 20 | if (!n) 21 | return (0); 22 | str1 = (unsigned char *) s1; 23 | str2 = (unsigned char *) s2; 24 | while (*str1 && *str2 && --n && *str1 == *str2) 25 | { 26 | str1++; 27 | str2++; 28 | } 29 | return (*str1 - *str2); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mhorton +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:12:05 by stanislav #+# #+# */ 9 | /* Updated: 2022/09/17 15:54:09 by mhorton ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *big, const char *little, size_t n) 16 | { 17 | size_t i; 18 | size_t j; 19 | 20 | if (!n && !big) 21 | return (NULL); 22 | if (!*little) 23 | return ((char *)big); 24 | i = 0; 25 | while (big[i]) 26 | { 27 | j = 0; 28 | while (little[j] && big[i + j] == little[j] && (i + j) < n) 29 | j++; 30 | if ((i + j) > n) 31 | return (NULL); 32 | if (!little[j]) 33 | return ((char *)&big[i]); 34 | if (!big[i + j]) 35 | return (NULL); 36 | i++; 37 | } 38 | return (NULL); 39 | } 40 | -------------------------------------------------------------------------------- /libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:12:12 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:12:12 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | const char *cptr; 18 | unsigned char chr; 19 | 20 | cptr = s; 21 | chr = c; 22 | while (*s) 23 | { 24 | if (*s == c) 25 | cptr = s; 26 | s++; 27 | } 28 | if (*cptr == chr) 29 | return ((char *)cptr); 30 | if (*s == chr) 31 | return ((char *)s); 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:13:31 by stanislav #+# #+# */ 9 | /* Updated: 2022/02/05 22:34:04 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_isinset(char c, const char *set) 16 | { 17 | while (*set) 18 | { 19 | if (*set == c) 20 | return (1); 21 | set++; 22 | } 23 | return (0); 24 | } 25 | 26 | char *ft_strtrim(char const *s1, char const *set) 27 | { 28 | char *str; 29 | const char *start; 30 | const char *end; 31 | size_t span; 32 | 33 | while (ft_isinset(*s1, set)) 34 | s1++; 35 | start = s1; 36 | while (*s1) 37 | s1++; 38 | s1--; 39 | while (ft_isinset(*s1, set) && start < s1) 40 | s1--; 41 | end = s1; 42 | span = end - start + 1; 43 | str = ft_calloc(span + 1, sizeof(char)); 44 | if (str) 45 | ft_memmove(str, start, span); 46 | return (str); 47 | } 48 | // ft_strlcpy(str, start, span + 1); -> ft_memmove(str, start, span); -------------------------------------------------------------------------------- /libft/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:13:38 by stanislav #+# #+# */ 9 | /* Updated: 2022/02/05 22:35:25 by stanislav ### ########.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 *sub; 18 | size_t slen; 19 | size_t max; 20 | 21 | max = -1; 22 | slen = ft_strlen(s); 23 | if (start < slen) 24 | { 25 | if (max - (size_t)start < len) 26 | return (NULL); 27 | if (slen > (start + len)) 28 | slen = start + len; 29 | slen -= start; 30 | sub = ft_calloc(slen + 1, sizeof(char)); 31 | if (sub) 32 | ft_memmove(sub, s + start, slen); 33 | } 34 | else 35 | sub = ft_calloc(1, sizeof(char)); 36 | return (sub); 37 | } 38 | // ft_strlcpy(sub, s + start, slen + 1); -> ft_memmove(sub, s + start, slen); -------------------------------------------------------------------------------- /libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:13:45 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:13:45 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | static int ft_isupper(int c) 14 | { 15 | return (c >= 'A' && c <= 'Z'); 16 | } 17 | 18 | int ft_tolower(int c) 19 | { 20 | if (ft_isupper(c)) 21 | return (c + 32); 22 | return (c); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:13:52 by stanislav #+# #+# */ 9 | /* Updated: 2021/11/03 22:13:53 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | static int ft_islower(int c) 14 | { 15 | return (c >= 'a' && c <= 'z'); 16 | } 17 | 18 | int ft_toupper(int c) 19 | { 20 | if (ft_islower(c)) 21 | return (c - 32); 22 | return (c); 23 | } 24 | -------------------------------------------------------------------------------- /libft/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: stanislav +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/03 22:29:20 by stanislav #+# #+# */ 9 | /* Updated: 2022/04/04 17:42:43 by stanislav ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | // size_t and malloc 17 | # include 18 | 19 | int ft_atoi(const char *nptr); 20 | 21 | void ft_bzero(void *s, size_t n); 22 | 23 | int ft_isalnum(int c); 24 | int ft_isalpha(int c); 25 | int ft_isascii(int c); 26 | int ft_isdigit(int c); 27 | int ft_isprint(int c); 28 | 29 | void *ft_memchr(const void *s, int c, size_t n); 30 | int ft_memcmp(const void *s1, const void *s2, size_t n); 31 | void *ft_memcpy(void *dst, const void *src, size_t n); 32 | void *ft_memmove(void *dst, const void *src, size_t n); 33 | void *ft_memset(void *s, int c, size_t n); 34 | 35 | char *ft_strchr(const char *s, int c); 36 | size_t ft_strlcat(char *dst, const char *src, size_t size); 37 | size_t ft_strlcpy(char *dst, const char *src, size_t size); 38 | size_t ft_strlen(const char *s); 39 | int ft_strncmp(const char *s1, const char *s2, size_t n); 40 | char *ft_strnstr(const char *big, const char *little, size_t n); 41 | char *ft_strrchr(const char *s, int c); 42 | 43 | int ft_tolower(int c); 44 | int ft_toupper(int c); 45 | 46 | void *ft_calloc(size_t nmemb, size_t size); 47 | char *ft_strdup(const char *s); 48 | 49 | // the additional section 50 | 51 | char *ft_itoa(int n); 52 | 53 | void ft_putchar_fd(char c, int fd); 54 | void ft_putendl_fd(char *s, int fd); 55 | void ft_putnbr_fd(int nbr, int fd); 56 | void ft_putstr_fd(char *s, int fd); 57 | 58 | void *ft_memccpy(void *dst, const void *src, int c, size_t n); 59 | 60 | char **ft_split(char const *s, char c); 61 | char *ft_strjoin(char const *s1, char const *s2); 62 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 63 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 64 | char *ft_strtrim(char const *s1, char const *set); 65 | char *ft_substr(char const *s, unsigned int start, size_t len); 66 | 67 | // end of the additional section 68 | 69 | // the bonus section 70 | 71 | typedef struct s_list 72 | { 73 | void *content; 74 | struct s_list *next; 75 | } t_list; 76 | 77 | void ft_lstadd_back(t_list **lst, t_list *new); 78 | void ft_lstadd_front(t_list **lst, t_list *new); 79 | void ft_lstclear(t_list **lst, void (*del)(void *)); 80 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 81 | void ft_lstiter(t_list *lst, void (*f)(void *)); 82 | t_list *ft_lstlast(t_list *lst); 83 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); 84 | t_list *ft_lstnew(void *content); 85 | int ft_lstsize(t_list *lst); 86 | 87 | // end of the bonus section 88 | 89 | #endif --------------------------------------------------------------------------------