From a31e4dce0ec83817e060c603266fdc134f3bc5c8 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Wed, 16 Aug 2023 17:41:49 +0200 Subject: [PATCH] Function ft_strtrim now allocates exactly the memory needed for the resulting string. ?inor changes were also made to make the code more consistent. --- ft_memmove.c | 11 +++++++++-- ft_strncmp.c | 4 ++-- ft_strrchr.c | 4 ++-- ft_strtrim.c | 17 ++++++++++++++--- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/ft_memmove.c b/ft_memmove.c index 8cb233d..aa808ed 100644 --- a/ft_memmove.c +++ b/ft_memmove.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/14 13:36:32 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:03:46 by ljiriste ### ########.fr */ +/* Updated: 2023/08/16 13:17:49 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,14 @@ void *ft_memmove(void *dest, const void *src, size_t n) size_t i; if (dest > src) - ft_memcpy(dest, src, n); + { + i = n; + while (i > 0) + { + --i; + *((unsigned char *)dest + i) = *((unsigned char *)src + i); + } + } else if (dest < src) { i = 0; diff --git a/ft_strncmp.c b/ft_strncmp.c index ef313b3..fb68698 100644 --- a/ft_strncmp.c +++ b/ft_strncmp.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/12 17:02:07 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:56:20 by ljiriste ### ########.fr */ +/* Updated: 2023/08/16 13:16:34 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ 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) + while (s1[i] == s2[i] && s1[i] && s2[i] && i + 1 < n) ++i; if (n > 0) return (s1[i] - s2[i]); diff --git a/ft_strrchr.c b/ft_strrchr.c index f85b16c..8be5a06 100644 --- a/ft_strrchr.c +++ b/ft_strrchr.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 10:46:09 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 17:11:34 by ljiriste ### ########.fr */ +/* Updated: 2023/08/16 13:16:52 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ char *ft_strrchr(const char *s, int c) char *result; result = NULL; - while (*s != '\0') + while (*s) { if (*s == (char)c) result = (char *)s; diff --git a/ft_strtrim.c b/ft_strtrim.c index ac21682..7bbf776 100644 --- a/ft_strtrim.c +++ b/ft_strtrim.c @@ -6,21 +6,32 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 15:11:52 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 16:29:25 by ljiriste ### ########.fr */ +/* Updated: 2023/08/16 17:30:54 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include #include "libft.h" -// allocating memory for the whole s1 - slightly memory inefficient +static size_t size_needed(const char *s1, const char *set) +{ + size_t res; + + res = 1; + while (*s1) + if (ft_strchr(set, *(s1++)) == NULL) + ++res; + return (res); +} + 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)); + res = malloc(size_needed(s1, set) * sizeof(char)); if (res == NULL) return (res); i = 0; -- 2.30.2