ft_strtrim needs to also handle empty strings and strings containing only chars from set.
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 11:34:52 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 17:12:08 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:26:57 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
void *res;
if (nmemb == 0 || size == 0)
- return (NULL);
+ return (malloc(0));
total_bytes = nmemb * size;
if (total_bytes / size != nmemb)
{
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/16 13:45:28 by ljiriste #+# #+# */
-/* Updated: 2023/08/16 16:48:58 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:33:24 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
{
t_list *temp;
+ if (lst == NULL || del == NULL)
+ return ;
while (*lst)
{
temp = (*lst)->next;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/16 13:43:02 by ljiriste #+# #+# */
-/* Updated: 2023/08/16 13:46:37 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:33:02 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
+ if (lst == NULL || del == NULL)
+ return ;
del(lst->content);
free(lst);
return ;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/14 13:35:59 by ljiriste #+# #+# */
-/* Updated: 2023/08/14 13:36:20 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:16:35 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
+#include <stddef.h>
#include <sys/types.h>
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
+ if (dest == NULL && src == NULL)
+ return (NULL);
while (n > 0)
{
--n;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 16:23:36 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 16:39:00 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:29:20 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
void ft_putstr_fd(char *s, int fd)
{
+ if (s == NULL)
+ return ;
write(fd, s, ft_strlen(s));
return ;
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 15:30:26 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 17:02:33 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 17:53:03 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (**s);
}
-static size_t word_count(const char *s, char c)
+static int word_count(const char *s, char c)
{
- size_t count;
+ int count;
+ if (s == NULL)
+ return (-1);
if (c == '\0')
return (1);
count = 0;
size_t i;
res = malloc((word_count(s, c) + 1) * sizeof(char *));
- if (res == NULL)
+ if (res == NULL || s == NULL)
return (res);
i = 0;
while (*s)
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 16:18:09 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 16:47:35 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:33:39 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
+#include <stddef.h>
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
unsigned int i;
+ if (s == NULL || f == NULL)
+ return ;
i = 0;
while (s[i])
{
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 15:04:10 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 16:43:17 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:33:53 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
char *res;
size_t size;
+ if (s1 == NULL || s2 == NULL)
+ return (NULL);
size = ft_strlen(s1) + ft_strlen(s2) + 1;
res = malloc(size * sizeof(char));
if (res == NULL)
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 17:32:33 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 12:55:06 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:40:14 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
+#include <stddef.h>
#include <sys/types.h>
#include "libft.h"
{
size_t length;
+ if ((dst == NULL && src == NULL) || size == 0)
+ return (0);
length = 0;
while (*dst && size > length)
{
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 16:12:48 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 16:17:31 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:32:11 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
char *res;
unsigned int i;
+ if (s == NULL || f == NULL)
+ return (NULL);
res = malloc((ft_strlen(s) + 1) * sizeof(char));
if (res == NULL)
return (NULL);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 17:02:07 by ljiriste #+# #+# */
-/* Updated: 2023/08/16 13:16:34 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:24:25 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
while (s1[i] == s2[i] && s1[i] && s2[i] && i + 1 < n)
++i;
if (n > 0)
- return (s1[i] - s2[i]);
+ return ((unsigned char)s1[i] - (unsigned char)s2[i]);
else
return (0);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 17:15:34 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 12:50:26 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:57:24 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
size_t little_len;
size_t i;
+ if (little == NULL && len == 0)
+ return ((char *)big);
if (*little == '\0')
return ((char *)big);
+ if (big == NULL && len == 0)
+ return (NULL);
little_len = ft_strlen(little);
i = 0;
while (big[i] && i + little_len <= len)
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 15:11:52 by ljiriste #+# #+# */
-/* Updated: 2023/08/16 17:30:54 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 19:12:36 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
static size_t size_needed(const char *s1, const char *set)
{
- size_t res;
+ size_t at_start;
+ size_t at_end;
+ size_t whole;
- res = 1;
+ whole = ft_strlen(s1);
+ at_start = 0;
+ at_end = 0;
+ while (ft_strchr(set, *s1))
+ {
+ ++s1;
+ ++at_start;
+ }
while (*s1)
- if (ft_strchr(set, *(s1++)) == NULL)
- ++res;
- return (res);
+ ++s1;
+ --s1;
+ while (ft_strchr(set, *s1))
+ {
+ --s1;
+ ++at_end;
+ }
+ return (whole - at_start - at_end + 1);
}
char *ft_strtrim(const char *s1, const char *set)
{
+ size_t size;
char *res;
- size_t i;
- size_t j;
- res = malloc(size_needed(s1, set) * sizeof(char));
+ if (s1 == NULL)
+ return (NULL);
+ size = size_needed(s1, set);
+ res = malloc(size * sizeof(char));
if (res == NULL)
return (res);
- i = 0;
- j = 0;
- while (s1[i])
- {
- if (ft_strchr(set, s1[i]) == NULL)
- res[j++] = s1[i];
- ++i;
- }
- res[j] = '\0';
+ while (ft_strchr(set, *s1))
+ ++s1;
+ ft_strlcpy(res, s1, size);
return (res);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 15:04:24 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 16:41:57 by ljiriste ### ########.fr */
+/* Updated: 2023/09/05 18:27:38 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
char *res;
size_t size;
+ if (s == NULL)
+ return (NULL);
size = ft_strlen(s) + 1;
if (start > size)
{