Function ft_strtrim now allocates exactly the memory needed for the resulting string.
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 16 Aug 2023 15:41:49 +0000 (17:41 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 16 Aug 2023 15:41:49 +0000 (17:41 +0200)
?inor changes were also made to make the code more consistent.

ft_memmove.c
ft_strncmp.c
ft_strrchr.c
ft_strtrim.c

index 8cb233dce1be08807660c02c2b6067f4432bebf2..aa808ede0f49c3dfd0b37f5f3bfb6f900538fda3 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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;
index ef313b3447b840305832ff6126b1dd9d18f6861f..fb68698bfedaa166ba7696b40ace9e3f5db14759 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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]);
index f85b16c3879ffaf3ddbf60213c442a9094660b66..8be5a063deca39bffff14ed8b75b60adea61ffcb 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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;
index ac21682b99b18e0a287df020e22a3063178efb30..7bbf77654a96ebecf77102bd48cce78adb009c4e 100644 (file)
@@ -6,21 +6,32 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <sys/types.h>
 #include <stdlib.h>
 #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;