From 5b653ccffdc33c8774697a93cad0b499b88dca71 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 28 Jul 2019 21:12:28 +0200 Subject: std library function almost done, tested with libft-unit-tests --- .gitignore | 3 +++ Makefile | 29 +++++++++++++++++++++++++++++ auteur | 1 + ft_atoi.c | 33 +++++++++++++++++++++++++++++++++ ft_bzero.c | 10 ++++++++++ ft_isalnum.c | 6 ++++++ ft_isalpha.c | 4 ++++ ft_isascii.c | 6 ++++++ ft_isdigit.c | 4 ++++ ft_isprint.c | 4 ++++ ft_memccpy.c | 22 ++++++++++++++++++++++ ft_memchr.c | 17 +++++++++++++++++ ft_memcmp.c | 19 +++++++++++++++++++ ft_memcpy.c | 16 ++++++++++++++++ ft_memmove.c | 38 ++++++++++++++++++++++++++++++++++++++ ft_memset.c | 11 +++++++++++ ft_strcat.c | 17 +++++++++++++++++ ft_strchr.c | 17 +++++++++++++++++ ft_strcmp.c | 9 +++++++++ ft_strcpy.c | 13 +++++++++++++ ft_strdup.c | 22 ++++++++++++++++++++++ ft_strlcat.c | 24 ++++++++++++++++++++++++ ft_strlen.c | 11 +++++++++++ ft_strncat.c | 19 +++++++++++++++++++ ft_strncmp.c | 15 +++++++++++++++ ft_strncpy.c | 16 ++++++++++++++++ ft_strnstr.c | 29 +++++++++++++++++++++++++++++ ft_strrchr.c | 16 ++++++++++++++++ ft_strstr.c | 27 +++++++++++++++++++++++++++ ft_tolower.c | 12 ++++++++++++ ft_toupper.c | 8 ++++++++ libft.h | 34 ++++++++++++++++++++++++++++++++++ 32 files changed, 512 insertions(+) create mode 100644 Makefile create mode 100644 auteur create mode 100644 ft_atoi.c create mode 100644 ft_bzero.c create mode 100644 ft_isalnum.c create mode 100644 ft_isalpha.c create mode 100644 ft_isascii.c create mode 100644 ft_isdigit.c create mode 100644 ft_isprint.c create mode 100644 ft_memccpy.c create mode 100644 ft_memchr.c create mode 100644 ft_memcmp.c create mode 100644 ft_memcpy.c create mode 100644 ft_memmove.c create mode 100644 ft_memset.c create mode 100644 ft_strcat.c create mode 100644 ft_strchr.c create mode 100644 ft_strcmp.c create mode 100644 ft_strcpy.c create mode 100644 ft_strdup.c create mode 100644 ft_strlcat.c create mode 100644 ft_strlen.c create mode 100644 ft_strncat.c create mode 100644 ft_strncmp.c create mode 100644 ft_strncpy.c create mode 100644 ft_strnstr.c create mode 100644 ft_strrchr.c create mode 100644 ft_strstr.c create mode 100644 ft_tolower.c create mode 100644 ft_toupper.c create mode 100644 libft.h diff --git a/.gitignore b/.gitignore index d7756c2..e0a9dfe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ a.out *.o +*.so +*.a +*.pdf diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5389a10 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +CC = gcc +CCFLAGS = -Wall -Wextra -Werror + +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_memccpy.c ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_strcat.c \ + ft_strchr.c ft_strcmp.c ft_strcpy.c ft_strdup.c ft_strlcat.c ft_strlen.c ft_strncat.c \ + ft_strncmp.c ft_strncpy.c ft_strnstr.c ft_strrchr.c ft_strstr.c ft_tolower.c \ + ft_toupper.c +OBJ = $(SRC:.c=.o) + +all: $(NAME) + +$(NAME): $(OBJ) libft.h + ar rc $(NAME) $(OBJ) + +%.o: %.c + $(CC) $(CCFLAGS) -c -o $@ $< + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +so: $(OBJ) libft.h + $(CC) -shared -Wl,-soname,libft.so -o libft.so $(OBJ) diff --git a/auteur b/auteur new file mode 100644 index 0000000..718becd --- /dev/null +++ b/auteur @@ -0,0 +1 @@ +cacharle diff --git a/ft_atoi.c b/ft_atoi.c new file mode 100644 index 0000000..94c42cc --- /dev/null +++ b/ft_atoi.c @@ -0,0 +1,33 @@ +#define MIN_INT (1 << 31) +#define MAX_INT (~(1 << 31)) + +int ft_atoi(const char *nptr) +{ + unsigned int nb; + int i; + int is_negative; + + while (*nptr == ' ' || *nptr == '\t'|| *nptr == '\n' + || *nptr == '\v'|| *nptr == '\f'|| *nptr == '\r') + nptr++; + is_negative = 0; + if (*nptr == '-' || *nptr == '+') + { + if (*nptr == '-') + is_negative = 1; + nptr++; + } + i = 0; + nb = 0; + while (nptr[i] >= '0' && nptr[i] <= '9') + { + if (!is_negative && nb > (unsigned int)MAX_INT) + return (-1); + else if (nb > (unsigned int)MIN_INT) + return (0); + nb *= 10; + nb += nptr[i] - '0'; + i++; + } + return ((int)(is_negative ? -nb : nb)); +} diff --git a/ft_bzero.c b/ft_bzero.c new file mode 100644 index 0000000..e936cf1 --- /dev/null +++ b/ft_bzero.c @@ -0,0 +1,10 @@ +#include + +void ft_bzero(void *s, size_t n) +{ + char *s_char_ptr; + + s_char_ptr = (char*)s; + while (n-- > 0) + s_char_ptr[n] = 0; +} diff --git a/ft_isalnum.c b/ft_isalnum.c new file mode 100644 index 0000000..d98da91 --- /dev/null +++ b/ft_isalnum.c @@ -0,0 +1,6 @@ +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/ft_isalpha.c b/ft_isalpha.c new file mode 100644 index 0000000..cd09abb --- /dev/null +++ b/ft_isalpha.c @@ -0,0 +1,4 @@ +int ft_isalpha(int c) +{ + return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); +} diff --git a/ft_isascii.c b/ft_isascii.c new file mode 100644 index 0000000..7742ded --- /dev/null +++ b/ft_isascii.c @@ -0,0 +1,6 @@ +#define MAX_CHAR ((1 << 7) - 1) + +int ft_isascii(int c) +{ + return (c >= 0 && c <= MAX_CHAR); +} diff --git a/ft_isdigit.c b/ft_isdigit.c new file mode 100644 index 0000000..a41e1fd --- /dev/null +++ b/ft_isdigit.c @@ -0,0 +1,4 @@ +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/ft_isprint.c b/ft_isprint.c new file mode 100644 index 0000000..6834120 --- /dev/null +++ b/ft_isprint.c @@ -0,0 +1,4 @@ +int ft_isprint(int c) +{ + return (c >= ' ' && c <= '~'); +} diff --git a/ft_memccpy.c b/ft_memccpy.c new file mode 100644 index 0000000..9cc483a --- /dev/null +++ b/ft_memccpy.c @@ -0,0 +1,22 @@ +#include + +void *ft_memccpy(void *dest, const void *src, int c, size_t n) +{ + size_t i; + unsigned char *uc_dest; + unsigned char *uc_src; + + uc_dest = (unsigned char*)dest; + uc_src = (unsigned char*)src; + i = 0; + while (i < n) + { + uc_dest[i] = uc_src[i]; + if (uc_dest[i] == (unsigned char)c) + break ; + i++; + } + if (i == n) + return (NULL); + return (dest + i + 1); +} diff --git a/ft_memchr.c b/ft_memchr.c new file mode 100644 index 0000000..662a6c7 --- /dev/null +++ b/ft_memchr.c @@ -0,0 +1,17 @@ +#include + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + unsigned char *uc_s; + + uc_s = (unsigned char*)s; + i = 0; + while (i < n) + { + if (uc_s[i] == (unsigned char)c) + return (uc_s + i); + i++; + } + return (NULL); +} diff --git a/ft_memcmp.c b/ft_memcmp.c new file mode 100644 index 0000000..76cd47d --- /dev/null +++ b/ft_memcmp.c @@ -0,0 +1,19 @@ +#include + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + unsigned char *uc_s1; + unsigned char *uc_s2; + + uc_s1 = (unsigned char*)s1; + uc_s2 = (unsigned char*)s2; + i = 0; + if (n == 0) + return (0); + while (i < n && uc_s1[i] == uc_s2[i]) + i++; + if (i == n) + i--; + return (uc_s1[i] - uc_s2[i]); +} diff --git a/ft_memcpy.c b/ft_memcpy.c new file mode 100644 index 0000000..1ac2708 --- /dev/null +++ b/ft_memcpy.c @@ -0,0 +1,16 @@ +#include + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + + if (dest == NULL && src == NULL) + return (NULL); + i = 0; + while (i < n) + { + *((char*)dest + i) = *((char*)src + i); + i++; + } + return (dest); +} diff --git a/ft_memmove.c b/ft_memmove.c new file mode 100644 index 0000000..54555cb --- /dev/null +++ b/ft_memmove.c @@ -0,0 +1,38 @@ +#include +#include + +#define BUF_SIZE 4096 + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + size_t i; + size_t j; + size_t k; + unsigned char tmp[BUF_SIZE]; + unsigned char *uc_dest; + unsigned char *uc_src; + + if (dest == NULL && src == NULL) + return (NULL); + uc_dest = (unsigned char*) dest; + uc_src = (unsigned char*) src; + i = 0; + while (i < n) + { + j = 0; + while (j < BUF_SIZE && i < n) + { + tmp[j] = uc_src[i]; + j++; + i++; + } + k = 0; + while (k < j) + { + uc_dest[k] = tmp[k]; + k++; + } + i++; + } + return (dest); +} diff --git a/ft_memset.c b/ft_memset.c new file mode 100644 index 0000000..e09db6c --- /dev/null +++ b/ft_memset.c @@ -0,0 +1,11 @@ +#include + +void *ft_memset(void *s, int c, size_t n) +{ + unsigned char uchar_c; + + uchar_c = (unsigned char)c; + while (n-- > 0) + ((unsigned char*)s)[n] = uchar_c; + return (s); +} diff --git a/ft_strcat.c b/ft_strcat.c new file mode 100644 index 0000000..5f03164 --- /dev/null +++ b/ft_strcat.c @@ -0,0 +1,17 @@ +char *ft_strcat(char *dest, const char *src) +{ + int i; + int j; + + i = 0; + while (dest[i]) + i++; + j = 0; + while (src[j]) + { + dest[i + j] = src[j]; + j++; + } + dest[i + j] = '\0'; + return (dest); +} diff --git a/ft_strchr.c b/ft_strchr.c new file mode 100644 index 0000000..67b969e --- /dev/null +++ b/ft_strchr.c @@ -0,0 +1,17 @@ +#include + +char *ft_strchr(const char *s, int c) +{ + char *cursor; + + cursor = (char*)s; + while (*cursor) + { + if (*cursor == (char)c) + return (cursor); + cursor++; + } + if (c == 0) + return (cursor); + return (NULL); +} diff --git a/ft_strcmp.c b/ft_strcmp.c new file mode 100644 index 0000000..8a86b2a --- /dev/null +++ b/ft_strcmp.c @@ -0,0 +1,9 @@ +int ft_strcmp(const char *s1, const char *s2) +{ + int i; + + i = 0; + while (s1[i] && s2[i] && s1[i] == s2[i]) + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/ft_strcpy.c b/ft_strcpy.c new file mode 100644 index 0000000..8f3946d --- /dev/null +++ b/ft_strcpy.c @@ -0,0 +1,13 @@ +char *ft_strcpy(char *dest, const char *src) +{ + int i; + + i = 0; + while (src[i]) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (dest); +} diff --git a/ft_strdup.c b/ft_strdup.c new file mode 100644 index 0000000..e3609cc --- /dev/null +++ b/ft_strdup.c @@ -0,0 +1,22 @@ +#include +#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) + return (NULL); + i = 0; + while (i < len) + { + clone[i] = s[i]; + i++; + } + clone[i] = '\0'; + return (clone); +} + diff --git a/ft_strlcat.c b/ft_strlcat.c new file mode 100644 index 0000000..6c550f3 --- /dev/null +++ b/ft_strlcat.c @@ -0,0 +1,24 @@ +#include +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t i; + size_t dst_len; + size_t src_len; + + dst_len = ft_strlen(dst); + src_len = ft_strlen(src); + if (size == 0) + return (src_len); + i = dst_len; + while (i < size - 1) + { + dst[i] = src[i - dst_len]; + i++; + } + if (dst[size - 1] != '\0') + return (src_len + size); + dst[size - 1] = '\0'; + return (dst_len + src_len); +} diff --git a/ft_strlen.c b/ft_strlen.c new file mode 100644 index 0000000..9568fa2 --- /dev/null +++ b/ft_strlen.c @@ -0,0 +1,11 @@ +#include + +size_t ft_strlen(const char *s) +{ + size_t counter; + + counter = 0; + while (s[counter]) + counter++; + return (counter); +} diff --git a/ft_strncat.c b/ft_strncat.c new file mode 100644 index 0000000..41e9f67 --- /dev/null +++ b/ft_strncat.c @@ -0,0 +1,19 @@ +#include + +char *ft_strncat(char *dest, const char *src, size_t n) +{ + size_t i; + size_t j; + + i = 0; + while (dest[i]) + i++; + j = 0; + while (j < n && src[j]) + { + dest[i + j] = src[j]; + j++; + } + dest[i + j] = '\0'; + return (dest); +} diff --git a/ft_strncmp.c b/ft_strncmp.c new file mode 100644 index 0000000..0f24a1e --- /dev/null +++ b/ft_strncmp.c @@ -0,0 +1,15 @@ +#include + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + if (n == 0) + return (0); + i = 0; + while (s1[i] == s2[i] && s1[i] && s2[i] && i < n) + i++; + if (i == n) + i--; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/ft_strncpy.c b/ft_strncpy.c new file mode 100644 index 0000000..100c813 --- /dev/null +++ b/ft_strncpy.c @@ -0,0 +1,16 @@ +#include + +char *ft_strncpy(char *dest, const char *src, size_t n) +{ + size_t i; + + i = 0; + while (src[i] && i < n) + { + dest[i] = src[i]; + i++; + } + while (i < n) + dest[i++] = '\0'; + return (dest); +} diff --git a/ft_strnstr.c b/ft_strnstr.c new file mode 100644 index 0000000..2308a57 --- /dev/null +++ b/ft_strnstr.c @@ -0,0 +1,29 @@ +#include +#include +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t j; + size_t min_len; + + min_len = len > ft_strlen(little) ? ft_strlen(little) : len; + if (min_len == 0) + return ((char*)big); + i = 0; + while (big[i]) + { + j = 0; + while (little[j] && big[i + j]) + { + if (little[j] != big[i + j]) + break ; + j++; + if (j == min_len) + return ((char*)(big + i)); + } + i++; + } + return (NULL); +} diff --git a/ft_strrchr.c b/ft_strrchr.c new file mode 100644 index 0000000..7f01501 --- /dev/null +++ b/ft_strrchr.c @@ -0,0 +1,16 @@ +#include +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + size_t i; + + i = ft_strlen(s); + while (s[i] != (char)c) + { + if (i == 0) + return (NULL); + i--; + } + return ((char*)s + i); +} diff --git a/ft_strstr.c b/ft_strstr.c new file mode 100644 index 0000000..0209de4 --- /dev/null +++ b/ft_strstr.c @@ -0,0 +1,27 @@ +#include +#include +#include "libft.h" + +char *ft_strstr(const char *haystack, const char *needle) +{ + size_t i; + char *cursor; + + cursor = (char*)haystack; + if (!ft_strlen(needle)) + return (cursor); + while (*cursor) + { + i = 0; + while (needle[i] && cursor[i]) + { + if (needle[i] != cursor[i]) + break ; + i++; + } + if (i == ft_strlen(needle)) + return (cursor); + cursor++; + } + return (NULL); +} diff --git a/ft_tolower.c b/ft_tolower.c new file mode 100644 index 0000000..5b5ee01 --- /dev/null +++ b/ft_tolower.c @@ -0,0 +1,12 @@ +int ft_tolower(int c) +{ + unsigned char uc; + + uc = c; + /* if (c < -1) */ + /* return (c + 256); */ + + if (uc >= 'A' && uc <= 'Z') + return (uc - 'A' + 'a'); + return (c); +} diff --git a/ft_toupper.c b/ft_toupper.c new file mode 100644 index 0000000..7e85f8d --- /dev/null +++ b/ft_toupper.c @@ -0,0 +1,8 @@ +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c - 'a' + 'A'); + return (c); +} diff --git a/libft.h b/libft.h new file mode 100644 index 0000000..0b110e9 --- /dev/null +++ b/libft.h @@ -0,0 +1,34 @@ +#ifndef LIBFT_H +# define LIBFT_H + +# include + +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_memccpy(void *dest, const void *src, int c, 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); +int ft_memcmp(const void *s1, const void *s2, size_t n); +size_t ft_strlen(const char *s); +char *ft_strdup(const char *s); +char *ft_strcpy(char *dest, const char *src); +char *ft_strncpy(char *dest, const char *src, size_t n); +char *ft_strcat(char *dest, const char *src); +char *ft_strncat(char *dest, const char *src, size_t n); +size_t ft_strlcat(char *dst, const char *src, size_t size); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strstr(const char *haystack, const char *needle); +char *ft_strnstr(const char *big, const char *little, size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +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); + +#endif -- cgit