From: Lukas Jiriste Date: Tue, 15 Aug 2023 11:03:47 +0000 (+0200) Subject: Implemented every function from Part 1. X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=c9219cab4867c2484e6ca1276ceb8c468e9980ec;p=Libft.git Implemented every function from Part 1. Added libft.h header and Makefile. Everything passes norminette and compiles to libft.a on make command. --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1ae2e12 --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +CC = cc +CFLAGS = -Wall -Wextra -Werror +AR = ar + +RM = rm -f + +INCDIR = . +SRCDIR = . +# SOURCES = $(addprefix $(SRCDIR)/, ft_putchar.c ft_putstr.c ft_strcmp.c ft_strlen.c ft_swap.c) +SOURCES = $(shell find $(SRCDIR) -name "*.c") +OBJECTS = $(SOURCES:.c=.o) + +NAME = libft.a + +all : $(NAME) + +$(NAME) : $(OBJECTS) + $(AR) rcs $(NAME) $(OBJECTS) + +%.o : %.c + $(CC) $(CFLAGS) -o $@ -c $< -I $(INCDIR) + +clean : + $(RM) $(OBJECTS) + +fclean : clean + $(RM) $(NAME) + +re : fclean + $(MAKE) all diff --git a/ft_atoi.c b/ft_atoi.c new file mode 100644 index 0000000..59dd217 --- /dev/null +++ b/ft_atoi.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/13 13:44:39 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:45:44 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int atoi(const char *nptr) +{ + int res; + int sign; + + res = 0; + sign = 1; + while (ft_isspace(*nptr)) + ++nptr; + if ((*nptr == '+' || *nptr == '-')) + { + if (*nptr == '-') + sign *= -1; + ++nptr; + } + while (ft_isdigit(*nptr)) + { + res *= 10; + res += *nptr - '0'; + ++nptr; + } + return (sign * res); +} diff --git a/ft_calloc.c b/ft_calloc.c new file mode 100644 index 0000000..b810d7e --- /dev/null +++ b/ft_calloc.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:34:52 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:54:11 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // for malloc +#include "libft.h" + +void *calloc(size_t nmemb, size_t size) +{ + size_t total_bytes; + void *res; + + if (nmemb == 0 || size == 0) + return (NULL); + total_bytes = nmemb * size; + if (total_bytes / size != nmemb) + return (NULL); + res = malloc(total_bytes); + ft_memset(res, 0, total_bytes); + return (res); +} diff --git a/ft_isascii.c b/ft_isascii.c new file mode 100644 index 0000000..43682c0 --- /dev/null +++ b/ft_isascii.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:19:40 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:20:08 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int is_ascii(int c) +{ + return (0x00 <= c && c <= 0x7F); +} diff --git a/ft_islower.c b/ft_islower.c index ebcf47d..1d26006 100644 --- a/ft_islower.c +++ b/ft_islower.c @@ -6,13 +6,13 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/14 13:31:47 by ljiriste #+# #+# */ -/* Updated: 2023/08/14 13:32:05 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 12:55:33 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static int ft_islower(int c) +int ft_islower(int c) { return ('a' <= c && c <= 'z'); } diff --git a/ft_isprint.c b/ft_isprint.c new file mode 100644 index 0000000..4cb7934 --- /dev/null +++ b/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:17:32 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:20:22 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (0x20 <= c && c <= 0x7E); +} diff --git a/ft_isspace.c b/ft_isspace.c new file mode 100644 index 0000000..cbf9b09 --- /dev/null +++ b/ft_isspace.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:29:54 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 11:30:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int is_space(int c) +{ + return (c == ' ' || ('\t' <= c && c <= '\r')); +} diff --git a/ft_isupper.c b/ft_isupper.c index 4a9acd2..adddb5b 100644 --- a/ft_isupper.c +++ b/ft_isupper.c @@ -6,13 +6,13 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/14 13:31:05 by ljiriste #+# #+# */ -/* Updated: 2023/08/14 13:31:31 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 12:55:24 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static int ft_isupper(int c) +int ft_isupper(int c) { return ('A' <= c && c <= 'Z'); } diff --git a/ft_memchr.c b/ft_memchr.c new file mode 100644 index 0000000..3a4a944 --- /dev/null +++ b/ft_memchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:56:53 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 11:01:34 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +void *memchr(const void *s, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if (*((unsigned char *)s + i) == (unsigned char)c) + return ((unsigned char *)s + i); + ++i; + } + return (NULL); +} diff --git a/ft_memcmp.c b/ft_memcmp.c new file mode 100644 index 0000000..e3c320c --- /dev/null +++ b/ft_memcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:06:15 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:53:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + + i = 0; + while (*((unsigned char *)s1 + i) == *((unsigned char *)s2 + i) + && i + 1 < n) + ++i; + if (n > 0) + return (*((unsigned char *)s1 + i) == *((unsigned char *)s2 + i)); + else + return (0); +} diff --git a/ft_memmove.c b/ft_memmove.c index 83692d1..8cb233d 100644 --- a/ft_memmove.c +++ b/ft_memmove.c @@ -6,14 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/14 13:36:32 by ljiriste #+# #+# */ -/* Updated: 2023/08/14 13:37:03 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 12:03:46 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "libft.h" -void *ft_memmove(void *dest, const void *src, size_t n) +void *ft_memmove(void *dest, const void *src, size_t n) { size_t i; diff --git a/ft_memset.c b/ft_memset.c index bfa0468..cbc4239 100644 --- a/ft_memset.c +++ b/ft_memset.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/14 13:13:22 by ljiriste #+# #+# */ -/* Updated: 2023/08/14 13:38:38 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 10:39:20 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,6 @@ void *ft_memset(void *s, int c, size_t n) { while (n > 0) - *((unsigned char *)s + (--n)) = c; + *((unsigned char *)s + (--n)) = (unsigned char)c; return (s); } diff --git a/ft_strchr.c b/ft_strchr.c new file mode 100644 index 0000000..594d6a2 --- /dev/null +++ b/ft_strchr.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:41:10 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:51:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *strchr(const char *s, int c) +{ + while (*s != '\0') + { + if (*s == c) + return ((char *)s); + ++s; + } + return (NULL); +} diff --git a/ft_strdup.c b/ft_strdup.c new file mode 100644 index 0000000..d16d5e0 --- /dev/null +++ b/ft_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/17 13:33:49 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:52:18 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // For the malloc function +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *dest; + size_t s_len; + + s_len = ft_strlen(s); + dest = malloc((s_len + 1) * sizeof(char)); + if (dest) + ft_strlcpy(dest, s, s_len + 1); + return (dest); +} diff --git a/ft_strlcat.c b/ft_strlcat.c index ed6e7b5..1a435b6 100644 --- a/ft_strlcat.c +++ b/ft_strlcat.c @@ -6,14 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/12 17:32:33 by ljiriste #+# #+# */ -/* Updated: 2023/08/14 13:39:39 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 12:55:06 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "libft.h" -size_t ft_strlcat(char *dst, char *src, size_t size) +size_t ft_strlcat(char *dst, const char *src, size_t size) { size_t length; diff --git a/ft_strlcpy.c b/ft_strlcpy.c index 3f1d16c..4b42275 100644 --- a/ft_strlcpy.c +++ b/ft_strlcpy.c @@ -6,14 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/11 17:28:43 by ljiriste #+# #+# */ -/* Updated: 2023/08/14 13:29:38 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 12:55:52 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "libft.h" -size_t ft_strlcpy(char *dst, char *src, size_t size) +size_t ft_strlcpy(char *dst, const char *src, size_t size) { size_t i; diff --git a/ft_strlen.c b/ft_strlen.c index f4ae6c7..b27da53 100644 --- a/ft_strlen.c +++ b/ft_strlen.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/14 13:40:59 by ljiriste #+# #+# */ -/* Updated: 2023/08/14 13:41:35 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 12:05:43 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,5 +20,5 @@ size_t ft_strlen(const char *s) len = 0; while (s[len] != '\0') ++len; - return len; + return (len); } diff --git a/ft_strncmp.c b/ft_strncmp.c new file mode 100644 index 0000000..ef313b3 --- /dev/null +++ b/ft_strncmp.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/12 17:02:07 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:56:20 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0' && i + 1 < n) + ++i; + if (n > 0) + return (s1[i] - s2[i]); + else + return (0); +} diff --git a/ft_strnstr.c b/ft_strnstr.c new file mode 100644 index 0000000..ccb257c --- /dev/null +++ b/ft_strnstr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/12 17:15:34 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:50:26 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // For the NULL pointer +#include +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t little_len; + size_t i; + + little_len = ft_strlen(little); + i = 0; + while (big[i] != '\0' && i + little_len <= len) + { + if (!(ft_strncmp(big + i, little, little_len))) + return ((char *)big + i); + ++i; + } + return (NULL); +} diff --git a/ft_strrchr.c b/ft_strrchr.c new file mode 100644 index 0000000..eeb3d46 --- /dev/null +++ b/ft_strrchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:46:09 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:51:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *strrchr(const char *s, int c) +{ + char *result; + + result = NULL; + while (*s != '\0') + { + if (*s == c) + result = (char *)s; + ++s; + } + return (result); +} diff --git a/ft_tolower.c b/ft_tolower.c new file mode 100644 index 0000000..5e169b0 --- /dev/null +++ b/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:36:54 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:53:38 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (ft_isupper(c)) + c = c + 'a' - 'A'; + return (c); +} diff --git a/ft_toupper.c b/ft_toupper.c new file mode 100644 index 0000000..1e79b9d --- /dev/null +++ b/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:35:21 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:54:28 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (ft_islower(c)) + c = c + 'A' - 'a'; + return (c); +} diff --git a/libft.h b/libft.h new file mode 100644 index 0000000..09b8629 --- /dev/null +++ b/libft.h @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:58:48 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include + +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_islower(int c); +int ft_isprint(int c); +int ft_isspace(int c); +int ft_isupper(int c); +int ft_isascii(int c); + +void *ft_memset(void *s, int c, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +void *ft_calloc(size_t nmemb, size_t size); +void ft_bzero(void *s, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); + +size_t ft_strlen(const char *s); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlcat(char *dst, const char *src, size_t size); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strdup(const char *s); + +int ft_toupper(int c); +int ft_tolower(int c); + +int ft_atoi(const char *nptr); + +#endif