This commit is contained in:
rzy
2026-08-31 02:16:33 +02:00
commit 94cd5aac51
25 changed files with 705 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
CC := cc
CFLAGS := -Wall -Wextra -Werror
SRCS := ft_isalpha.c \
ft_isdigit.c \
ft_isalnum.c \
ft_isascii.c \
ft_isprint.c \
ft_strlen.c \
ft_memset.c \
ft_bzero.c \
ft_memcpy.c \
ft_memmove.c \
ft_strlcpy.c \
ft_strlcat.c \
ft_toupper.c \
ft_tolower.c \
ft_strchr.c \
ft_strrchr.c \
ft_strncmp.c \
ft_memchr.c \
ft_memcmp.c \
ft_strnstr.c \
ft_atoi.c \
ft_calloc.c \
ft_strdup.c
OBJS := $(SRCS:.c=.o)
NAME := libft.a
all: $(NAME)
$(NAME): $(OBJS)
ar -crs $(NAME) $(OBJS)
%.o: %.c
$(CC) $(CFLAGS) -c $^ -o $@
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re
+42
View File
@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:34:29 by rzy #+# #+# */
/* Updated: 2026/08/23 23:37:29 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char c)
{
return (c == ' ' || (c >= '\t' && c <= '\r'));
}
int ft_atoi(const char *nptr)
{
int result;
int sign;
int i;
result = 0;
sign = 1;
i = 0;
while (ft_isspace(nptr[i]))
++i;
if (nptr[i] == '+' || nptr[i] == '-')
{
if (nptr[i] == '-')
sign = -1;
++i;
}
while (ft_isdigit(nptr[i]))
{
result = (result * 10) + (nptr[i] - '0');
++i;
}
return (result * sign);
}
+26
View File
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:35:16 by rzy #+# #+# */
/* Updated: 2026/08/23 22:38:02 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
unsigned char *ptr;
size_t i;
ptr = (unsigned char *)s;
i = 0;
while (i < n)
{
ptr[i] = 0;
++i;
}
}
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/24 01:03:26 by rzy #+# #+# */
/* Updated: 2026/08/24 01:03:38 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t n, size_t size)
{
unsigned char *ptr;
size_t i;
ptr = (unsigned char *)malloc(n * size);
if (ptr == NULL)
return (NULL);
i = 0;
while (i < (n * size))
{
ptr[i] = 0;
++i;
}
return (ptr);
}
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:22:49 by rzy #+# #+# */
/* Updated: 2026/08/23 22:23:32 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:20:02 by rzy #+# #+# */
/* Updated: 2026/08/23 22:20:41 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}
+17
View File
@@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:24:58 by rzy #+# #+# */
/* Updated: 2026/08/23 22:26:30 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
return (c >= 0 && c <= 127);
}
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:20:52 by rzy #+# #+# */
/* Updated: 2026/08/23 22:22:21 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}
+17
View File
@@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:26:41 by rzy #+# #+# */
/* Updated: 2026/08/23 22:27:43 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
return (c >= 32 && c <= 126);
}
+28
View File
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:21:11 by rzy #+# #+# */
/* Updated: 2026/08/23 23:21:27 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
unsigned char *ptr;
size_t i;
ptr = (unsigned char *)s;
i = 0;
while (i < n)
{
if (ptr[i] == (unsigned char)c)
return ((void *)s + i);
++i;
}
return (NULL);
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:24:25 by rzy #+# #+# */
/* Updated: 2026/08/23 23:26:32 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *s1_ptr;
unsigned char *s2_ptr;
size_t i;
s1_ptr = (unsigned char *)s1;
s2_ptr = (unsigned char *)s2;
i = 0;
while (i < n)
{
if (s1_ptr[i] != s2_ptr[i])
return (s1_ptr[i] - s2_ptr[i]);
++i;
}
return (0);
}
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:39:05 by rzy #+# #+# */
/* Updated: 2026/08/23 22:42:56 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
unsigned char *dest_ptr;
unsigned char *src_ptr;
size_t i;
dest_ptr = (unsigned char *)dest;
src_ptr = (unsigned char *)src;
i = 0;
while (i < n)
{
dest_ptr[i] = src_ptr[i];
++i;
}
return (dest);
}
+41
View File
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:44:14 by rzy #+# #+# */
/* Updated: 2026/08/23 22:56:01 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
unsigned char *dest_ptr;
unsigned char *src_ptr;
int i;
dest_ptr = (unsigned char *)dest;
src_ptr = (unsigned char *)src;
if (src > dest)
{
i = 0;
while ((size_t)i < n)
{
dest_ptr[i] = src_ptr[i];
++i;
}
}
else
{
i = n - 1;
while (i >= 0)
{
dest_ptr[i] = src_ptr[i];
--i;
}
}
return (dest);
}
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:29:57 by rzy #+# #+# */
/* Updated: 2026/08/23 22:31:46 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
unsigned char *ptr;
size_t i;
ptr = (unsigned char *)s;
i = 0;
while (i < n)
{
ptr[i] = (unsigned char)c;
++i;
}
return (s);
}
+28
View File
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:12:35 by rzy #+# #+# */
/* Updated: 2026/08/23 23:15:51 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == (char)c)
return ((char *)&s[i]);
++i;
}
if (s[i] == (char)c)
return ((char *)&s[i]);
return (NULL);
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/24 01:09:08 by rzy #+# #+# */
/* Updated: 2026/08/24 01:09:25 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s)
{
int i;
char *dup;
dup = malloc(sizeof (char) * (ft_strlen(s) + 1));
if (dup == NULL)
return (NULL);
i = 0;
while (s[i])
{
dup[i] = s[i];
++i;
}
dup[i] = 0;
return (dup);
}
+34
View File
@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:01:11 by rzy #+# #+# */
/* Updated: 2026/08/23 23:04:11 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t dest_len;
size_t src_len;
dest_len = ft_strlen(dest);
src_len = ft_strlen(src);
if (dest_len >= size)
return (src_len + size);
if (src_len < size - dest_len)
{
ft_memcpy(dest + dest_len, src, src_len);
dest[dest_len + src_len] = 0;
}
else
{
ft_memcpy(dest + dest_len, src, size - dest_len - 1);
dest[size - 1] = 0;
}
return (dest_len + src_len);
}
+28
View File
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:56:51 by rzy #+# #+# */
/* Updated: 2026/08/23 23:00:02 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t i;
i = 0;
if (size == 0)
return (ft_strlen(src));
while (src[i] && i < size - 1)
{
dest[i] = src[i];
++i;
}
dest[i] = 0;
return (ft_strlen(src));
}
+22
View File
@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 22:28:12 by rzy #+# #+# */
/* Updated: 2026/08/23 22:28:51 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
++i;
return (i);
}
+32
View File
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:18:37 by rzy #+# #+# */
/* Updated: 2026/08/23 23:20:35 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
unsigned char *s1_ptr;
unsigned char *s2_ptr;
size_t i;
s1_ptr = (unsigned char *)s1;
s2_ptr = (unsigned char *)s2;
i = 0;
while (i < n)
{
if (!s1_ptr[i] && !s2_ptr[i])
return (0);
if (s1_ptr[i] != s2_ptr[i])
return (s1_ptr[i] - s2_ptr[i]);
++i;
}
return (0);
}
+32
View File
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:27:28 by rzy #+# #+# */
/* Updated: 2026/08/23 23:33:18 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t n)
{
size_t i;
size_t j;
if (!needle[0])
return ((char *)(haystack));
i = 0;
while (i < n && haystack[i])
{
j = 0;
while (i + j < n && needle[j] && haystack[i + j] == needle[j])
++j;
if (!needle[j])
return ((char *)&haystack[i]);
++i;
}
return (NULL);
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:16:26 by rzy #+# #+# */
/* Updated: 2026/08/23 23:17:56 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int i;
char *occ;
i = 0;
occ = NULL;
while (s[i])
{
if (s[i] == (char)c)
occ = (char *)&s[i];
++i;
}
if (s[i] == (char)c)
occ = (char *)&s[i];
return (occ);
}
+19
View File
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:11:15 by rzy #+# #+# */
/* Updated: 2026/08/23 23:11:34 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
return (c + 32);
return (c);
}
+19
View File
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 23:05:53 by rzy #+# #+# */
/* Updated: 2026/08/23 23:11:10 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
return (c - 32);
return (c);
}
+43
View File
@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rzy <ry@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/08/23 19:15:48 by rzy #+# #+# */
/* Updated: 2026/08/23 19:16:01 by rzy ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stddef.h>
# include <stdlib.h>
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);
size_t ft_strlen(const char *s);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, 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);
size_t ft_strlcpy(char *dest, const char *src, size_t size);
size_t ft_strlcat(char *dest, const char *src, size_t size);
int ft_toupper(int c);
int ft_tolower(int c);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
char *ft_strnstr(const char *haystack, const char *needle, size_t n);
int ft_atoi(const char *nptr);
void *ft_calloc(size_t n, size_t size);
char *ft_strdup(const char *s);
#endif