From 6428ed06b0fff66b84e2d1939823b10a3e9db8b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Mon, 5 May 2025 18:24:21 +0200 Subject: [PATCH] feat: initial version --- hdrs/libft.h | 21 +++++++++++++++++++++ srcs/ft_atoi.c | 29 +++++++++++++++++++++++++++++ srcs/ft_bzero.c | 6 ++++++ srcs/ft_isalpha.c | 7 +++++++ srcs/ft_isdigit.c | 8 ++++++++ srcs/ft_islower.c | 8 ++++++++ srcs/ft_isupper.c | 8 ++++++++ srcs/ft_memcpy.c | 12 ++++++++++++ srcs/ft_memcpy32.c | 25 +++++++++++++++++++++++++ srcs/ft_memcpy64.c | 25 +++++++++++++++++++++++++ srcs/ft_memset.c | 9 +++++++++ srcs/ft_memset32.c | 37 +++++++++++++++++++++++++++++++++++++ srcs/ft_memset64.c | 38 ++++++++++++++++++++++++++++++++++++++ srcs/ft_strlen.c | 14 ++++++++++++++ srcs/ft_tolower.c | 8 ++++++++ srcs/ft_toupper.c | 8 ++++++++ 16 files changed, 263 insertions(+) create mode 100644 hdrs/libft.h create mode 100644 srcs/ft_atoi.c create mode 100644 srcs/ft_bzero.c create mode 100644 srcs/ft_isalpha.c create mode 100644 srcs/ft_isdigit.c create mode 100644 srcs/ft_islower.c create mode 100644 srcs/ft_isupper.c create mode 100644 srcs/ft_memcpy.c create mode 100644 srcs/ft_memcpy32.c create mode 100644 srcs/ft_memcpy64.c create mode 100644 srcs/ft_memset.c create mode 100644 srcs/ft_memset32.c create mode 100644 srcs/ft_memset64.c create mode 100644 srcs/ft_strlen.c create mode 100644 srcs/ft_tolower.c create mode 100644 srcs/ft_toupper.c diff --git a/hdrs/libft.h b/hdrs/libft.h new file mode 100644 index 0000000..79effd4 --- /dev/null +++ b/hdrs/libft.h @@ -0,0 +1,21 @@ +#ifndef LIBFT_H +# define LIBFT_H + +# include + +int ft_islower(int c); +int ft_isupper(int c); +int ft_tolower(int c); +int ft_toupper(int c); +int ft_isdigit(int c); +int ft_isalpha(int c); +void ft_bzero(void *s, size_t n); +void *ft_memset(void *s, int c, size_t n); +void *ft_memset32(void *s, int c, size_t n); +void *ft_memset64(void *s, int c, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memcpy32(void *dst, const void *src, size_t n); +void *ft_memcpy64(void *dst, const void *src, size_t n); +int ft_atoi(const char *str); + +#endif diff --git a/srcs/ft_atoi.c b/srcs/ft_atoi.c new file mode 100644 index 0000000..7aeb76f --- /dev/null +++ b/srcs/ft_atoi.c @@ -0,0 +1,29 @@ +#include "libft.h" + +int ft_atoi(const char *str) +{ + int i = 0; + int r = 0; + int s = 1; + + while (str[i] == ' ' || (str[i] >= '\t' && str[i] <= '\r')) + i++; + + if (str[i] == '-') + { + s = -1; + i++; + } + else if (str[i] == '+') + i++; + + while(str[i] != '\0') + { + if (str[i] >= '0' && str[i] <= '9') + r = r * 10 + str[i] - '0'; + else + break; + i++; + } + return (s * r); +} \ No newline at end of file diff --git a/srcs/ft_bzero.c b/srcs/ft_bzero.c new file mode 100644 index 0000000..18c1ba7 --- /dev/null +++ b/srcs/ft_bzero.c @@ -0,0 +1,6 @@ +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, '\0', n); +} diff --git a/srcs/ft_isalpha.c b/srcs/ft_isalpha.c new file mode 100644 index 0000000..225dd81 --- /dev/null +++ b/srcs/ft_isalpha.c @@ -0,0 +1,7 @@ +int ft_isalpha(int c) +{ + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return (1); + else + return (0); +} diff --git a/srcs/ft_isdigit.c b/srcs/ft_isdigit.c new file mode 100644 index 0000000..bca0a5e --- /dev/null +++ b/srcs/ft_isdigit.c @@ -0,0 +1,8 @@ +#include "libft.h" + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} \ No newline at end of file diff --git a/srcs/ft_islower.c b/srcs/ft_islower.c new file mode 100644 index 0000000..cbb4920 --- /dev/null +++ b/srcs/ft_islower.c @@ -0,0 +1,8 @@ +#include "libft.h" + +int ft_islower(int c) +{ + if (c >= 'a' && c <= 'z') + return (1); + return (0); +} \ No newline at end of file diff --git a/srcs/ft_isupper.c b/srcs/ft_isupper.c new file mode 100644 index 0000000..34677a8 --- /dev/null +++ b/srcs/ft_isupper.c @@ -0,0 +1,8 @@ +#include "libft.h" + +int ft_isupper(int c) +{ + if (c >= 'A' && c <= 'Z') + return (1); + return (0); +} \ No newline at end of file diff --git a/srcs/ft_memcpy.c b/srcs/ft_memcpy.c new file mode 100644 index 0000000..2317791 --- /dev/null +++ b/srcs/ft_memcpy.c @@ -0,0 +1,12 @@ +#include + +void *ft_memcpy(void *dst, const void *src, size_t n) { + unsigned char *d = (unsigned char *)dst; + const unsigned char *s = (const unsigned char *)src; + + while (n--) { + *d++ = *s++; + } + + return dst; +} diff --git a/srcs/ft_memcpy32.c b/srcs/ft_memcpy32.c new file mode 100644 index 0000000..4317188 --- /dev/null +++ b/srcs/ft_memcpy32.c @@ -0,0 +1,25 @@ +#include +#include + +void *ft_memcpy32(void *dst, const void *src, size_t n) { + size_t i; + uint32_t *lp_dst; + const uint32_t *lp_src; + unsigned char *bp_dst; + const unsigned char *bp_src; + + lp_dst = (uint32_t *)dst; + lp_src = (const uint32_t *)src; + + for (i = 0; i < n / 4; i++) { + lp_dst[i] = lp_src[i]; + } + + bp_dst = (unsigned char *)(lp_dst + i); + bp_src = (const unsigned char *)(lp_src + i); + for (i = 0; i < n % 4; i++) { + bp_dst[i] = bp_src[i]; + } + + return dst; +} diff --git a/srcs/ft_memcpy64.c b/srcs/ft_memcpy64.c new file mode 100644 index 0000000..d3150ee --- /dev/null +++ b/srcs/ft_memcpy64.c @@ -0,0 +1,25 @@ +#include +#include + +void *ft_memcpy64(void *dst, const void *src, size_t n) { + size_t i; + uint64_t *lp_dst; + const uint64_t *lp_src; + unsigned char *bp_dst; + const unsigned char *bp_src; + + lp_dst = (uint64_t *)dst; + lp_src = (const uint64_t *)src; + + for (i = 0; i < n / 8; i++) { + lp_dst[i] = lp_src[i]; + } + + bp_dst = (unsigned char *)(lp_dst + i); + bp_src = (const unsigned char *)(lp_src + i); + for (i = 0; i < n % 8; i++) { + bp_dst[i] = bp_src[i]; + } + + return dst; +} diff --git a/srcs/ft_memset.c b/srcs/ft_memset.c new file mode 100644 index 0000000..58aeefc --- /dev/null +++ b/srcs/ft_memset.c @@ -0,0 +1,9 @@ +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + unsigned char *p = (unsigned char *)s; + while (n--) + *p++ = (unsigned char)c; + return s; +} diff --git a/srcs/ft_memset32.c b/srcs/ft_memset32.c new file mode 100644 index 0000000..b156514 --- /dev/null +++ b/srcs/ft_memset32.c @@ -0,0 +1,37 @@ +#include +#include + +void *ft_memset32(void *s, int c, size_t n) +{ + unsigned char *ptr = (unsigned char *)s; + uint32_t pattern; + uint32_t *long_ptr; + + unsigned char byte = (unsigned char)c; + + while (n > 0 && ((uintptr_t)ptr % 4 != 0)) + { + *ptr++ = byte; + n--; + } + + if (n >= 4) + { + pattern = byte; + pattern |= pattern << 8; + pattern |= pattern << 16; + + long_ptr = (uint32_t *)ptr; + while (n >= 4) + { + *long_ptr++ = pattern; + n -= 4; + } + ptr = (unsigned char *)long_ptr; + } + + while (n--) + *ptr++ = byte; + + return s; +} diff --git a/srcs/ft_memset64.c b/srcs/ft_memset64.c new file mode 100644 index 0000000..9417c09 --- /dev/null +++ b/srcs/ft_memset64.c @@ -0,0 +1,38 @@ +#include +#include + +void *ft_memset64(void *s, int c, size_t n) +{ + unsigned char *ptr = (unsigned char *)s; + uint64_t pattern; + uint64_t *long_ptr; + + unsigned char byte = (unsigned char)c; + + while (n > 0 && ((uintptr_t)ptr % 8 != 0)) + { + *ptr++ = byte; + n--; + } + + if (n >= 8) + { + pattern = byte; + pattern |= pattern << 8; + pattern |= pattern << 16; + pattern |= pattern << 32; + + long_ptr = (uint64_t *)ptr; + while (n >= 8) + { + *long_ptr++ = pattern; + n -= 8; + } + ptr = (unsigned char *)long_ptr; + } + + while (n--) + *ptr++ = byte; + + return s; +} diff --git a/srcs/ft_strlen.c b/srcs/ft_strlen.c new file mode 100644 index 0000000..7846d77 --- /dev/null +++ b/srcs/ft_strlen.c @@ -0,0 +1,14 @@ +#include + +size_t ft_strlen(const char *s) +{ + const char *p = s; + + if (!s) + return 0; + + while (*p) + ++p; + + return (size_t)(p - s); +} diff --git a/srcs/ft_tolower.c b/srcs/ft_tolower.c new file mode 100644 index 0000000..33fd0e9 --- /dev/null +++ b/srcs/ft_tolower.c @@ -0,0 +1,8 @@ +#include "libft.h" + +int ft_tolower(int c) +{ + if (ft_isupper(c)) + c = c + 32; + return (c); +} \ No newline at end of file diff --git a/srcs/ft_toupper.c b/srcs/ft_toupper.c new file mode 100644 index 0000000..2d53ff2 --- /dev/null +++ b/srcs/ft_toupper.c @@ -0,0 +1,8 @@ +#include "libft.h" + +int ft_toupper(int c) +{ + if (ft_islower(c)) + c = c - 32; + return (c); +} \ No newline at end of file