Everything passes norminette, compiles and links, but there are issues to be resolved.
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/13 13:44:39 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 12:45:44 by ljiriste ### ########.fr */
+/* Updated: 2023/08/15 17:11:47 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
-int atoi(const char *nptr)
+int ft_atoi(const char *nptr)
{
int res;
int sign;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 11:34:52 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 12:54:11 by ljiriste ### ########.fr */
+/* Updated: 2023/08/15 17:12:08 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h> // for malloc
#include "libft.h"
-void *calloc(size_t nmemb, size_t size)
+void *ft_calloc(size_t nmemb, size_t size)
{
size_t total_bytes;
void *res;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 12:19:40 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 12:20:08 by ljiriste ### ########.fr */
+/* Updated: 2023/08/15 17:10:02 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
-int is_ascii(int c)
+#include "libft.h"
+
+int ft_isascii(int c)
{
return (0x00 <= c && c <= 0x7F);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 11:29:54 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 11:30:29 by ljiriste ### ########.fr */
+/* Updated: 2023/08/15 17:12:30 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
-int is_space(int c)
+int ft_isspace(int c)
{
return (c == ' ' || ('\t' <= c && c <= '\r'));
}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_itoa.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:58:10 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 17:12:42 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+static unsigned int size_needed(int n)
+{
+ unsigned int res;
+
+ res = 1;
+ if (n == 0)
+ return (1);
+ if (n < 0)
+ ++res;
+ while (n != 0)
+ {
+ ++res;
+ n /= 10;
+ }
+ return (res);
+}
+
+char *ft_itoa(int n)
+{
+ unsigned int size;
+ char *res;
+
+ size = size_needed(n);
+ res = malloc(size * sizeof(char));
+ if (res == NULL)
+ return (res);
+ res[--size] = '\0';
+ if (n == 0 && res)
+ res[0] = '0';
+ else if (n < 0 && res)
+ {
+ res[0] = '-';
+ res[--size] = '0' - (n % 10);
+ n = -(n / 10);
+ }
+ while (n > 0 && res)
+ {
+ res[--size] = '0' + (n % 10);
+ n /= 10;
+ }
+ return (res);
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 10:56:53 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 11:01:34 by ljiriste ### ########.fr */
+/* Updated: 2023/08/15 17:07:39 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <sys/types.h>
#include "libft.h"
-void *memchr(const void *s, int c, size_t n)
+void *ft_memchr(const void *s, int c, size_t n)
{
size_t i;
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putchar_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:20:18 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:38:23 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include "libft.h"
+
+void ft_putchar_fd(char c, int fd)
+{
+ write(fd, &c, 1);
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putendl_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:25:41 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:38:32 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include "libft.h"
+
+void ft_putendl_fd(char *s, int fd)
+{
+ ft_putstr_fd(s, fd);
+ write(fd, "\n", 1);
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putnbr_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:27:25 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:38:56 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include "libft.h"
+
+void ft_putnbr_fd(int n, int fd)
+{
+ ft_putstr_fd(ft_itoa(n), fd);
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putstr_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:23:36 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:39:00 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include "libft.h"
+
+void ft_putstr_fd(char *s, int fd)
+{
+ write(fd, s, ft_strlen(s));
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_split.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:30:26 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 17:02:33 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+/* If str_p points (double deref.) to character that is equal to c,
+ this function advances it to first until it is not equal to c.
+ Otherwise it advances the pointer to first character, that equals c.
+ Function returns when encountering terminating null. */
+static void advance(const char **str_p, char c)
+{
+ if (**str_p == c)
+ while (**str_p == c && **str_p)
+ ++(*str_p);
+ else
+ while (**str_p != c && **str_p)
+ ++(*str_p);
+ return ;
+}
+
+static void free_str_array(char **arr)
+{
+ size_t i;
+
+ i = 0;
+ while (arr[i] != NULL)
+ free(arr[i++]);
+ free(arr);
+ return ;
+}
+
+static size_t word_count(const char *s, char c)
+{
+ size_t count;
+
+ count = 0;
+ while (*s)
+ {
+ while (*s == c)
+ ++s;
+ if (*s)
+ ++count;
+ while (*s != c && *s)
+ ++s;
+ }
+ return (count);
+}
+
+static size_t strlen_to_char(const char *s, char c)
+{
+ size_t len;
+
+ len = 0;
+ while (s[len] != c && s[len])
+ ++len;
+ return (len);
+}
+
+char **ft_split(const char *s, char c)
+{
+ char **res;
+ size_t size;
+ size_t i;
+
+ res = malloc((word_count(s, c) + 1) * sizeof(char *));
+ if (res == NULL)
+ return (res);
+ i = 0;
+ while (*s)
+ {
+ advance(&s, c);
+ if (*s == '\0')
+ break ;
+ size = (strlen_to_char(s, c) + 1);
+ res[i] = malloc(size * sizeof(char));
+ if (res[i] == NULL)
+ {
+ free_str_array(res);
+ return (NULL);
+ }
+ ft_strlcpy(res[i++], s, size);
+ advance(&s, c);
+ }
+ res[i] = NULL;
+ return (res);
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 10:41:10 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 12:51:36 by ljiriste ### ########.fr */
+/* Updated: 2023/08/15 17:10:35 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "libft.h"
-char *strchr(const char *s, int c)
+char *ft_strchr(const char *s, int c)
{
while (*s != '\0')
{
return ((char *)s);
++s;
}
+ if (c == '\0')
+ return ((char *)s);
return (NULL);
}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_striteri.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:18:09 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:47:35 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_striteri(char *s, void (*f)(unsigned int, char *))
+{
+ unsigned int i;
+
+ i = 0;
+ while (s[i])
+ {
+ f(i, s + i);
+ ++i;
+ }
+ return ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strjoin.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:04:10 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:43:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+char *ft_strjoin(const char *s1, const char *s2)
+{
+ char *res;
+ size_t size;
+
+ size = ft_strlen(s1) + ft_strlen(s2) + 1;
+ res = malloc(size * sizeof(char));
+ if (res == NULL)
+ return (res);
+ res[0] = '\0';
+ ft_strlcat(res, s1, size);
+ ft_strlcat(res, s2, size);
+ return (res);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strmapi.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 16:12:48 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:17:31 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+char *ft_strmapi(const char *s, char (*f)(unsigned int, char))
+{
+ char *res;
+ unsigned int i;
+
+ res = malloc((ft_strlen(s) + 1) * sizeof(char));
+ if (res == NULL)
+ return (NULL);
+ i = 0;
+ while (s[i])
+ {
+ res[i] = f(i, s[i]);
+ ++i;
+ }
+ res[i] = '\0';
+ return (res);
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 10:46:09 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 12:51:17 by ljiriste ### ########.fr */
+/* Updated: 2023/08/15 17:11:34 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "libft.h"
-char *strrchr(const char *s, int c)
+char *ft_strrchr(const char *s, int c)
{
char *result;
result = (char *)s;
++s;
}
+ if (c == '\0')
+ return ((char *)s);
return (result);
}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strtrim.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:11:52 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:29:25 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+// allocating memory for the whole s1 - slightly memory inefficient
+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));
+ if (res == NULL)
+ return (res);
+ i = 0;
+ j = 0;
+ while (s1[i] != '\0')
+ {
+ if (ft_strchr(set, s1[i]) == NULL)
+ res[j++] = s1[i];
+ ++i;
+ }
+ return (res);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_substr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/15 15:04:24 by ljiriste #+# #+# */
+/* Updated: 2023/08/15 16:41:57 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#include "libft.h"
+
+char *ft_substr(const char *s, unsigned int start, size_t len)
+{
+ char *res;
+ size_t size;
+
+ size = ft_strlen(s + start) + 1;
+ if (size > len + 1)
+ size = len + 1;
+ res = malloc(size * sizeof(char));
+ if (res == NULL)
+ return (res);
+ ft_strlcpy(res, s + start, size);
+ return (res);
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */
-/* Updated: 2023/08/15 12:58:48 by ljiriste ### ########.fr */
+/* Updated: 2023/08/15 16:42:58 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int ft_atoi(const char *nptr);
+char *ft_substr(const char *s, unsigned int start, size_t len);
+char *ft_strjoin(const char *s1, const char *s2);
+char *ft_strtrim(const char *s1, const char *set);
+char **ft_split(const char *s, char c);
+
+char *ft_itoa(int n);
+
+char *ft_strmapi(const char *s, char (*f)(unsigned int, char));
+void ft_striteri(char *s, void (*f)(unsigned int, char *));
+
+void ft_putchar_fd(char c, int fd);
+void ft_putstr_fd(char *s, int fd);
+void ft_putendl_fd(char *s, int fd);
+void ft_putnbr_fd(int n, int fd);
+
#endif