From 960f8d50e5addab591d063d81cca2de4ba80841c Mon Sep 17 00:00:00 2001
From: Adam Joly <adjoly@2E2.42angouleme.fr>
Date: Sat, 11 Nov 2023 01:28:12 +0100
Subject: [PATCH] fix strjoin & substr & make strtrim

---
 Makefile     |  3 ++-
 ft_calloc.c  |  4 ++--
 ft_split.c   | 28 +++++++++++++++++++++++-----
 ft_strjoin.c | 30 ++++++++++++++++++++----------
 ft_strtrim.c | 31 +++++++++++++++++++++++++++++++
 ft_substr.c  | 17 ++++++++---------
 libft.h      |  3 ++-
 7 files changed, 88 insertions(+), 28 deletions(-)
 create mode 100644 ft_strtrim.c

diff --git a/Makefile b/Makefile
index b118731..89ba9c2 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
 #    By: adjoly <adjoly@student.42.fr>              +#+  +:+       +#+         #
 #                                                 +#+#+#+#+#+   +#+            #
 #    Created: 2023/11/01 11:03:22 by adjoly            #+#    #+#              #
-#    Updated: 2023/11/09 19:22:28 by adjoly           ###   ########.fr        #
+#    Updated: 2023/11/11 01:04:23 by adjoly           ###   ########.fr        #
 #                                                                              #
 # **************************************************************************** #
 
@@ -44,6 +44,7 @@ SRCS = ft_atoi.c \
 		ft_strrchr.c \
 		ft_strnstr.c \
 		ft_putendl_fd.c \
+		ft_strtrim.c \
 
 OBJS = $(SRCS:.c=.o)
 
diff --git a/ft_calloc.c b/ft_calloc.c
index b39ff62..6d2e83c 100644
--- a/ft_calloc.c
+++ b/ft_calloc.c
@@ -6,7 +6,7 @@
 /*   By: adjoly <adjoly@student.42.fr>              +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/11/01 16:02:26 by adjoly            #+#    #+#             */
-/*   Updated: 2023/11/05 22:23:07 by adjoly           ###   ########.fr       */
+/*   Updated: 2023/11/11 00:55:15 by adjoly           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,7 +18,7 @@ void	*ft_calloc(size_t nmemb, size_t size)
 	void	*result;
 
 	if (size != 0 && nmemb != 0 && (nmemb * size) / nmemb != size)
-		return (NULL);
+		return (malloc(0));
 	result = malloc(size * nmemb);
 	if (result == NULL)
 		return (NULL);
diff --git a/ft_split.c b/ft_split.c
index b2bae14..f293a3a 100644
--- a/ft_split.c
+++ b/ft_split.c
@@ -6,7 +6,7 @@
 /*   By: adjoly <adjoly@student.42.fr>              +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/11/09 19:04:42 by adjoly            #+#    #+#             */
-/*   Updated: 2023/11/10 17:05:46 by adjoly           ###   ########.fr       */
+/*   Updated: 2023/11/11 00:36:54 by adjoly           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,23 +18,41 @@ static int	ft_countword(char const *s, char sep)
 	int	w_count;
 
 	i = 0;
-	if (s[i] != sep)
-		w_count++;
+	w_count = 0;
 	while (s[i])
 	{
-		if (s[i] == sep && s[i - 1] != sep)
+		if (s[i] != sep && (i == 0 || s[i - 1] == sep))
 			w_count++;
 		i++;
 	}
 	return (w_count);
 }
 
+const int	ft_countletter(char const *s, char sep)
+{
+	int	i;
+
+	i = 0;
+	while (s[i] != sep || s[i] != '\0')
+		i++;
+	return (i);
+}
+
 char	**ft_split(char const *s, char c)
 {
 	int		w_count;
 	char	**result;
 
 	w_count = ft_countword(s, c);
-	result = malloc(w_count);
+	result = malloc((w_count + 1) * sizeof(char *));
+	if (result == NULL)
+		return (NULL);
+	while (s[i])
+	{
+		while ()
+		{
+		
+		}
+	}
 	return (result);
 }
diff --git a/ft_strjoin.c b/ft_strjoin.c
index f296829..5951941 100644
--- a/ft_strjoin.c
+++ b/ft_strjoin.c
@@ -6,7 +6,7 @@
 /*   By: adjoly <adjoly@student.42.fr>              +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/11/04 13:44:09 by adjoly            #+#    #+#             */
-/*   Updated: 2023/11/09 19:19:28 by adjoly           ###   ########.fr       */
+/*   Updated: 2023/11/11 01:26:47 by adjoly           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,18 +14,28 @@
 
 char	*ft_strjoin(char const *s1, char const *s2)
 {
-	// int		i;
 	char	*result;
-	size_t	len_s1;
-	size_t	len_s2;
+	size_t	i;
+	size_t	j;
 
-	// i = 0;
-	len_s1 = ft_strlen(s1);
-	len_s2 = ft_strlen(s2);
-	result = malloc(len_s1 + len_s2 + 1);
+	i = 0;
+	j = 0;
+	if (s1 == NULL && s2 == NULL)
+		return (NULL);
+	result = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
 	if (result == NULL)
 		return (NULL);
-	ft_memcpy(result, s1, len_s1);
-	ft_memcpy(result + len_s1, s2, len_s2);
+	while (s1[i])
+	{
+		result[i] = s1[i];
+		i++;
+	}
+	while (s2[j])
+	{
+		result[i] = s2[j];
+		i++;
+		j++;
+	}
+	result[i] = '\0';
 	return (result);
 }
diff --git a/ft_strtrim.c b/ft_strtrim.c
new file mode 100644
index 0000000..f567d4b
--- /dev/null
+++ b/ft_strtrim.c
@@ -0,0 +1,31 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strtrim.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: adjoly <adjoly@student.42.fr>              +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/11/11 00:37:37 by adjoly            #+#    #+#             */
+/*   Updated: 2023/11/11 01:09:36 by adjoly           ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char	*ft_strtrim(char const *s1, char const *set)
+{
+	int		i;
+	int		j;
+	char	*result;
+
+	i = 0;
+	if (s1 == NULL || set == NULL)
+		return (NULL);
+	j = ft_strlen(s1);
+	while (ft_strchr(set, s1[i]) != 0)
+		i++;
+	while (ft_strrchr(set, s1[j]) != 0)
+		j--;
+	result = ft_substr(s1, i, (j - i) + 1);
+	return (result);
+}
diff --git a/ft_substr.c b/ft_substr.c
index edcef0a..cff2350 100644
--- a/ft_substr.c
+++ b/ft_substr.c
@@ -6,7 +6,7 @@
 /*   By: adjoly <adjoly@student.42.fr>              +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/11/02 17:59:58 by adjoly            #+#    #+#             */
-/*   Updated: 2023/11/10 17:13:29 by adjoly           ###   ########.fr       */
+/*   Updated: 2023/11/11 01:03:32 by adjoly           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -19,16 +19,15 @@ char	*ft_substr(char const *s, unsigned int start, size_t len)
 
 	i = 0;
 	if (s == NULL)
-		return (NULL);
-	if (start > ft_strlen(s))
-	{
-		result = calloc(1, 1);
-		return (result);
-	}
-	result = malloc(((ft_strlen(s) - len) + 1) * sizeof(char));
+		return (0);
+	if (len >= ft_strlen(s))
+		len = ft_strlen(s) - start;
+	if (len == 0 || start >= ft_strlen(s))
+		return (calloc(1, 1));
+	result = malloc((len + 1) * sizeof(char));
 	if (result == NULL)
 		return (NULL);
-	while (i < len - 1 && s[start + i])
+	while (i < len && s[start + i])
 	{
 		result[i] = s[start + i];
 		i++;
diff --git a/libft.h b/libft.h
index 4684357..69a0b4a 100644
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
 /*   By: adjoly <adjoly@student.42.fr>              +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/11/01 10:06:03 by adjoly            #+#    #+#             */
-/*   Updated: 2023/11/09 19:22:26 by adjoly           ###   ########.fr       */
+/*   Updated: 2023/11/11 01:05:15 by adjoly           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -49,5 +49,6 @@ int		ft_memcmp(const void *s1, const void *s2, size_t n);
 char	*ft_strrchr(const char *s, int c);
 char	*ft_strnstr(const char *big, const char *little, size_t len);
 void	ft_putendl_fd(char *s, int fd);
+char	*ft_strtrim(char const *s1, char const *set);
 
 #endif
\ No newline at end of file