?inor changes were also made to make the code more consistent.
/* 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 */
/* */
/* ************************************************************************** */
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;
/* 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 */
/* */
/* ************************************************************************** */
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]);
/* 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 */
/* */
/* ************************************************************************** */
char *result;
result = NULL;
- while (*s != '\0')
+ while (*s)
{
if (*s == (char)c)
result = (char *)s;
/* 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;