diff options
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | ft_atoi.c | 34 | ||||
| -rw-r--r-- | ft_bzero.c | 3 | ||||
| -rw-r--r-- | ft_calloc.c | 3 | ||||
| -rw-r--r-- | ft_itoa.c | 49 | ||||
| -rw-r--r-- | ft_lstadd_back_bonus.c | 3 | ||||
| -rw-r--r-- | ft_lstclear_bonus.c | 5 | ||||
| -rw-r--r-- | ft_lstdelone_bonus.c | 8 | ||||
| -rw-r--r-- | ft_lstiter_bonus.c | 3 | ||||
| -rw-r--r-- | ft_lstlast_bonus.c | 3 | ||||
| -rw-r--r-- | ft_lstmap_bonus.c | 5 | ||||
| -rw-r--r-- | ft_lstnew_bonus.c | 3 | ||||
| -rw-r--r-- | ft_lstsize_bonus.c | 3 | ||||
| -rw-r--r-- | ft_memalloc.c | 13 | ||||
| -rw-r--r-- | ft_memccpy.c | 25 | ||||
| -rw-r--r-- | ft_memchr.c | 3 | ||||
| -rw-r--r-- | ft_memcpy.c | 18 | ||||
| -rw-r--r-- | ft_memdel.c | 6 | ||||
| -rw-r--r-- | ft_memmove.c | 4 | ||||
| -rw-r--r-- | ft_memset.c | 12 | ||||
| -rw-r--r-- | ft_putchar.c | 4 | ||||
| -rw-r--r-- | ft_putchar_fd.c | 2 | ||||
| -rw-r--r-- | ft_putendl.c | 6 | ||||
| -rw-r--r-- | ft_putendl_fd.c | 2 | ||||
| -rw-r--r-- | ft_putnbr.c | 15 | ||||
| -rw-r--r-- | ft_putnbr_fd.c | 4 | ||||
| -rw-r--r-- | ft_putstr.c | 13 | ||||
| -rw-r--r-- | ft_putstr_fd.c | 5 | ||||
| -rw-r--r-- | ft_split.c | 30 | ||||
| -rw-r--r-- | ft_strchr.c | 6 | ||||
| -rw-r--r-- | ft_strclr.c | 6 | ||||
| -rw-r--r-- | ft_strdel.c | 2 | ||||
| -rw-r--r-- | ft_strdup.c | 17 | ||||
| -rw-r--r-- | ft_strequ.c | 4 | ||||
| -rw-r--r-- | ft_striter.c | 6 | ||||
| -rw-r--r-- | ft_striteri.c | 6 | ||||
| -rw-r--r-- | ft_strjoin.c | 9 | ||||
| -rw-r--r-- | ft_strlcat.c | 3 | ||||
| -rw-r--r-- | ft_strlcpy.c | 3 | ||||
| -rw-r--r-- | ft_strlen.c | 4 | ||||
| -rw-r--r-- | ft_strmap.c | 5 | ||||
| -rw-r--r-- | ft_strmapi.c | 3 | ||||
| -rw-r--r-- | ft_strncat.c | 8 | ||||
| -rw-r--r-- | ft_strncmp.c | 4 | ||||
| -rw-r--r-- | ft_strncpy.c | 7 | ||||
| -rw-r--r-- | ft_strndup.c | 17 | ||||
| -rw-r--r-- | ft_strnequ.c | 4 | ||||
| -rw-r--r-- | ft_strnew.c | 14 | ||||
| -rw-r--r-- | ft_strnstr.c | 6 | ||||
| -rw-r--r-- | ft_strrchr.c | 3 | ||||
| -rw-r--r-- | ft_strstr.c | 28 | ||||
| -rw-r--r-- | ft_strtrim.c | 10 | ||||
| -rw-r--r-- | ft_substr.c | 20 | ||||
| -rw-r--r-- | ft_tolower.c | 10 | ||||
| -rw-r--r-- | ft_toupper.c | 4 | ||||
| -rw-r--r-- | libft.h | 86 | ||||
| -rw-r--r-- | pre2019_subject.en.pdf | bin | 0 -> 1455686 bytes | |||
| -rw-r--r-- | subject.en.pdf | bin | 0 -> 1657956 bytes |
60 files changed, 231 insertions, 360 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..56fd0f2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +a.out +*.o +*.so +*.a +main.c @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/08 15:45:53 by cacharle #+# #+# # -# Updated: 2019/11/15 09:19:31 by cacharle ### ########.fr # +# Updated: 2019/11/20 04:13:32 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -16,10 +16,6 @@ RM = rm -f CC = gcc CCFLAGS = -Wall -Wextra -Werror -ifeq ($(shell uname),Linux) - CCFLAGS += -D LINUX -endif - NAME = libft.a SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \ ft_isprint.c ft_itoa.c ft_memalloc.c ft_memccpy.c ft_memchr.c \ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e930431 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# libft @@ -6,48 +6,38 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:46:16 by cacharle #+# #+# */ -/* Updated: 2019/10/19 10:50:39 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 01:43:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + #define MIN_INT -2147483648 #define MAX_INT 2147483647 -static int precheck(const char **str) -{ - int is_negative; - - while (**str == ' ' || **str == '\t' || **str == '\n' - || **str == '\v' || **str == '\f' || **str == '\r') - (*str)++; - is_negative = 0; - if (**str == '-' || **str == '+') - { - if (**str == '-') - is_negative = 1; - (*str)++; - } - return (is_negative); -} - int ft_atoi(const char *str) { unsigned int nb; int i; int is_negative; - is_negative = precheck(&str); + while (*str == ' ' || *str == '\t' || *str == '\n' + || *str == '\v' || *str == '\f' || *str == '\r') + str++; + is_negative = 0; + if (*str == '-' || *str == '+') + if (*str++ == '-') + is_negative = 1; i = 0; nb = 0; - while (str[i] >= '0' && str[i] <= '9') + while (ft_isdigit(str[i])) { if (!is_negative && nb > (unsigned int)MAX_INT) return (-1); else if (nb > (unsigned int)MIN_INT) return (0); nb *= 10; - nb += str[i] - '0'; - i++; + nb += str[i++] & 0x0F; } return ((int)(is_negative ? -nb : nb)); } @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:50:10 by cacharle #+# #+# */ -/* Updated: 2019/10/18 12:26:42 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:29:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> #include "libft.h" void ft_bzero(void *s, size_t n) diff --git a/ft_calloc.c b/ft_calloc.c index 97a45f2..0a79c03 100644 --- a/ft_calloc.c +++ b/ft_calloc.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 12:45:37 by cacharle #+# #+# */ -/* Updated: 2019/10/20 10:55:01 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:00:52 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" void *ft_calloc(size_t count, size_t size) @@ -6,59 +6,34 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:19:56 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:21:23 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:13:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> +#include "libft.h" -static int count_len(int nbr) +char *ft_itoa(int n) { - int counter; + char *str; + int len; unsigned int u_nbr; - if (nbr == 0) - return (1); - counter = 0; - u_nbr = nbr; - if (nbr < 0) - { - counter++; - u_nbr = -nbr; - } + len = n < 0 || n == 0 ? 1 : 0; + u_nbr = n < 0 ? -n : n; while (u_nbr > 0) { u_nbr /= 10; - counter++; + len++; } - return (counter); -} - -char *ft_itoa(int n) -{ - char *str; - int len; - int is_negative; - unsigned int u_nbr; - - len = count_len(n); - if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + if ((str = ft_strnew(len)) == NULL) return (NULL); - str[len] = '\0'; - is_negative = 0; - u_nbr = n; + u_nbr = n < 0 ? -n : n; if (n < 0) - { - is_negative = 1; str[0] = '-'; - u_nbr = -n; - } - len--; - while (len >= (is_negative ? 1 : 0)) + while (--len >= (n < 0 ? 1 : 0)) { - str[len] = u_nbr % 10 + '0'; + str[len] = (u_nbr % 10) | 0x30; u_nbr /= 10; - len--; } return (str); } diff --git a/ft_lstadd_back_bonus.c b/ft_lstadd_back_bonus.c index be2dbf3..01eb00c 100644 --- a/ft_lstadd_back_bonus.c +++ b/ft_lstadd_back_bonus.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 09:02:03 by cacharle #+# #+# */ -/* Updated: 2019/10/20 10:55:13 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:01:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" void ft_lstadd_back(t_list **alst, t_list *new) diff --git a/ft_lstclear_bonus.c b/ft_lstclear_bonus.c index e8f0541..ee1d9e5 100644 --- a/ft_lstclear_bonus.c +++ b/ft_lstclear_bonus.c @@ -6,16 +6,15 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 09:02:39 by cacharle #+# #+# */ -/* Updated: 2019/10/17 11:03:13 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:02:37 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" void ft_lstclear(t_list **lst, void (*del)(void *)) { - if (lst == NULL || del == NULL) + if (lst == NULL) return ; if (*lst == NULL) return ; diff --git a/ft_lstdelone_bonus.c b/ft_lstdelone_bonus.c index aaa314f..30cec69 100644 --- a/ft_lstdelone_bonus.c +++ b/ft_lstdelone_bonus.c @@ -6,17 +6,17 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 09:03:02 by cacharle #+# #+# */ -/* Updated: 2019/10/24 09:36:04 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:02:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" void ft_lstdelone(t_list *lst, void (*del)(void *)) { - if (lst == NULL || del == NULL) + if (lst == NULL) return ; - (*del)(lst->content); + if (del != NULL) + (*del)(lst->content); free(lst); } diff --git a/ft_lstiter_bonus.c b/ft_lstiter_bonus.c index 7b543cb..282e0fa 100644 --- a/ft_lstiter_bonus.c +++ b/ft_lstiter_bonus.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 09:03:22 by cacharle #+# #+# */ -/* Updated: 2019/10/17 09:02:38 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:01:39 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" void ft_lstiter(t_list *lst, void (*f)(void *)) diff --git a/ft_lstlast_bonus.c b/ft_lstlast_bonus.c index 4246cfc..247f4da 100644 --- a/ft_lstlast_bonus.c +++ b/ft_lstlast_bonus.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 09:03:40 by cacharle #+# #+# */ -/* Updated: 2019/10/09 09:08:12 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:02:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" t_list *ft_lstlast(t_list *lst) diff --git a/ft_lstmap_bonus.c b/ft_lstmap_bonus.c index 81b9949..c623d6f 100644 --- a/ft_lstmap_bonus.c +++ b/ft_lstmap_bonus.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 09:03:57 by cacharle #+# #+# */ -/* Updated: 2019/10/24 09:41:20 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:01:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) @@ -18,7 +17,7 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) t_list *mapped; t_list *tmp; - if (lst == NULL || f == NULL || del == NULL) + if (lst == NULL || f == NULL) return (NULL); mapped = NULL; while (lst != NULL) diff --git a/ft_lstnew_bonus.c b/ft_lstnew_bonus.c index 3f6ca38..ea10e4d 100644 --- a/ft_lstnew_bonus.c +++ b/ft_lstnew_bonus.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 09:01:16 by cacharle #+# #+# */ -/* Updated: 2019/10/10 14:51:30 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:01:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" t_list *ft_lstnew(void const *content) diff --git a/ft_lstsize_bonus.c b/ft_lstsize_bonus.c index 0613c93..b9d65d2 100644 --- a/ft_lstsize_bonus.c +++ b/ft_lstsize_bonus.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 09:04:28 by cacharle #+# #+# */ -/* Updated: 2019/10/09 09:20:36 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:01:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" int ft_lstsize(t_list *lst) diff --git a/ft_memalloc.c b/ft_memalloc.c index c485bfd..5aab2ec 100644 --- a/ft_memalloc.c +++ b/ft_memalloc.c @@ -6,20 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:07:14 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:07:47 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:28:56 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> -#include <string.h> +#include "libft.h" void *ft_memalloc(size_t size) { - void *ptr; - - if ((ptr = malloc(size)) == NULL) - return (NULL); - while (size-- > 0) - ((unsigned char*)ptr)[size] = 0; - return (ptr); + return (ft_calloc(size, 1)); } diff --git a/ft_memccpy.c b/ft_memccpy.c index f3dacbc..f95aa03 100644 --- a/ft_memccpy.c +++ b/ft_memccpy.c @@ -6,27 +6,26 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:01:53 by cacharle #+# #+# */ -/* Updated: 2019/10/20 12:41:51 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:30:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> +#include "libft.h" void *ft_memccpy(void *dest, const void *src, int c, size_t n) { - size_t i; - unsigned char *uc_dest; - unsigned char *uc_src; + size_t i; + t_byte *cast_dest; + t_byte *cast_src; - uc_dest = (unsigned char*)dest; - uc_src = (unsigned char*)src; - i = 0; - while (i < n) + cast_dest = (t_byte*)dest; + cast_src = (t_byte*)src; + i = -1; + while (++i < n) { - uc_dest[i] = uc_src[i]; - if (uc_dest[i] == (unsigned char)c) - return (uc_dest + i + 1); - i++; + cast_dest[i] = cast_src[i]; + if (cast_dest[i] == (unsigned char)c) + return (cast_dest + i + 1); } return (NULL); } diff --git a/ft_memchr.c b/ft_memchr.c index 934ede8..d2364db 100644 --- a/ft_memchr.c +++ b/ft_memchr.c @@ -6,12 +6,11 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:55:31 by cacharle #+# #+# */ -/* Updated: 2019/10/20 12:56:46 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:30:55 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -#include <string.h> void *ft_memchr(const void *s, int c, size_t n) { diff --git a/ft_memcpy.c b/ft_memcpy.c index 642db20..70837bc 100644 --- a/ft_memcpy.c +++ b/ft_memcpy.c @@ -6,23 +6,17 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:00:07 by cacharle #+# #+# */ -/* Updated: 2019/10/21 11:17:05 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:20:29 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> +#include "libft.h" void *ft_memcpy(void *dest, const void *src, size_t n) { - size_t i; - - if (dest == NULL) - return (NULL); - i = 0; - while (i < n) - { - *((char*)dest + i) = *((char*)src + i); - i++; - } + if (dest == src) + return (dest); + while (n-- > 0) + *((t_byte*)dest + n) = *((t_byte*)src + n); return (dest); } diff --git a/ft_memdel.c b/ft_memdel.c index 206e5af..2b21f33 100644 --- a/ft_memdel.c +++ b/ft_memdel.c @@ -6,14 +6,16 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:00:56 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:01:06 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:22:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> +#include "libft.h" void ft_memdel(void **ap) { + if (ap == NULL) + return ; free(*ap); *ap = NULL; } diff --git a/ft_memmove.c b/ft_memmove.c index 0b7e430..aa107bd 100644 --- a/ft_memmove.c +++ b/ft_memmove.c @@ -6,12 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:03:21 by cacharle #+# #+# */ -/* Updated: 2019/10/21 11:07:04 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:31:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> -#include <string.h> #include "libft.h" void *ft_memmove(void *dst, const void *src, size_t len) diff --git a/ft_memset.c b/ft_memset.c index c1fcda6..cd7616c 100644 --- a/ft_memset.c +++ b/ft_memset.c @@ -6,23 +6,15 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:01:23 by cacharle #+# #+# */ -/* Updated: 2019/10/21 11:35:07 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:22:29 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> #include "libft.h" void *ft_memset(void *s, int c, size_t n) { - t_byte cast_c; - t_byte *cast_s; - - if (s == NULL) - return (NULL); - cast_c = (t_byte)c; - cast_s = (t_byte*)s; while (n-- > 0) - *cast_s++ = cast_c; + *((t_byte*)s + n) = (t_byte)c; return (s); } diff --git a/ft_putchar.c b/ft_putchar.c index 0e01aea..2838f0a 100644 --- a/ft_putchar.c +++ b/ft_putchar.c @@ -6,11 +6,11 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:53:31 by cacharle #+# #+# */ -/* Updated: 2019/10/09 08:45:20 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:49:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <unistd.h> +#include "libft.h" void ft_putchar(char c) { diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c index 78d94a8..97d6f7a 100644 --- a/ft_putchar_fd.c +++ b/ft_putchar_fd.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:42:34 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:42:40 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:49:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ft_putendl.c b/ft_putendl.c index 719331b..880977e 100644 --- a/ft_putendl.c +++ b/ft_putendl.c @@ -6,15 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:42:54 by cacharle #+# #+# */ -/* Updated: 2019/10/18 12:08:37 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:00:32 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <unistd.h> #include "libft.h" void ft_putendl(char *s) { - ft_putstr(s); - ft_putchar('\n'); + ft_putendl_fd(s, STDOUT_FILENO); } diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c index 399b03e..a8077fc 100644 --- a/ft_putendl_fd.c +++ b/ft_putendl_fd.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:44:06 by cacharle #+# #+# */ -/* Updated: 2019/10/07 13:32:37 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:00:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ft_putnbr.c b/ft_putnbr.c index 9e1f894..247df40 100644 --- a/ft_putnbr.c +++ b/ft_putnbr.c @@ -6,24 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:52:33 by cacharle #+# #+# */ -/* Updated: 2019/10/07 09:53:09 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:59:34 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <unistd.h> #include "libft.h" void ft_putnbr(int n) { - unsigned int p_n; - - p_n = n; - if (n < 0) - { - ft_putchar('-'); - p_n = -n; - } - if (p_n > 9) - ft_putnbr(p_n / 10); - ft_putchar(p_n % 10 + '0'); + ft_putnbr_fd(n, STDOUT_FILENO); } diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c index 89cae15..169d1b5 100644 --- a/ft_putnbr_fd.c +++ b/ft_putnbr_fd.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:40:35 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:40:52 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:46:11 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,5 +26,5 @@ void ft_putnbr_fd(int n, int fd) } if (p_n > 9) ft_putnbr_fd(p_n / 10, fd); - ft_putchar_fd(p_n % 10 + '0', fd); + ft_putchar_fd(p_n % 10 | 0x30, fd); } diff --git a/ft_putstr.c b/ft_putstr.c index ac402ed..14b01a3 100644 --- a/ft_putstr.c +++ b/ft_putstr.c @@ -6,20 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:52:12 by cacharle #+# #+# */ -/* Updated: 2019/10/17 09:06:58 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:48:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <unistd.h> +#include "libft.h" void ft_putstr(char const *s) { - int i; - - i = 0; - while (s[i]) - { - write(STDOUT_FILENO, &s[i], 1); - i++; - } + ft_putstr_fd((char*)s, STDOUT_FILENO); } diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c index 645c133..d0279ab 100644 --- a/ft_putstr_fd.c +++ b/ft_putstr_fd.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:40:15 by cacharle #+# #+# */ -/* Updated: 2019/10/07 13:26:14 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:47:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,5 @@ void ft_putstr_fd(char *s, int fd) { if (s == NULL || fd < 0 || fd > OPEN_MAX) return ; - while (*s) - write(fd, s++, 1); + write(fd, s, ft_strlen(s)); } @@ -6,11 +6,11 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */ -/* Updated: 2019/10/17 08:39:40 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:08:27 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> +#include "libft.h" static size_t count_segment(char const *s, char c) { @@ -33,30 +33,12 @@ static size_t count_segment(char const *s, char c) return (counter); } -static char *ft_strndup(const char *s1, size_t n) -{ - char *clone; - size_t i; - - if ((clone = (char*)malloc(sizeof(char) * (n + 1))) == NULL) - return (NULL); - i = 0; - while (i < n) - { - clone[i] = s1[i]; - i++; - } - clone[i] = '\0'; - return (clone); -} - static void *destroy_strs(char **strs) { - int i; - - i = 0; - while (strs[i] != NULL) - free(strs[i++]); + if (strs == NULL) + return (NULL); + while (*strs != NULL) + free(*strs++); free(strs); return (NULL); } diff --git a/ft_strchr.c b/ft_strchr.c index 45eecdd..0ae161b 100644 --- a/ft_strchr.c +++ b/ft_strchr.c @@ -6,11 +6,11 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:14:47 by cacharle #+# #+# */ -/* Updated: 2019/10/20 13:10:19 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:02:02 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> +#include "libft.h" char *ft_strchr(const char *s, int c) { @@ -20,7 +20,7 @@ char *ft_strchr(const char *s, int c) return ((char*)s); s++; } - if ((char)c == 0) + if ((char)c == '\0') return ((char*)s); return (NULL); } diff --git a/ft_strclr.c b/ft_strclr.c index 2f936bf..c927a46 100644 --- a/ft_strclr.c +++ b/ft_strclr.c @@ -6,12 +6,16 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:15:18 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:15:24 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 02:02:02 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + void ft_strclr(char *s) { + if (s == NULL) + return ; while (*s) *s++ = '\0'; } diff --git a/ft_strdel.c b/ft_strdel.c index d62d36d..05cf064 100644 --- a/ft_strdel.c +++ b/ft_strdel.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:39:14 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:39:33 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 01:58:27 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ft_strdup.c b/ft_strdup.c index f0f8a06..65a6ac6 100644 --- a/ft_strdup.c +++ b/ft_strdup.c @@ -6,28 +6,17 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:18:07 by cacharle #+# #+# */ -/* Updated: 2019/10/18 14:28:41 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:13:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" char *ft_strdup(const char *s) { char *clone; - size_t i; - size_t len; - len = ft_strlen(s); - if ((clone = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + if ((clone = ft_strnew(ft_strlen(s))) == NULL) return (NULL); - i = 0; - while (i < len) - { - clone[i] = s[i]; - i++; - } - clone[i] = '\0'; - return (clone); + return (ft_strcpy(clone, s)); } diff --git a/ft_strequ.c b/ft_strequ.c index ea41207..75ccb81 100644 --- a/ft_strequ.c +++ b/ft_strequ.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:18:34 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:18:41 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 02:00:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,7 @@ int ft_strequ(char const *s1, char const *s2) { + if (s1 == NULL || s2 == NULL) + return (0); return (ft_strcmp(s1, s2) == 0); } diff --git a/ft_striter.c b/ft_striter.c index a3a2306..f410d24 100644 --- a/ft_striter.c +++ b/ft_striter.c @@ -6,12 +6,16 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:38:16 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:38:24 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 02:01:32 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + void ft_striter(char *s, void (*f)(char *)) { + if (s == NULL || f == NULL) + return ; while (*s) (*f)(s++); } diff --git a/ft_striteri.c b/ft_striteri.c index 0d3ccdf..05f15d4 100644 --- a/ft_striteri.c +++ b/ft_striteri.c @@ -6,14 +6,18 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:33:09 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:37:37 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 02:01:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + void ft_striteri(char *s, void (*f)(unsigned int, char *)) { unsigned int i; + if (s == NULL || f == NULL) + return ; i = 0; while (s[i]) { diff --git a/ft_strjoin.c b/ft_strjoin.c index 0078d65..2bc4908 100644 --- a/ft_strjoin.c +++ b/ft_strjoin.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:35:26 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:38:07 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:02:20 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" char *ft_strjoin(char const *s1, char const *s2) @@ -19,9 +18,7 @@ char *ft_strjoin(char const *s1, char const *s2) if (s1 == NULL || s2 == NULL) return (NULL); - if ((joined = (char*)malloc(sizeof(char) - * (ft_strlen(s1) + ft_strlen(s2) + 1))) == NULL) + if ((joined = ft_strnew(ft_strlen(s1) + ft_strlen(s2))) == NULL) return (NULL); - joined = ft_strcpy(joined, s1); - return (ft_strcat(joined, s2)); + return (ft_strcat(ft_strcpy(joined, s1), s2)); } diff --git a/ft_strlcat.c b/ft_strlcat.c index 109d390..ce7fa0b 100644 --- a/ft_strlcat.c +++ b/ft_strlcat.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:31:37 by cacharle #+# #+# */ -/* Updated: 2019/10/20 13:16:45 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:31:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> #include "libft.h" size_t ft_strlcat(char *dst, const char *src, size_t size) diff --git a/ft_strlcpy.c b/ft_strlcpy.c index 0b1b0d4..6afb8f5 100644 --- a/ft_strlcpy.c +++ b/ft_strlcpy.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 12:28:47 by cacharle #+# #+# */ -/* Updated: 2019/10/21 11:05:52 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:31:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> #include "libft.h" size_t ft_strlcpy(char *dst, const char *src, size_t size) diff --git a/ft_strlen.c b/ft_strlen.c index 8a5431b..dc612a8 100644 --- a/ft_strlen.c +++ b/ft_strlen.c @@ -6,11 +6,11 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:32:48 by cacharle #+# #+# */ -/* Updated: 2019/10/18 13:07:56 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:31:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> +#include "libft.h" size_t ft_strlen(const char *s) { diff --git a/ft_strmap.c b/ft_strmap.c index 8661a68..61d16f1 100644 --- a/ft_strmap.c +++ b/ft_strmap.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:29:52 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:31:29 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:02:11 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" char *ft_strmap(char const *s, char (*f)(char)) @@ -19,6 +18,8 @@ char *ft_strmap(char const *s, char (*f)(char)) size_t len; char *mapped; + if (s == NULL || f == NULL) + return (NULL); len = ft_strlen(s); if ((mapped = (char*)malloc(sizeof(char) * (len + 1))) == NULL) return (NULL); diff --git a/ft_strmapi.c b/ft_strmapi.c index 7784980..71d77e4 100644 --- a/ft_strmapi.c +++ b/ft_strmapi.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:29:32 by cacharle #+# #+# */ -/* Updated: 2019/10/07 13:22:15 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:02:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" char *ft_strmapi(char *s, char (*f)(unsigned int, char)) diff --git a/ft_strncat.c b/ft_strncat.c index 3bbb41f..d68db0a 100644 --- a/ft_strncat.c +++ b/ft_strncat.c @@ -6,20 +6,18 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:28:37 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:30:53 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:33:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> +#include "libft.h" char *ft_strncat(char *dest, const char *src, size_t n) { size_t i; size_t j; - i = 0; - while (dest[i]) - i++; + i = ft_strlen(dest); j = 0; while (j < n && src[j]) { diff --git a/ft_strncmp.c b/ft_strncmp.c index e8188d0..46f79c2 100644 --- a/ft_strncmp.c +++ b/ft_strncmp.c @@ -6,11 +6,11 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:27:34 by cacharle #+# #+# */ -/* Updated: 2019/10/21 11:04:38 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:33:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> +#include "libft.h" int ft_strncmp(const char *s1, const char *s2, size_t n) { diff --git a/ft_strncpy.c b/ft_strncpy.c index 4a4efed..5f4c69f 100644 --- a/ft_strncpy.c +++ b/ft_strncpy.c @@ -6,11 +6,11 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:26:59 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:28:17 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:36:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> +#include "libft.h" char *ft_strncpy(char *dest, const char *src, size_t n) { @@ -22,7 +22,6 @@ char *ft_strncpy(char *dest, const char *src, size_t n) dest[i] = src[i]; i++; } - while (i < n) - dest[i++] = '\0'; + ft_bzero(dest + i, n - i); return (dest); } diff --git a/ft_strndup.c b/ft_strndup.c index d43c0ce..0683dae 100644 --- a/ft_strndup.c +++ b/ft_strndup.c @@ -6,26 +6,17 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/25 03:28:52 by cacharle #+# #+# */ -/* Updated: 2019/10/25 03:36:24 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:15:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> -#include <string.h> +#include "libft.h" char *ft_strndup(const char *s1, size_t n) { char *clone; - size_t i; - if ((clone = (char*)malloc(sizeof(char) * (n + 1))) == NULL) + if ((clone = ft_strnew(n)) == NULL) return (NULL); - i = 0; - while (i < n && s1[i]) - { - clone[i] = s1[i]; - i++; - } - clone[i] = '\0'; - return (clone); + return (ft_strncpy(clone, s1, n)); } diff --git a/ft_strnequ.c b/ft_strnequ.c index 34d0627..e242ee7 100644 --- a/ft_strnequ.c +++ b/ft_strnequ.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:30:27 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:30:33 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 02:00:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,7 @@ int ft_strnequ(char const *s1, char const *s2, size_t n) { + if (s1 == NULL || s2 == NULL) + return (0); return (ft_strncmp(s1, s2, n) == 0); } diff --git a/ft_strnew.c b/ft_strnew.c index 4274d5d..1bca6d5 100644 --- a/ft_strnew.c +++ b/ft_strnew.c @@ -6,21 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:17:34 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:17:59 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:16:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> -#include <string.h> +#include "libft.h" char *ft_strnew(size_t size) { - char *str; - - if ((str = (char*)malloc(sizeof(char) * (size + 1))) == NULL) - return (NULL); - str[size] = '\0'; - while (size-- > 0) - str[size] = '\0'; - return (str); + return ((char*)ft_calloc(size + 1, sizeof(char))); } diff --git a/ft_strnstr.c b/ft_strnstr.c index 4ca9f4b..4995637 100644 --- a/ft_strnstr.c +++ b/ft_strnstr.c @@ -6,12 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:25:13 by cacharle #+# #+# */ -/* Updated: 2019/10/21 11:04:56 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:58:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> -#include <string.h> #include "libft.h" char *ft_strnstr(const char *haystack, const char *needle, size_t len) @@ -23,7 +21,7 @@ char *ft_strnstr(const char *haystack, const char *needle, size_t len) return ((char*)haystack); while (*haystack && len-- >= needle_len) { - if (ft_strncmp(haystack, needle, needle_len) == 0) + if (ft_strnequ(haystack, needle, needle_len)) return ((char*)haystack); haystack++; } diff --git a/ft_strrchr.c b/ft_strrchr.c index 5653c2b..56c8be5 100644 --- a/ft_strrchr.c +++ b/ft_strrchr.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:26:24 by cacharle #+# #+# */ -/* Updated: 2019/10/20 13:11:13 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:36:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <string.h> #include "libft.h" char *ft_strrchr(const char *s, int c) diff --git a/ft_strstr.c b/ft_strstr.c index f71423f..4d4d403 100644 --- a/ft_strstr.c +++ b/ft_strstr.c @@ -6,34 +6,24 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:15:29 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:21:49 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:58:32 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> -#include <string.h> #include "libft.h" char *ft_strstr(const char *haystack, const char *needle) { - size_t i; - char *cursor; + size_t needle_len; - cursor = (char*)haystack; - if (!ft_strlen(needle)) - return (cursor); - while (*cursor) + needle_len = ft_strlen(needle); + if (needle_len == 0) + return ((char*)haystack); + while (*haystack) { - i = 0; - while (needle[i] && cursor[i]) - { - if (needle[i] != cursor[i]) - break ; - i++; - } - if (i == ft_strlen(needle)) - return (cursor); - cursor++; + if (ft_strnequ(haystack, needle, needle_len)) + return ((char*)haystack); + haystack++; } return (NULL); } diff --git a/ft_strtrim.c b/ft_strtrim.c index 8cde14e..aa48826 100644 --- a/ft_strtrim.c +++ b/ft_strtrim.c @@ -6,18 +6,16 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:24:16 by cacharle #+# #+# */ -/* Updated: 2019/10/14 13:22:33 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:52:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" char *ft_strtrim(char const *s1, char const *set) { size_t start; size_t len; - char *trimed; if (s1 == NULL || set == NULL) return (NULL); @@ -29,9 +27,5 @@ char *ft_strtrim(char const *s1, char const *set) while (s1[start + len - 1] && ft_strchr(set, s1[start + len - 1]) != NULL) len--; - if ((trimed = (char*)malloc(sizeof(char) * (len + 1))) == NULL) - return (NULL); - trimed = ft_strncpy(trimed, &s1[start], len); - trimed[len] = '\0'; - return (trimed); + return (ft_substr(s1, start, len)); } diff --git a/ft_substr.c b/ft_substr.c index 88f7203..84d6c58 100644 --- a/ft_substr.c +++ b/ft_substr.c @@ -6,31 +6,21 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ -/* Updated: 2019/10/18 11:55:44 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 03:57:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" char *ft_substr(char const *s, unsigned int start, size_t len) { - unsigned int i; char *sub; if (s == NULL) return (NULL); - if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + if ((sub = ft_strnew(len)) == NULL) return (NULL); - i = 0; - if (!(start > ft_strlen(s))) - { - while (i < len && s[start + i]) - { - sub[i] = s[start + i]; - i++; - } - } - sub[i] = '\0'; - return (sub); + if (start > ft_strlen(s)) + return (sub); + return (ft_strncpy(sub, s + start, len)); } diff --git a/ft_tolower.c b/ft_tolower.c index 6a252aa..919469f 100644 --- a/ft_tolower.c +++ b/ft_tolower.c @@ -6,13 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:14:26 by cacharle #+# #+# */ -/* Updated: 2019/10/20 13:04:16 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 01:04:02 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +/* +** E: 0100 0101 +** e: 0110 0101 +** ^ +*/ + int ft_tolower(int c) { if (c >= 'A' && c <= 'Z') - return (c + 'a' - 'A'); + return (c | 0b00100000); return (c); } diff --git a/ft_toupper.c b/ft_toupper.c index 0257786..8579b91 100644 --- a/ft_toupper.c +++ b/ft_toupper.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:14:10 by cacharle #+# #+# */ -/* Updated: 2019/10/07 10:14:20 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 01:04:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,6 @@ int ft_toupper(int c) { if (c >= 'a' && c <= 'z') - return (c - 'a' + 'A'); + return (c ^ 0b00100000); return (c); } @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */ -/* Updated: 2019/11/16 08:12:45 by cacharle ### ########.fr */ +/* Updated: 2019/11/20 04:13:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,25 +14,19 @@ # define LIBFT_H # include <unistd.h> -# include <string.h> +# include <stdlib.h> +# include <stddef.h> # include <limits.h> # include "get_next_line.h" -# ifdef LINUX -# include <stdio.h> -# define OPEN_MAX FOPEN_MAX -# endif - # define TRUE 1 # define FALSE 0 typedef unsigned char t_byte; -typedef struct s_list -{ - void *content; - struct s_list *next; -} t_list; +/* +** memory +*/ void *ft_memset(void *s, int c, size_t n); void ft_bzero(void *s, size_t n); @@ -41,6 +35,14 @@ void *ft_memccpy(void *dest, 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); + +void *ft_memalloc(size_t size); +void ft_memdel(void **ap); + +/* +** string +*/ + size_t ft_strlen(const char *s); char *ft_strdup(const char *s); char *ft_strcpy(char *dest, const char *src); @@ -56,22 +58,13 @@ 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); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); int ft_atoi(const char *nptr); -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_calloc(size_t count, size_t size); -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 *s, char (*f)(unsigned int, char)); int ft_strequ(char const *s1, char const *s2); @@ -80,7 +73,35 @@ char *ft_substr(char const *s, unsigned int start, size_t len); char *ft_strjoin(char const *s1, char const *s2); char *ft_strtrim(char const *s1, char const *set); char **ft_split(char const *s, char c); + +char *ft_strjoin_free(char const *s1, char const *s2, + int free_nb); +char *ft_strjoin_free_snd(char const *s1, char const *s2); +int ft_strcount(char *str, char c); + +/* +** character +*/ + +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); + +/* +** allocation +*/ + +void *ft_calloc(size_t count, size_t size); char *ft_itoa(int n); + +/* +** fildes +*/ + void ft_putendl(char *s); void ft_putchar(char c); void ft_putstr(char const *s); @@ -92,9 +113,15 @@ void ft_putnbr_fd(int n, int fd); char *ft_strndup(const char *s1, size_t n); /* -** bonus +** list */ +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + t_list *ft_lstnew(void const *content); void ft_lstadd_front(t_list **alst, t_list *new); int ft_lstsize(t_list *lst); @@ -107,13 +134,4 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); void ft_lstpop_front(t_list **lst, void (*del)(void *)); -/* -** added -*/ - -char *ft_strjoin_free(char const *s1, char const *s2, - int free_nb); -char *ft_strjoin_free_snd(char const *s1, char const *s2); -int ft_strcount(char *str, char c); - #endif diff --git a/pre2019_subject.en.pdf b/pre2019_subject.en.pdf Binary files differnew file mode 100644 index 0000000..e5bee09 --- /dev/null +++ b/pre2019_subject.en.pdf diff --git a/subject.en.pdf b/subject.en.pdf Binary files differnew file mode 100644 index 0000000..67a054c --- /dev/null +++ b/subject.en.pdf |
