Implemented functions of Part 2 and repaired older functions.
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 15 Aug 2023 15:15:58 +0000 (17:15 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 15 Aug 2023 15:15:58 +0000 (17:15 +0200)
Everything passes norminette, compiles and links, but there are issues to be resolved.

19 files changed:
ft_atoi.c
ft_calloc.c
ft_isascii.c
ft_isspace.c
ft_itoa.c [new file with mode: 0644]
ft_memchr.c
ft_putchar_fd.c [new file with mode: 0644]
ft_putendl_fd.c [new file with mode: 0644]
ft_putnbr_fd.c [new file with mode: 0644]
ft_putstr_fd.c [new file with mode: 0644]
ft_split.c [new file with mode: 0644]
ft_strchr.c
ft_striteri.c [new file with mode: 0644]
ft_strjoin.c [new file with mode: 0644]
ft_strmapi.c [new file with mode: 0644]
ft_strrchr.c
ft_strtrim.c [new file with mode: 0644]
ft_substr.c [new file with mode: 0644]
libft.h

index 59dd2176f8366544fe09fbc01adb836ca11ceedf..881b763fc6188221f8d9f57b52d0a9efc761242f 100644 (file)
--- a/ft_atoi.c
+++ b/ft_atoi.c
@@ -6,13 +6,13 @@
 /*   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;
index b810d7e63077358c90ba1fa09487a8f3684132eb..d7fe1f3539a378509460b791db74fb59a7b00d0c 100644 (file)
@@ -6,14 +6,14 @@
 /*   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;
index 43682c015181b89add5e978b29d33cbd90c00daa..377081b9c05a8dce037d802b03aa9aae87a18839 100644 (file)
@@ -6,11 +6,13 @@
 /*   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);
 }
index cbf9b0994929577b790c599103ced774f2ae7990..23f6070f27fbea5efb25856543c8628e40caea56 100644 (file)
@@ -6,13 +6,13 @@
 /*   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'));
 }
diff --git a/ft_itoa.c b/ft_itoa.c
new file mode 100644 (file)
index 0000000..b47047c
--- /dev/null
+++ b/ft_itoa.c
@@ -0,0 +1,57 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
index 3a4a944344e51c0c35aa246d8b0f8a45f1a2ba15..9be9e521e525c0c06def14338cc01cb55d76c640 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,7 +14,7 @@
 #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;
 
diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c
new file mode 100644 (file)
index 0000000..cfa4476
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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 ;
+}
diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c
new file mode 100644 (file)
index 0000000..cb811fa
--- /dev/null
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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 ;
+}
diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c
new file mode 100644 (file)
index 0000000..fb9d62b
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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 ;
+}
diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c
new file mode 100644 (file)
index 0000000..5f3fb14
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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 ;
+}
diff --git a/ft_split.c b/ft_split.c
new file mode 100644 (file)
index 0000000..673b9d5
--- /dev/null
@@ -0,0 +1,96 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
index 594d6a2c40824f6312c7e6c8cf330d39bad05217..65c51a298c4989940b3b6e88685a4152e3c858d6 100644 (file)
@@ -6,14 +6,14 @@
 /*   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')
        {
@@ -21,5 +21,7 @@ char  *strchr(const char *s, int c)
                        return ((char *)s);
                ++s;
        }
+       if (c == '\0')
+               return ((char *)s);
        return (NULL);
 }
diff --git a/ft_striteri.c b/ft_striteri.c
new file mode 100644 (file)
index 0000000..606ce6b
--- /dev/null
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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 ;
+}
diff --git a/ft_strjoin.c b/ft_strjoin.c
new file mode 100644 (file)
index 0000000..644ddbc
--- /dev/null
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
diff --git a/ft_strmapi.c b/ft_strmapi.c
new file mode 100644 (file)
index 0000000..14550a0
--- /dev/null
@@ -0,0 +1,32 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
index eeb3d46b0a13107d70738ce77c9d8f61e7218b2e..54b51954f76bb7dc0e776618ad387a5da1ebd172 100644 (file)
@@ -6,14 +6,14 @@
 /*   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;
 
@@ -24,5 +24,7 @@ char  *strrchr(const char *s, int c)
                        result = (char *)s;
                ++s;
        }
+       if (c == '\0')
+               return ((char *)s);
        return (result);
 }
diff --git a/ft_strtrim.c b/ft_strtrim.c
new file mode 100644 (file)
index 0000000..44cddeb
--- /dev/null
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
diff --git a/ft_substr.c b/ft_substr.c
new file mode 100644 (file)
index 0000000..99b30a4
--- /dev/null
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
diff --git a/libft.h b/libft.h
index 09b86297604ef387c11defc77ddfe49bbcb79823..28f01be25143436bb1f2aca885cc98b31caf9ab8 100644 (file)
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -46,4 +46,19 @@ int          ft_tolower(int c);
 
 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