From 83fc28bc3292ec391f4f55b261b79e4499aaffff Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 15 Aug 2023 17:15:58 +0200 Subject: [PATCH] Implemented functions of Part 2 and repaired older functions. Everything passes norminette, compiles and links, but there are issues to be resolved. --- ft_atoi.c | 4 +-- ft_calloc.c | 4 +-- ft_isascii.c | 6 ++-- ft_isspace.c | 4 +-- ft_itoa.c | 57 +++++++++++++++++++++++++++++ ft_memchr.c | 4 +-- ft_putchar_fd.c | 20 +++++++++++ ft_putendl_fd.c | 21 +++++++++++ ft_putnbr_fd.c | 20 +++++++++++ ft_putstr_fd.c | 20 +++++++++++ ft_split.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ ft_strchr.c | 6 ++-- ft_striteri.c | 26 ++++++++++++++ ft_strjoin.c | 29 +++++++++++++++ ft_strmapi.c | 32 +++++++++++++++++ ft_strrchr.c | 6 ++-- ft_strtrim.c | 35 ++++++++++++++++++ ft_substr.c | 29 +++++++++++++++ libft.h | 17 ++++++++- 19 files changed, 421 insertions(+), 15 deletions(-) create mode 100644 ft_itoa.c create mode 100644 ft_putchar_fd.c create mode 100644 ft_putendl_fd.c create mode 100644 ft_putnbr_fd.c create mode 100644 ft_putstr_fd.c create mode 100644 ft_split.c create mode 100644 ft_striteri.c create mode 100644 ft_strjoin.c create mode 100644 ft_strmapi.c create mode 100644 ft_strtrim.c create mode 100644 ft_substr.c diff --git a/ft_atoi.c b/ft_atoi.c index 59dd217..881b763 100644 --- a/ft_atoi.c +++ b/ft_atoi.c @@ -6,13 +6,13 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/13 13:44:39 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:45:44 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 17:11:47 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int atoi(const char *nptr) +int ft_atoi(const char *nptr) { int res; int sign; diff --git a/ft_calloc.c b/ft_calloc.c index b810d7e..d7fe1f3 100644 --- a/ft_calloc.c +++ b/ft_calloc.c @@ -6,14 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 11:34:52 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:54:11 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 17:12:08 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include // for malloc #include "libft.h" -void *calloc(size_t nmemb, size_t size) +void *ft_calloc(size_t nmemb, size_t size) { size_t total_bytes; void *res; diff --git a/ft_isascii.c b/ft_isascii.c index 43682c0..377081b 100644 --- a/ft_isascii.c +++ b/ft_isascii.c @@ -6,11 +6,13 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:19:40 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:20:08 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 17:10:02 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -int is_ascii(int c) +#include "libft.h" + +int ft_isascii(int c) { return (0x00 <= c && c <= 0x7F); } diff --git a/ft_isspace.c b/ft_isspace.c index cbf9b09..23f6070 100644 --- a/ft_isspace.c +++ b/ft_isspace.c @@ -6,13 +6,13 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 11:29:54 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 11:30:29 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 17:12:30 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int is_space(int c) +int ft_isspace(int c) { return (c == ' ' || ('\t' <= c && c <= '\r')); } diff --git a/ft_itoa.c b/ft_itoa.c new file mode 100644 index 0000000..b47047c --- /dev/null +++ b/ft_itoa.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:58:10 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 17:12:42 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static unsigned int size_needed(int n) +{ + unsigned int res; + + res = 1; + if (n == 0) + return (1); + if (n < 0) + ++res; + while (n != 0) + { + ++res; + n /= 10; + } + return (res); +} + +char *ft_itoa(int n) +{ + unsigned int size; + char *res; + + size = size_needed(n); + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + res[--size] = '\0'; + if (n == 0 && res) + res[0] = '0'; + else if (n < 0 && res) + { + res[0] = '-'; + res[--size] = '0' - (n % 10); + n = -(n / 10); + } + while (n > 0 && res) + { + res[--size] = '0' + (n % 10); + n /= 10; + } + return (res); +} diff --git a/ft_memchr.c b/ft_memchr.c index 3a4a944..9be9e52 100644 --- a/ft_memchr.c +++ b/ft_memchr.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 10:56:53 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 11:01:34 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 17:07:39 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ #include #include "libft.h" -void *memchr(const void *s, int c, size_t n) +void *ft_memchr(const void *s, int c, size_t n) { size_t i; diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c new file mode 100644 index 0000000..cfa4476 --- /dev/null +++ b/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:20:18 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:38:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); + return ; +} diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c new file mode 100644 index 0000000..cb811fa --- /dev/null +++ b/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:25:41 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:38:32 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + write(fd, "\n", 1); + return ; +} diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c new file mode 100644 index 0000000..fb9d62b --- /dev/null +++ b/ft_putnbr_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:27:25 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:38:56 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + ft_putstr_fd(ft_itoa(n), fd); + return ; +} diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c new file mode 100644 index 0000000..5f3fb14 --- /dev/null +++ b/ft_putstr_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:23:36 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:39:00 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + write(fd, s, ft_strlen(s)); + return ; +} diff --git a/ft_split.c b/ft_split.c new file mode 100644 index 0000000..673b9d5 --- /dev/null +++ b/ft_split.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:30:26 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 17:02:33 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +/* If str_p points (double deref.) to character that is equal to c, + this function advances it to first until it is not equal to c. + Otherwise it advances the pointer to first character, that equals c. + Function returns when encountering terminating null. */ +static void advance(const char **str_p, char c) +{ + if (**str_p == c) + while (**str_p == c && **str_p) + ++(*str_p); + else + while (**str_p != c && **str_p) + ++(*str_p); + return ; +} + +static void free_str_array(char **arr) +{ + size_t i; + + i = 0; + while (arr[i] != NULL) + free(arr[i++]); + free(arr); + return ; +} + +static size_t word_count(const char *s, char c) +{ + size_t count; + + count = 0; + while (*s) + { + while (*s == c) + ++s; + if (*s) + ++count; + while (*s != c && *s) + ++s; + } + return (count); +} + +static size_t strlen_to_char(const char *s, char c) +{ + size_t len; + + len = 0; + while (s[len] != c && s[len]) + ++len; + return (len); +} + +char **ft_split(const char *s, char c) +{ + char **res; + size_t size; + size_t i; + + res = malloc((word_count(s, c) + 1) * sizeof(char *)); + if (res == NULL) + return (res); + i = 0; + while (*s) + { + advance(&s, c); + if (*s == '\0') + break ; + size = (strlen_to_char(s, c) + 1); + res[i] = malloc(size * sizeof(char)); + if (res[i] == NULL) + { + free_str_array(res); + return (NULL); + } + ft_strlcpy(res[i++], s, size); + advance(&s, c); + } + res[i] = NULL; + return (res); +} diff --git a/ft_strchr.c b/ft_strchr.c index 594d6a2..65c51a2 100644 --- a/ft_strchr.c +++ b/ft_strchr.c @@ -6,14 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 10:41:10 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:51:36 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 17:10:35 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "libft.h" -char *strchr(const char *s, int c) +char *ft_strchr(const char *s, int c) { while (*s != '\0') { @@ -21,5 +21,7 @@ char *strchr(const char *s, int c) return ((char *)s); ++s; } + if (c == '\0') + return ((char *)s); return (NULL); } diff --git a/ft_striteri.c b/ft_striteri.c new file mode 100644 index 0000000..606ce6b --- /dev/null +++ b/ft_striteri.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:18:09 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:47:35 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + unsigned int i; + + i = 0; + while (s[i]) + { + f(i, s + i); + ++i; + } + return ; +} diff --git a/ft_strjoin.c b/ft_strjoin.c new file mode 100644 index 0000000..644ddbc --- /dev/null +++ b/ft_strjoin.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:04:10 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:43:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strjoin(const char *s1, const char *s2) +{ + char *res; + size_t size; + + size = ft_strlen(s1) + ft_strlen(s2) + 1; + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + res[0] = '\0'; + ft_strlcat(res, s1, size); + ft_strlcat(res, s2, size); + return (res); +} diff --git a/ft_strmapi.c b/ft_strmapi.c new file mode 100644 index 0000000..14550a0 --- /dev/null +++ b/ft_strmapi.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:12:48 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:17:31 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)) +{ + char *res; + unsigned int i; + + res = malloc((ft_strlen(s) + 1) * sizeof(char)); + if (res == NULL) + return (NULL); + i = 0; + while (s[i]) + { + res[i] = f(i, s[i]); + ++i; + } + res[i] = '\0'; + return (res); +} diff --git a/ft_strrchr.c b/ft_strrchr.c index eeb3d46..54b5195 100644 --- a/ft_strrchr.c +++ b/ft_strrchr.c @@ -6,14 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 10:46:09 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:51:17 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 17:11:34 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "libft.h" -char *strrchr(const char *s, int c) +char *ft_strrchr(const char *s, int c) { char *result; @@ -24,5 +24,7 @@ char *strrchr(const char *s, int c) result = (char *)s; ++s; } + if (c == '\0') + return ((char *)s); return (result); } diff --git a/ft_strtrim.c b/ft_strtrim.c new file mode 100644 index 0000000..44cddeb --- /dev/null +++ b/ft_strtrim.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:11:52 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:29:25 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +// allocating memory for the whole s1 - slightly memory inefficient +char *ft_strtrim(const char *s1, const char *set) +{ + char *res; + size_t i; + size_t j; + + res = malloc((ft_strlen(s1) + 1) * sizeof(char)); + if (res == NULL) + return (res); + i = 0; + j = 0; + while (s1[i] != '\0') + { + if (ft_strchr(set, s1[i]) == NULL) + res[j++] = s1[i]; + ++i; + } + return (res); +} diff --git a/ft_substr.c b/ft_substr.c new file mode 100644 index 0000000..99b30a4 --- /dev/null +++ b/ft_substr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:04:24 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:41:57 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_substr(const char *s, unsigned int start, size_t len) +{ + char *res; + size_t size; + + size = ft_strlen(s + start) + 1; + if (size > len + 1) + size = len + 1; + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + ft_strlcpy(res, s + start, size); + return (res); +} diff --git a/libft.h b/libft.h index 09b8629..28f01be 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:58:48 by ljiriste ### ########.fr */ +/* Updated: 2023/08/15 16:42:58 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,4 +46,19 @@ int ft_tolower(int c); int ft_atoi(const char *nptr); +char *ft_substr(const char *s, unsigned int start, size_t len); +char *ft_strjoin(const char *s1, const char *s2); +char *ft_strtrim(const char *s1, const char *set); +char **ft_split(const char *s, char c); + +char *ft_itoa(int n); + +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); + +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); + #endif -- 2.30.2