diff options
| -rw-r--r-- | .gitignore | 5 | ||||
| -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 | 5 | ||||
| -rw-r--r-- | ft_putendl.c | 6 | ||||
| -rw-r--r-- | ft_putendl_fd.c | 3 | ||||
| -rw-r--r-- | ft_putnbr.c | 15 | ||||
| -rw-r--r-- | ft_putnbr_fd.c | 6 | ||||
| -rw-r--r-- | ft_putstr.c | 13 | ||||
| -rw-r--r-- | ft_putstr_fd.c | 8 | ||||
| -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 | 19 | ||||
| -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 | 81 | ||||
| -rw-r--r-- | pre2019_subject.en.pdf | bin | 0 -> 1455686 bytes | |||
| -rw-r--r-- | subject.en.pdf | bin | 0 -> 1657956 bytes |
59 files changed, 233 insertions, 358 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 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) |
