]> Untitled Git - axy/ft/libft.git/commitdiff
partial completion
author= <=>
Mon, 27 Oct 2025 14:51:27 +0000 (15:51 +0100)
committer= <=>
Mon, 27 Oct 2025 14:51:27 +0000 (15:51 +0100)
Makefile
ft_char.c [new file with mode: 0644]
ft_char2.c [new file with mode: 0644]
ft_mem.c [new file with mode: 0644]
ft_mem2.c [new file with mode: 0644]
ft_str.c [new file with mode: 0644]
ft_str2.c [new file with mode: 0644]
libft.h [new file with mode: 0644]

index 844d5c53b0ff810af71fe5c9878eb74b88b688d6..3b720175065bfb72e99332853ab48c61ca0e30e3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 NAME=libft.a
 
-SRCS=
+SRCS=ft_char2.c  ft_char.c  ft_mem2.c  ft_mem.c  ft_str2.c  ft_str.c
 
 OBJS=${SRCS:.c=.o}
 
diff --git a/ft_char.c b/ft_char.c
new file mode 100644 (file)
index 0000000..ff7a429
--- /dev/null
+++ b/ft_char.c
@@ -0,0 +1,36 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_char.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/10/27 11:30:03 by agilliar          #+#    #+#             */
+/*   Updated: 2025/10/27 13:17:26 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+int    ft_isalpha(int c)
+{
+       return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
+}
+
+int    ft_isdigit(int c)
+{
+       return (c >= '0' && c <= '9');
+}
+
+int    ft_isalnum(int c)
+{
+       return (ft_isalpha(c) || ft_isdigit(c));
+}
+
+int    ft_isascii(int c)
+{
+       return (c >= '\0' && c <= '\x7F');
+}
+
+int    ft_isprint(int c)
+{
+       return (c >= ' ' && c <= '~');
+}
diff --git a/ft_char2.c b/ft_char2.c
new file mode 100644 (file)
index 0000000..a5c80c0
--- /dev/null
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_char2.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/10/27 12:56:07 by agilliar          #+#    #+#             */
+/*   Updated: 2025/10/27 13:08:18 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+int    ft_toupper(int c)
+{
+       if (c >= 'a' && c <= 'z')
+               c = c - 'a' + 'A';
+       return (c);
+}
+
+int    ft_tolower(int c)
+{
+       if (c >= 'A' && c <= 'Z')
+               c = c - 'A' + 'a';
+       return (c);
+}
diff --git a/ft_mem.c b/ft_mem.c
new file mode 100644 (file)
index 0000000..4420cc3
--- /dev/null
+++ b/ft_mem.c
@@ -0,0 +1,68 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_mem.c                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/10/27 12:08:42 by agilliar          #+#    #+#             */
+/*   Updated: 2025/10/27 14:51:45 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   *ft_memset(void *s, int c, size_t n)
+{
+       size_t  i;
+
+       i = 0;
+       while (i < n)
+               ((unsigned char *) s)[i++] = c;
+       return (s);
+}
+
+void   *ft_bzero(void *s, size_t n)
+{
+       return (ft_memset(s, '\0', n));
+}
+
+void   *ft_memcpy(void *restrict dest, const void *src, size_t n)
+{
+       size_t  i;
+
+       i = 0;
+       while (i < n)
+       {
+               ((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
+               i++;
+       }
+       return (dest);
+}
+
+void   *ft_memmove(void *dest, const void *src, size_t n)
+{
+       size_t  i;
+
+       i = 0;
+       while (i < n)
+       {
+               ((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
+               i++;
+       }
+       return (dest);
+}
+
+void   *ft_memchr(const void *s, int c, size_t n)
+{
+       size_t  i;
+
+       i = 0;
+       while (i < n)
+       {
+               if (((unsigned char *)s)[i] == c)
+                       return ((void *)&((unsigned char *)s)[i]);
+               i++;
+       }
+       return (NULL);
+}
diff --git a/ft_mem2.c b/ft_mem2.c
new file mode 100644 (file)
index 0000000..9ca5cb2
--- /dev/null
+++ b/ft_mem2.c
@@ -0,0 +1,54 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_mem2.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/10/27 14:39:28 by agilliar          #+#    #+#             */
+/*   Updated: 2025/10/27 15:10:14 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include <stdlib.h>
+#include <stdint.h>
+
+int    ft_memcmp(const void *s1, const void *s2, size_t n)
+{
+       size_t  i;
+       int             res;
+
+       i = 0;
+       res = 0;
+       while (res == 0 && i < n)
+       {
+               res = ((unsigned char *) s1)[i] - ((unsigned char *) s2)[i];
+               i++;
+       }
+       return (res);
+}
+
+void   *ft_calloc(size_t nmemb, size_t size)
+{
+       void    *res;
+
+       if (SIZE_MAX / size < nmemb)
+               return (NULL);
+       res = malloc(nmemb * size);
+       if (res)
+               ft_bzero(res, nmemb * size);
+       return (res);
+}
+
+char   *ft_strdup(char *src)
+{
+       size_t  len;
+       char    *res;
+
+       len = ft_strlen(src) + 1;
+       res = malloc(len);
+       if (res)
+               ft_memcpy(res, src, len);
+       return (res);
+}
diff --git a/ft_str.c b/ft_str.c
new file mode 100644 (file)
index 0000000..df5aa0b
--- /dev/null
+++ b/ft_str.c
@@ -0,0 +1,85 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_str.c                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/10/27 12:11:26 by agilliar          #+#    #+#             */
+/*   Updated: 2025/10/27 13:29:49 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+size_t ft_strlen(const char *s)
+{
+       size_t  i;
+
+       i = 0;
+       while (s[i] != '\0')
+               i++;
+       return (i);
+}
+
+size_t ft_strlcpy(char *dest, char *src, size_t size)
+{
+       size_t  len;
+       size_t  i;
+
+       len = 0;
+       while (src[len] != '\0')
+               len++;
+       if (size == 0)
+               return (len);
+       i = 0;
+       while (i < len && i < size - 1)
+               *dest++ = src[i++];
+       *dest = '\0';
+       return (len);
+}
+
+size_t ft_strlcat(char *dest, char *src, size_t size)
+{
+       size_t  src_len;
+       size_t  dest_len;
+       size_t  i;
+
+       src_len = 0;
+       while (src[src_len])
+               src_len++;
+       dest_len = 0;
+       while (dest_len < size && dest[dest_len])
+               dest_len++;
+       i = 0;
+       while (dest_len + i + 1 < size && i < src_len)
+       {
+               dest[dest_len + i] = src[i];
+               i++;
+       }
+       if (dest_len != size)
+               dest[dest_len + i] = '\0';
+       return (src_len + dest_len);
+}
+
+char   *ft_strchr(const char *s, int c)
+{
+       while (*s != c && *s != '\0')
+               s++;
+       if (*s == c)
+               return ((char *) s);
+       return (NULL);
+}
+
+char   *ft_strrchr(const char *s, int c)
+{
+       char    *res;
+
+       res = (char *) s;
+       while (*s != '\0')
+               if (*s == c)
+                       res = (char *) s;
+       if (*res == c)
+               return (res);
+       return (NULL);
+}
diff --git a/ft_str2.c b/ft_str2.c
new file mode 100644 (file)
index 0000000..3ab511f
--- /dev/null
+++ b/ft_str2.c
@@ -0,0 +1,94 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_str2.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/10/27 13:31:21 by agilliar          #+#    #+#             */
+/*   Updated: 2025/10/27 15:36:29 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include <stdbool.h>
+#include <stdlib.h>
+
+int    ft_strncmp(char *s1, char *s2, size_t n)
+{
+       unsigned int    idx;
+
+       if (n == 0)
+               return (0);
+       idx = 0;
+       while (s1[idx] != '\0' && s1[idx] == s2[idx] && idx < n - 1)
+               idx++;
+       return ((unsigned char) s1[idx] - (unsigned char) s2[idx]);
+}
+
+char   *ft_strnstr(const char *big, const char *little, size_t len)
+{
+       size_t  i;
+       size_t  j;
+
+       i = 0;
+       j = 0;
+       while (little[i] && i + j < len && big[i + j])
+       {
+               if (big[i + j] != little[i])
+               {
+                       i = 0;
+                       j++;
+               }
+               else
+                       i++;
+       }
+       if (little[i])
+               return (NULL);
+       return ((char *)&big[j]);
+}
+
+static bool    ft_isspace(char c)
+{
+       return (c == ' ' || (c >= '\t' && c <= '\r'));
+}
+
+int    ft_atoi(const char *nptr)
+{
+       int     sign;
+       int     res;
+
+       sign = 1;
+       res = 0;
+       while (ft_isspace(*nptr))
+               nptr++;
+       if (*nptr == '-')
+               sign = -1;
+       else if (*nptr != '+')
+               nptr--;
+       nptr++;
+       while (ft_isdigit(*nptr))
+       {
+               res *= 10;
+               res += sign * (*nptr - '0');
+               nptr++;
+       }
+       return (res);
+}
+
+char   *ft_substr(const char *s, unsigned int start, size_t len)
+{
+       size_t  i;
+       char    *res;
+
+       s = s + start;
+       i = 0;
+       while (s[i] && i < len)
+               i++;
+       res = malloc(i + 1);
+       if (!res)
+               return (res);
+       ft_memcpy(res, s, i);
+       res[i] = '\0';
+       return (res);
+}
diff --git a/libft.h b/libft.h
new file mode 100644 (file)
index 0000000..295fe96
--- /dev/null
+++ b/libft.h
@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   libft.h                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/10/27 11:27:06 by agilliar          #+#    #+#             */
+/*   Updated: 2025/10/27 15:34:27 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef LIBFT_H
+# define LIBFT_H
+
+# include <stddef.h>
+
+int            ft_strncmp(char *s1, char *s2, size_t n);
+char   *ft_strnstr(const char *big, const char *little, size_t len);
+int            ft_atoi(const char *nptr);
+int            ft_memcmp(const void *s1, const void *s2, size_t n);
+void   *ft_calloc(size_t nmemb, size_t size);
+char   *ft_strdup(char *src);
+void   *ft_memset(void *s, int c, size_t n);
+void   *ft_bzero(void *s, size_t n);
+void   *ft_memcpy(void *restrict dest, const void *src, size_t n);
+void   *ft_memmove(void *dest, const void *src, size_t n);
+void   *ft_memchr(const void *s, int c, size_t n);
+int            ft_toupper(int c);
+int            ft_tolower(int c);
+int            ft_isalpha(int c);
+int            ft_isdigit(int c);
+int            ft_isalnum(int c);
+int            ft_isascii(int c);
+int            ft_isprint(int c);
+size_t ft_strlen(const char *s);
+size_t ft_strlcpy(char *dest, char *src, size_t size);
+size_t ft_strlcat(char *dest, char *src, size_t size);
+char   *ft_strchr(const char *s, int c);
+char   *ft_strrchr(const char *s, int c);
+
+#endif