diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..06d79bb --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: yochered +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2018/10/26 13:59:12 by yochered #+# #+# # +# Updated: 2018/11/30 15:03:04 by yochered ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a +SRC = ft_memset.c ft_bzero.c ft_memcpy.c ft_memccpy.c ft_memmove.c ft_memchr.c\ + ft_memcmp.c ft_strlen.c ft_strdup.c ft_strcpy.c ft_strncpy.c ft_strcat.c\ + ft_strncat.c ft_strlcat.c ft_strchr.c ft_strrchr.c ft_strstr.c ft_strnstr.c\ + ft_strcmp.c ft_strncmp.c ft_atoi.c ft_isalpha.c ft_isdigit.c ft_isalnum.c\ + ft_isascii.c ft_isprint.c ft_toupper.c ft_tolower.c ft_memalloc.c\ + ft_memdel.c ft_strnew.c ft_strdel.c ft_strclr.c ft_striter.c ft_striteri.c\ + ft_strmap.c ft_strmapi.c ft_strequ.c ft_strnequ.c ft_strsub.c ft_strjoin.c\ + ft_strtrim.c ft_strsplit.c ft_itoa.c ft_putchar.c ft_putstr.c ft_putendl.c\ + ft_putnbr.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c\ + ft_lstnew.c ft_lstdelone.c ft_lstdel.c ft_lstadd.c ft_lstiter.c ft_lstmap.c\ + ft_itoa_base.c ft_lstrev.c ft_cycle_detector.c ft_print_memory.c +BINARY = $(SRC:.c=.o) + +all: $(NAME) + +$(NAME): $(BINARY) + ar rc $(NAME) $(BINARY) + +%.o: %.c + gcc -Wall -W -Werror -c -o $@ $< + +clean: + /bin/rm -f $(BINARY) + +fclean: clean + /bin/rm -f $(NAME) + +re: fclean all diff --git a/author b/author new file mode 100644 index 0000000..3e13c33 --- /dev/null +++ b/author @@ -0,0 +1 @@ +yochered diff --git a/ft_atoi.c b/ft_atoi.c new file mode 100644 index 0000000..c859cb9 --- /dev/null +++ b/ft_atoi.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:42:06 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:42:07 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isspace(int c) +{ + unsigned char s; + + s = (unsigned char)c; + if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' + || c == '\r') + return (1); + return (0); +} + +int ft_atoi(const char *str) +{ + int sign; + long res; + + sign = 1; + res = 0; + while (ft_isspace(*str) == 1) + str++; + if (*str == '+' || *str == '-') + { + if (*str == '-') + sign = -1; + str++; + } + while (ft_isdigit(*str) == 1) + { + if (9223372036854775807 - res < *str - 48) + return (sign == -1 ? 0 : -1); + res = res * 10 + (*str - 48); + str++; + } + return (res * sign); +} diff --git a/ft_bzero.c b/ft_bzero.c new file mode 100644 index 0000000..ddff3f3 --- /dev/null +++ b/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 12:30:01 by yochered #+# #+# */ +/* Updated: 2018/10/25 12:30:04 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/ft_cycle_detector.c b/ft_cycle_detector.c new file mode 100644 index 0000000..8ea318f --- /dev/null +++ b/ft_cycle_detector.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cycle_detector.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 17:47:52 by yochered #+# #+# */ +/* Updated: 2018/10/29 17:47:53 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_cycle_detector(t_list *list) +{ + t_list *hare; + t_list *tortoise; + + if (!list) + return (1); + tortoise = list; + hare = list; + while (1) + { + tortoise = tortoise->next; + if (hare->next != NULL) + hare = hare->next->next; + else + return (0); + if (!hare || !tortoise) + return (0); + if (hare == tortoise) + return (1); + } +} diff --git a/ft_isalnum.c b/ft_isalnum.c new file mode 100644 index 0000000..2bf268a --- /dev/null +++ b/ft_isalnum.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:02:27 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:02:28 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + if (ft_isalpha(c) == 1 || ft_isdigit(c) == 1) + return (1); + return (0); +} diff --git a/ft_isalpha.c b/ft_isalpha.c new file mode 100644 index 0000000..7165ae8 --- /dev/null +++ b/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:47:15 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:47:16 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalpha(int c) +{ + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return (1); + return (0); +} diff --git a/ft_isascii.c b/ft_isascii.c new file mode 100644 index 0000000..568ccd4 --- /dev/null +++ b/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:13:52 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:13:54 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/ft_isdigit.c b/ft_isdigit.c new file mode 100644 index 0000000..263e71a --- /dev/null +++ b/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:59:47 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:59:48 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/ft_isprint.c b/ft_isprint.c new file mode 100644 index 0000000..4e1b1a6 --- /dev/null +++ b/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:18:11 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:18:12 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/ft_itoa.c b/ft_itoa.c new file mode 100644 index 0000000..990861b --- /dev/null +++ b/ft_itoa.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 11:31:35 by yochered #+# #+# */ +/* Updated: 2018/10/24 11:32:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static int count_digits(int nbr) +{ + int res; + + res = 0; + if (nbr <= 0) + res++; + while (nbr != 0) + { + nbr /= 10; + res++; + } + return (res); +} + +char *ft_itoa(int n) +{ + char *res; + int len; + int start; + + len = count_digits(n); + start = 0; + res = (char *)malloc((len + 1) * sizeof(char)); + if (!res) + return (NULL); + if (n < 0) + { + start++; + res[0] = '-'; + } + res[len--] = '\0'; + while (len >= start) + { + if (n > 0) + res[len--] = n % 10 + 48; + else + res[len--] = -(n % 10) + 48; + n /= 10; + } + return (res); +} diff --git a/ft_itoa_base.c b/ft_itoa_base.c new file mode 100644 index 0000000..fef9801 --- /dev/null +++ b/ft_itoa_base.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 16:17:41 by yochered #+# #+# */ +/* Updated: 2018/10/29 16:17:42 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static int count_digits(int value, int base) +{ + int digits; + + digits = 0; + if (value < 0 && base == 10) + digits++; + if (value == 0) + digits++; + while (value != 0) + { + digits++; + value /= base; + } + return (digits); +} + +char *ft_itoa_base(int value, int base) +{ + char *res; + char *base_digits; + int digits; + + if (!(base_digits = (char *)malloc(16 * sizeof(char)))) + return (NULL); + base_digits = "0123456789ABCDEF"; + digits = count_digits(value, base); + if (!(res = (char *)malloc((digits + 1) * sizeof(char)))) + return (NULL); + if (value < 0 && base == 10) + res[0] = '-'; + if (value == 0) + res[0] = '0'; + res[digits] = '\0'; + while (value && digits-- > 0) + { + res[digits] = value < 0 ? base_digits[-(value % base)] : + base_digits[(value % base)]; + value /= base; + } + return (res); +} diff --git a/ft_lstadd.c b/ft_lstadd.c new file mode 100644 index 0000000..0877a5d --- /dev/null +++ b/ft_lstadd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 10:52:34 by yochered #+# #+# */ +/* Updated: 2018/10/29 10:52:35 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd(t_list **alst, t_list *new) +{ + if (!alst || !new) + return ; + new->next = *alst; + *alst = new; +} diff --git a/ft_lstdel.c b/ft_lstdel.c new file mode 100644 index 0000000..c09731a --- /dev/null +++ b/ft_lstdel.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 11:04:10 by yochered #+# #+# */ +/* Updated: 2018/10/29 11:04:12 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) +{ + t_list *node; + t_list *next_node; + + if (!alst || !del) + return ; + node = *alst; + while (node) + { + next_node = node->next; + ft_lstdelone(&node, del); + *alst = (*alst)->next; + node = next_node; + } + *alst = NULL; +} diff --git a/ft_lstdelone.c b/ft_lstdelone.c new file mode 100644 index 0000000..484e473 --- /dev/null +++ b/ft_lstdelone.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 10:34:51 by yochered #+# #+# */ +/* Updated: 2018/10/29 10:34:52 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) +{ + t_list *node; + + if (!alst || !del) + return ; + node = *alst; + del(node->content, node->content_size); + free(node); + *alst = NULL; +} diff --git a/ft_lstiter.c b/ft_lstiter.c new file mode 100644 index 0000000..c0e6f54 --- /dev/null +++ b/ft_lstiter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 11:14:33 by yochered #+# #+# */ +/* Updated: 2018/10/29 11:14:44 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + if (!lst) + return ; + while (lst) + { + f(lst); + lst = lst->next; + } +} diff --git a/ft_lstmap.c b/ft_lstmap.c new file mode 100644 index 0000000..e33c31b --- /dev/null +++ b/ft_lstmap.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 11:18:59 by yochered #+# #+# */ +/* Updated: 2018/10/29 11:19:00 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *new_list; + + if (lst) + { + new_list = f(lst); + new_list->next = ft_lstmap(lst->next, f); + return (new_list); + } + return (NULL); +} diff --git a/ft_lstnew.c b/ft_lstnew.c new file mode 100644 index 0000000..f0c45d5 --- /dev/null +++ b/ft_lstnew.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 09:57:18 by yochered #+# #+# */ +/* Updated: 2018/10/29 09:57:20 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *node; + void *data; + size_t size; + + if (!(node = (t_list *)malloc(sizeof(t_list)))) + return (NULL); + data = (void *)content; + size = content_size; + if (!content) + { + node->content = NULL; + node->content_size = 0; + } + else + { + if (!(node->content = malloc(ft_strlen((char *)content)))) + return (NULL); + node->content = ft_memcpy(node->content, data, size); + node->content_size = size; + } + node->next = NULL; + return (node); +} diff --git a/ft_lstrev.c b/ft_lstrev.c new file mode 100644 index 0000000..269d4ae --- /dev/null +++ b/ft_lstrev.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstrev.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/29 17:13:30 by yochered #+# #+# */ +/* Updated: 2018/10/29 17:13:31 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstrev(t_list **begin_list) +{ + t_list *prev; + t_list *cur; + t_list *next; + + if (!begin_list) + return ; + prev = NULL; + cur = *begin_list; + while (cur) + { + next = cur->next; + cur->next = prev; + prev = cur; + cur = next; + } + *begin_list = prev; +} diff --git a/ft_memalloc.c b/ft_memalloc.c new file mode 100644 index 0000000..ddac108 --- /dev/null +++ b/ft_memalloc.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 15:16:03 by yochered #+# #+# */ +/* Updated: 2018/10/25 11:16:22 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +void *ft_memalloc(size_t size) +{ + void *mem_area; + + if (!(mem_area = (unsigned char *)malloc(size * sizeof(unsigned char)))) + return (NULL); + mem_area = (unsigned char *)ft_memset(mem_area, 0, size); + return (mem_area); +} diff --git a/ft_memccpy.c b/ft_memccpy.c new file mode 100644 index 0000000..9c39681 --- /dev/null +++ b/ft_memccpy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 16:03:29 by yochered #+# #+# */ +/* Updated: 2018/10/25 16:03:30 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memccpy(void *dst, const void *src, int c, size_t n) +{ + size_t i; + unsigned char *dst_tmp; + unsigned char *src_tmp; + + i = 0; + dst_tmp = (unsigned char *)dst; + src_tmp = (unsigned char *)src; + while (i < n) + { + dst_tmp[i] = src_tmp[i]; + if (src_tmp[i] == (unsigned char)c) + return (dst + i + 1); + i++; + } + return (NULL); +} diff --git a/ft_memchr.c b/ft_memchr.c new file mode 100644 index 0000000..7742025 --- /dev/null +++ b/ft_memchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 15:30:22 by yochered #+# #+# */ +/* Updated: 2018/10/25 15:30:23 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memchr(const void *s, int c, size_t n) +{ + unsigned char *str; + + str = (unsigned char *)s; + while (n--) + { + if (*str == (unsigned char)c) + return (str); + str++; + } + return (NULL); +} diff --git a/ft_memcmp.c b/ft_memcmp.c new file mode 100644 index 0000000..b6bf7be --- /dev/null +++ b/ft_memcmp.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 15:40:33 by yochered #+# #+# */ +/* Updated: 2018/10/25 15:40:34 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *s1_tmp; + unsigned char *s2_tmp; + + s1_tmp = (unsigned char *)s1; + s2_tmp = (unsigned char *)s2; + while (n--) + { + if (*s1_tmp != *s2_tmp) + return (*s1_tmp - *s2_tmp); + s1_tmp++; + s2_tmp++; + } + return (0); +} diff --git a/ft_memcpy.c b/ft_memcpy.c new file mode 100644 index 0000000..f6ee42c --- /dev/null +++ b/ft_memcpy.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 13:45:23 by yochered #+# #+# */ +/* Updated: 2018/10/25 13:45:24 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + unsigned char *dst_tmp; + unsigned char *src_tmp; + + dst_tmp = (unsigned char *)dst; + src_tmp = (unsigned char *)src; + while (n--) + *dst_tmp++ = *src_tmp++; + return (dst); +} diff --git a/ft_memdel.c b/ft_memdel.c new file mode 100644 index 0000000..55ec587 --- /dev/null +++ b/ft_memdel.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 17:40:50 by yochered #+# #+# */ +/* Updated: 2018/10/24 17:40:51 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void ft_memdel(void **ap) +{ + if (ap) + { + free(*ap); + *ap = NULL; + } +} diff --git a/ft_memmove.c b/ft_memmove.c new file mode 100644 index 0000000..25b451a --- /dev/null +++ b/ft_memmove.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 13:30:49 by yochered #+# #+# */ +/* Updated: 2018/10/25 13:30:52 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + unsigned char *dst_tmp; + unsigned char *src_tmp; + size_t i; + + i = -1; + dst_tmp = (unsigned char *)dst; + src_tmp = (unsigned char *)src; + if (src_tmp < dst_tmp) + { + while (len--) + *(dst_tmp + len) = *(src_tmp + len); + } + else + { + while (++i < len) + *(dst_tmp + i) = *(src_tmp + i); + } + return (dst); +} diff --git a/ft_memset.c b/ft_memset.c new file mode 100644 index 0000000..5c0bedb --- /dev/null +++ b/ft_memset.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 16:16:00 by yochered #+# #+# */ +/* Updated: 2018/10/24 16:16:01 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memset(void *b, int c, size_t len) +{ + unsigned char *ptr; + + ptr = (unsigned char *)b; + if (len) + { + while (len--) + *ptr++ = (unsigned char)c; + } + return (b); +} diff --git a/ft_print_memory.c b/ft_print_memory.c new file mode 100644 index 0000000..af95d03 --- /dev/null +++ b/ft_print_memory.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_memory.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/30 14:30:22 by yochered #+# #+# */ +/* Updated: 2018/10/30 14:30:23 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void print_hex(unsigned char c) +{ + char hex_digits[16]; + int i; + char n; + + i = 0; + n = '0'; + while (n <= '9') + hex_digits[i++] = n++; + n = 'a'; + while (n <= 'f') + hex_digits[i++] = n++; + hex_digits[i] = '\0'; + ft_putchar(hex_digits[c / 16]); + ft_putchar(hex_digits[c % 16]); +} + +static void print_ascii(unsigned char c) +{ + if (c >= 32 && c <= 126) + ft_putchar(c); + else + ft_putchar('.'); +} + +static void print_line(unsigned char *str, size_t min, size_t max) +{ + size_t i; + + i = min; + while (i < min + 16 && i < max) + { + print_hex(str[i]); + if (i % 2 != 0) + ft_putchar(' '); + i++; + } + while (i < min + 16) + { + ft_putchar(' '); + ft_putchar(' '); + if (i % 2 != 0) + ft_putchar(' '); + i++; + } + i = min; + while (i < min + 16 && i < max) + { + print_ascii(str[i]); + i++; + } + ft_putchar('\n'); +} + +void ft_print_memory(const void *addr, size_t size) +{ + size_t c; + unsigned char *str; + + c = 0; + str = (unsigned char *)addr; + while (c < size) + { + print_line(str, c, size); + c += 16; + } +} diff --git a/ft_putchar.c b/ft_putchar.c new file mode 100644 index 0000000..b0bb263 --- /dev/null +++ b/ft_putchar.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 12:28:23 by yochered #+# #+# */ +/* Updated: 2018/10/24 12:28:26 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_putchar(char c) +{ + ft_putchar_fd(c, 1); +} diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c new file mode 100644 index 0000000..e4d274c --- /dev/null +++ b/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 12:19:57 by yochered #+# #+# */ +/* Updated: 2018/10/24 12:19:59 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar_fd(char c, int fd) +{ + if (fd < 0) + return ; + write(fd, &c, 1); +} diff --git a/ft_putendl.c b/ft_putendl.c new file mode 100644 index 0000000..029460b --- /dev/null +++ b/ft_putendl.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 18:19:58 by yochered #+# #+# */ +/* Updated: 2018/10/24 18:20:00 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl(char const *s) +{ + ft_putendl_fd(s, 1); +} diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c new file mode 100644 index 0000000..9bd0b42 --- /dev/null +++ b/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 16:43:22 by yochered #+# #+# */ +/* Updated: 2018/10/27 16:43:23 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char const *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/ft_putnbr.c b/ft_putnbr.c new file mode 100644 index 0000000..05668b6 --- /dev/null +++ b/ft_putnbr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 10:38:49 by yochered #+# #+# */ +/* Updated: 2018/10/24 10:47:55 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr(int n) +{ + ft_putnbr_fd(n, 1); +} diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c new file mode 100644 index 0000000..09e9dd1 --- /dev/null +++ b/ft_putnbr_fd.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 16:40:54 by yochered #+# #+# */ +/* Updated: 2018/10/27 16:40:55 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n < 0) + { + ft_putchar_fd('-', fd); + if (n <= -10) + ft_putnbr_fd(n / -10, fd); + ft_putchar_fd(-(n % 10) + 48, fd); + } + else if (n >= 10) + { + ft_putnbr_fd(n / 10, fd); + ft_putchar_fd(n % 10 + 48, fd); + } + else + ft_putchar_fd(n % 10 + 48, fd); +} diff --git a/ft_putstr.c b/ft_putstr.c new file mode 100644 index 0000000..8edb47e --- /dev/null +++ b/ft_putstr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 10:27:02 by yochered #+# #+# */ +/* Updated: 2018/10/24 10:27:11 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + ft_putstr_fd(s, 1); +} diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c new file mode 100644 index 0000000..ab544cd --- /dev/null +++ b/ft_putstr_fd.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 12:28:37 by yochered #+# #+# */ +/* Updated: 2018/10/24 12:28:38 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char const *s, int fd) +{ + if (!s) + return ; + while (*s) + { + ft_putchar_fd(*s, fd); + s++; + } +} diff --git a/ft_quick_sort.c b/ft_quick_sort.c new file mode 100644 index 0000000..0ca4e10 --- /dev/null +++ b/ft_quick_sort.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_quick_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/30 15:01:25 by yochered #+# #+# */ +/* Updated: 2018/10/30 15:01:26 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +static void ft_swap(int *a, int *b) +{ + int tmp; + + tmp = *a; + *a = *b; + *b = tmp; +} + +static int partition(int *tab, int low, int high) +{ + int pivot; + int i; + int j; + + pivot = tab[high]; + j = low; + i = low - 1; + while (j <= high - 1) + { + if (tab[j] <= pivot) + { + i++; + ft_swap(&tab[i], &tab[j]); + } + j++; + } + ft_swap(&tab[i + 1], &tab[high]); + return (i + 1); +} + +void ft_quick_sort(int **tab, int low, int high) +{ + int pivot; + + if (low < high) + { + pivot = partition(*tab, low, high); + ft_quick_sort(tab, low, pivot - 1); + ft_quick_sort(tab, pivot + 1, high); + } +} diff --git a/ft_strcat.c b/ft_strcat.c new file mode 100644 index 0000000..916a565 --- /dev/null +++ b/ft_strcat.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 16:43:12 by yochered #+# #+# */ +/* Updated: 2018/10/25 16:43:13 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *s1, const char *s2) +{ + char *s1_tmp; + + s1_tmp = s1; + s1_tmp += ft_strlen(s1); + while (*s2) + *s1_tmp++ = *s2++; + *s1_tmp = '\0'; + return (s1); +} diff --git a/ft_strchr.c b/ft_strchr.c new file mode 100644 index 0000000..1e29083 --- /dev/null +++ b/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 17:50:17 by yochered #+# #+# */ +/* Updated: 2018/10/25 17:50:18 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + char *str; + + str = (char *)s; + if ((char)c == '\0') + return (str + ft_strlen(str)); + while (*str) + { + if (*str == (char)c) + return (str); + str++; + } + return (NULL); +} diff --git a/ft_strclr.c b/ft_strclr.c new file mode 100644 index 0000000..4a1ff1d --- /dev/null +++ b/ft_strclr.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 12:57:03 by yochered #+# #+# */ +/* Updated: 2018/10/27 12:57:04 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_strclr(char *s) +{ + if (!s) + return ; + while (*s) + *s++ = '\0'; +} diff --git a/ft_strcmp.c b/ft_strcmp.c new file mode 100644 index 0000000..6e66940 --- /dev/null +++ b/ft_strcmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:32:04 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:32:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcmp(const char *s1, const char *s2) +{ + int i; + + i = 0; + while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/ft_strcpy.c b/ft_strcpy.c new file mode 100644 index 0000000..e181f80 --- /dev/null +++ b/ft_strcpy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 15:42:03 by yochered #+# #+# */ +/* Updated: 2018/10/24 15:42:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dst, const char *src) +{ + char *dst_tmp; + + dst_tmp = dst; + while (*src) + *dst_tmp++ = *src++; + *dst_tmp = '\0'; + return (dst); +} diff --git a/ft_strdel.c b/ft_strdel.c new file mode 100644 index 0000000..ad253f0 --- /dev/null +++ b/ft_strdel.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:07:06 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:07:07 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void ft_strdel(char **as) +{ + if (as) + { + free(*as); + *as = NULL; + } +} diff --git a/ft_strdup.c b/ft_strdup.c new file mode 100644 index 0000000..9433c14 --- /dev/null +++ b/ft_strdup.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 15:32:48 by yochered #+# #+# */ +/* Updated: 2018/10/24 15:32:50 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *dest; + char *dest_tmp; + int len; + + len = ft_strlen(s1); + dest = (char *)malloc((len + 1) * sizeof(char)); + if (!dest) + return (NULL); + dest_tmp = dest; + while (*s1) + *dest++ = *s1++; + *dest = '\0'; + return (dest_tmp); +} diff --git a/ft_strequ.c b/ft_strequ.c new file mode 100644 index 0000000..c6f5d18 --- /dev/null +++ b/ft_strequ.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 14:58:04 by yochered #+# #+# */ +/* Updated: 2018/10/24 14:58:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strequ(char const *s1, char const *s2) +{ + int i; + + i = 0; + if (!s1 || !s2) + return (0); + while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') + i++; + if (s1[i] != '\0' || s2[i] != '\0') + return (0); + return (1); +} diff --git a/ft_striter.c b/ft_striter.c new file mode 100644 index 0000000..818585f --- /dev/null +++ b/ft_striter.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:04:52 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:04:52 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_striter(char *s, void (*f)(char *)) +{ + if (!s || !f) + return ; + while (*s) + { + f(&*s); + s++; + } +} diff --git a/ft_striteri.c b/ft_striteri.c new file mode 100644 index 0000000..68cec02 --- /dev/null +++ b/ft_striteri.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:11:47 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:11:48 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + int i; + + i = 0; + if (!s || !f) + return ; + while (s[i] != '\0') + { + f(i, &s[i]); + i++; + } +} diff --git a/ft_strjoin.c b/ft_strjoin.c new file mode 100644 index 0000000..5a86c5c --- /dev/null +++ b/ft_strjoin.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:44:36 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:44:37 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *res; + char *res_tmp; + + if (!s1 || !s2) + return (NULL); + res = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char)); + if (!res) + return (NULL); + res_tmp = res; + while (*s1) + *res_tmp++ = *s1++; + while (*s2) + *res_tmp++ = *s2++; + *res_tmp = '\0'; + return (res); +} diff --git a/ft_strlcat.c b/ft_strlcat.c new file mode 100644 index 0000000..782ecb9 --- /dev/null +++ b/ft_strlcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 17:17:28 by yochered #+# #+# */ +/* Updated: 2018/10/25 17:17:29 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t dstsize) +{ + size_t i; + size_t dstlength; + size_t srclength; + + i = 0; + dstlength = ft_strlen(dst); + srclength = ft_strlen(src); + if (dstsize <= dstlength) + return (srclength + dstsize); + while (dst[i] != '\0' && i < dstsize - 1) + i++; + while (*src && i < dstsize - 1) + dst[i++] = *src++; + dst[i] = '\0'; + return (dstlength + srclength); +} diff --git a/ft_strlen.c b/ft_strlen.c new file mode 100644 index 0000000..0dce382 --- /dev/null +++ b/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 11:34:29 by yochered #+# #+# */ +/* Updated: 2018/10/24 11:34:30 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_strlen(const char *string) +{ + size_t len; + + len = 0; + while (string[len] != '\0') + len++; + return (len); +} diff --git a/ft_strmap.c b/ft_strmap.c new file mode 100644 index 0000000..43e894e --- /dev/null +++ b/ft_strmap.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:13:12 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:13:13 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strmap(char const *s, char (*f)(char)) +{ + char *fresh_str; + char *fresh_str_tmp; + char *s_tmp; + + if (!s) + return (NULL); + s_tmp = (char *)s; + fresh_str = (char *)malloc((ft_strlen(s_tmp) + 1) * sizeof(char)); + if (!fresh_str) + return (NULL); + fresh_str_tmp = fresh_str; + while (*s_tmp) + { + *fresh_str++ = f(*s_tmp); + s_tmp++; + } + *fresh_str = '\0'; + return (fresh_str_tmp); +} diff --git a/ft_strmapi.c b/ft_strmapi.c new file mode 100644 index 0000000..9ba010c --- /dev/null +++ b/ft_strmapi.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:15:20 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:15:22 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *fresh_str; + char *s_tmp; + int i; + + i = 0; + if (!s) + return (NULL); + s_tmp = (char *)s; + fresh_str = (char *)malloc((ft_strlen(s_tmp) + 1) * sizeof(char)); + if (!fresh_str) + return (NULL); + while (s_tmp[i] != '\0') + { + fresh_str[i] = f(i, s_tmp[i]); + i++; + } + fresh_str[i] = '\0'; + return (fresh_str); +} diff --git a/ft_strncat.c b/ft_strncat.c new file mode 100644 index 0000000..11d18d3 --- /dev/null +++ b/ft_strncat.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 17:10:34 by yochered #+# #+# */ +/* Updated: 2018/10/25 17:10:36 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncat(char *s1, const char *s2, size_t n) +{ + char *s1_tmp; + + s1_tmp = s1; + s1_tmp += ft_strlen(s1); + while (*s2 && n--) + *s1_tmp++ = *s2++; + *s1_tmp = '\0'; + return (s1); +} diff --git a/ft_strncmp.c b/ft_strncmp.c new file mode 100644 index 0000000..6dba04e --- /dev/null +++ b/ft_strncmp.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:38:08 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:38:09 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + if (n == 0) + return (0); + while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0' && i < n - 1) + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/ft_strncpy.c b/ft_strncpy.c new file mode 100644 index 0000000..8d76398 --- /dev/null +++ b/ft_strncpy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/25 14:23:04 by yochered #+# #+# */ +/* Updated: 2018/10/25 14:23:05 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncpy(char *dst, const char *src, size_t len) +{ + size_t src_len; + size_t cpy_len; + + src_len = 0; + cpy_len = len; + while (src[src_len] != '\0' && len--) + src_len++; + if (src_len < cpy_len) + { + ft_memcpy(dst, src, src_len); + ft_memset(dst + src_len, 0, cpy_len - src_len); + } + else + ft_memcpy(dst, src, cpy_len); + return (dst); +} diff --git a/ft_strnequ.c b/ft_strnequ.c new file mode 100644 index 0000000..e4e6479 --- /dev/null +++ b/ft_strnequ.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 15:08:08 by yochered #+# #+# */ +/* Updated: 2018/10/30 18:13:56 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + size_t i; + + i = 0; + if (!s1 || !s2 || !n) + return (1); + while (n > 0) + { + if (!s1[i] && !s2[i]) + return (1); + if (s1[i] != s2[i]) + return (0); + n--; + i++; + } + return (1); +} diff --git a/ft_strnew.c b/ft_strnew.c new file mode 100644 index 0000000..50af776 --- /dev/null +++ b/ft_strnew.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 18:03:37 by yochered #+# #+# */ +/* Updated: 2018/10/24 18:03:38 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strnew(size_t size) +{ + char *res; + size_t i; + + res = (char *)malloc((size + 1) * sizeof(char)); + i = 0; + if (!res) + return (NULL); + while (i < size) + res[i++] = '\0'; + res[i] = '\0'; + return (res); +} diff --git a/ft_strnstr.c b/ft_strnstr.c new file mode 100644 index 0000000..ece06df --- /dev/null +++ b/ft_strnstr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 11:10:30 by yochered #+# #+# */ +/* Updated: 2018/10/26 11:10:32 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + char *str; + size_t substr_len; + + substr_len = ft_strlen(needle); + str = (char *)haystack; + if (substr_len == 0) + return (str); + while (*str && len-- >= substr_len) + { + if (ft_memcmp(str, needle, substr_len) == 0) + return (str); + str++; + } + return (NULL); +} diff --git a/ft_strrchr.c b/ft_strrchr.c new file mode 100644 index 0000000..0cc61c3 --- /dev/null +++ b/ft_strrchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 10:53:14 by yochered #+# #+# */ +/* Updated: 2018/10/27 10:53:25 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + char *str; + char *last_s; + + last_s = NULL; + str = (char *)s; + if ((char)c == '\0') + return (str + ft_strlen(str)); + while (*str) + { + if (*str == (char)c) + last_s = str; + str++; + } + return (last_s); +} diff --git a/ft_strsplit.c b/ft_strsplit.c new file mode 100644 index 0000000..1627211 --- /dev/null +++ b/ft_strsplit.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 15:14:46 by yochered #+# #+# */ +/* Updated: 2018/10/27 15:14:51 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static int count_words(char const *s, char c) +{ + int words; + int len; + int i; + + words = 0; + i = -1; + len = ft_strlen(s); + while (++i < len) + { + if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0')) + words++; + } + return (words); +} + +static int count_len(char const *s, char c) +{ + int len; + + len = 0; + while (*s == c) + s++; + while (s[len] != c && s[len] != '\0') + len++; + return (len); +} + +char **ft_strsplit(char const *s, char c) +{ + int i; + int j; + int k; + char **res; + + if (!s || !(res = (char **)malloc(sizeof(*res) * (count_words(s, c) + 1)))) + return (NULL); + i = -1; + j = 0; + while (++i < count_words(s, c)) + { + k = 0; + if (!(res[i] = ft_strnew(count_len(&s[j], c)))) + res[i] = NULL; + while (s[j] == c) + j++; + while (s[j] != c && s[j]) + res[i][k++] = s[j++]; + } + res[i] = 0; + return (res); +} diff --git a/ft_strstr.c b/ft_strstr.c new file mode 100644 index 0000000..ed21d04 --- /dev/null +++ b/ft_strstr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 10:24:59 by yochered #+# #+# */ +/* Updated: 2018/10/26 10:25:01 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strstr(const char *haystack, const char *needle) +{ + char *str; + int substr_len; + + str = (char *)haystack; + substr_len = ft_strlen(needle); + if (substr_len == 0) + return (str); + while (*str) + { + if (ft_memcmp(str, needle, substr_len) == 0) + return (str); + str++; + } + return (NULL); +} diff --git a/ft_strsub.c b/ft_strsub.c new file mode 100644 index 0000000..de957ab --- /dev/null +++ b/ft_strsub.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:27:50 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:27:52 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *fresh_str; + char *str; + size_t i; + + i = 0; + fresh_str = (char *)malloc((len + 1) * sizeof(char)); + if (!fresh_str || !s) + return (NULL); + str = (char *)s; + while (str[start] != '\0' && i < len) + fresh_str[i++] = str[start++]; + fresh_str[i] = '\0'; + return (fresh_str); +} diff --git a/ft_strtrim.c b/ft_strtrim.c new file mode 100644 index 0000000..7db75ce --- /dev/null +++ b/ft_strtrim.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/27 13:52:56 by yochered #+# #+# */ +/* Updated: 2018/10/27 13:52:57 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +static int is_space(char c) +{ + if (c == ' ' || c == ',' || c == '\n' || c == '\t') + return (1); + return (0); +} + +static int count_len(char const *s) +{ + int len; + int i; + + len = ft_strlen(s); + while (is_space(s[len - 1])) + len--; + i = -1; + while (is_space(s[++i])) + len--; + if (len < 0) + len = 0; + return (len); +} + +char *ft_strtrim(char const *s) +{ + int i; + int len; + char *res; + + if (!s) + return (NULL); + i = -1; + len = count_len(s); + res = (char *)malloc((len + 1) * sizeof(char)); + if (!res) + return (NULL); + while (is_space(*s)) + s++; + while (++i < len) + res[i] = *s++; + res[i] = '\0'; + return (res); +} diff --git a/ft_tolower.c b/ft_tolower.c new file mode 100644 index 0000000..1d15d6a --- /dev/null +++ b/ft_tolower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:33:32 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:33:34 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_tolower(int c) +{ + if (c >= 65 && c <= 90) + c += 32; + return (c); +} diff --git a/ft_toupper.c b/ft_toupper.c new file mode 100644 index 0000000..5a14c43 --- /dev/null +++ b/ft_toupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/26 12:33:22 by yochered #+# #+# */ +/* Updated: 2018/10/26 12:33:24 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + c -= 32; + return (c); +} diff --git a/libft.h b/libft.h new file mode 100644 index 0000000..86c95b3 --- /dev/null +++ b/libft.h @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yochered +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 10:27:20 by yochered #+# #+# */ +/* Updated: 2018/10/24 10:27:56 by yochered ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# include + +typedef struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +} t_list; + +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memccpy(void *dst, const void *src, int c, size_t n); +void *ft_memmove(void *dst, const void *src, size_t len); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +size_t ft_strlen(const char *s); +char *ft_strdup(const char *s1); +char *ft_strcpy(char *dst, const char *src); +char *ft_strncpy(char *dst, const char *src, size_t len); +char *ft_strcat(char *s1, const char *s2); +char *ft_strncat(char *s1, const char *s2, size_t n); +size_t ft_strlcat(char *dst, const char *src, size_t dstsize); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strstr(const char *haystack, const char *needle); +char *ft_strnstr(const char *haystack, const char *needle, +size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_atoi(const char *str); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +int ft_toupper(int c); +int ft_tolower(int c); +void *ft_memalloc(size_t size); +void ft_memdel(void **ap); +char *ft_strnew(size_t size); +void ft_strdel(char **as); +void ft_strclr(char *s); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +int ft_strequ(char const *s1, char const *s2); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s); +char **ft_strsplit(char const *s, char c); +char *ft_itoa(int n); +void ft_putchar(char c); +void ft_putstr(char const *s); +void ft_putendl(char const *s); +void ft_putnbr(int n); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char const *s, int fd); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr_fd(int n, int fd); +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); +void ft_lstadd(t_list **alst, t_list *new); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); +char *ft_itoa_base(int value, int base); +void ft_lstrev(t_list **begin_list); +int ft_cycle_detector(t_list *list); +void ft_print_memory(const void *addr, size_t size); +void ft_quick_sort(int **tab, int low, int high); + +#endif