aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ft_calloc.c2
-rw-r--r--ft_memcmp.c3
-rw-r--r--ft_memset.c17
-rw-r--r--ft_strcat.c18
-rw-r--r--ft_strchr.c12
-rw-r--r--ft_strclr.c5
-rw-r--r--ft_strcmp.c11
-rw-r--r--ft_strcpy.c15
-rw-r--r--ft_strlen.c30
-rw-r--r--ft_strncmp.c10
-rw-r--r--ft_strncpy.c16
-rw-r--r--libft.h5
12 files changed, 72 insertions, 72 deletions
diff --git a/ft_calloc.c b/ft_calloc.c
index 0a79c03..24501bf 100644
--- a/ft_calloc.c
+++ b/ft_calloc.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 12:45:37 by cacharle #+# #+# */
-/* Updated: 2019/11/20 04:00:52 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 01:05:53 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/ft_memcmp.c b/ft_memcmp.c
index 26cc0e7..2c8e179 100644
--- a/ft_memcmp.c
+++ b/ft_memcmp.c
@@ -6,11 +6,10 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 09:56:44 by cacharle #+# #+# */
-/* Updated: 2019/10/20 13:00:29 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 01:58:42 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
-#include <string.h>
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
diff --git a/ft_memset.c b/ft_memset.c
index cd7616c..7963fd0 100644
--- a/ft_memset.c
+++ b/ft_memset.c
@@ -6,15 +6,26 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:01:23 by cacharle #+# #+# */
-/* Updated: 2019/11/20 03:22:29 by cacharle ### ########.fr */
+/* Updated: 2019/11/20 23:22:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
+#define BUF_TYPE long int
+
void *ft_memset(void *s, int c, size_t n)
{
- while (n-- > 0)
- *((t_byte*)s + n) = (t_byte)c;
+ BUF_TYPE buf;
+
+ c = (unsigned char)c;
+ while (n % 8 > 0)
+ *((t_byte*)s + --n) = (t_byte)c;
+ n /= 8;
+ buf = (BUF_TYPE)c | (BUF_TYPE)c << 8 | (BUF_TYPE)c << 16
+ | (BUF_TYPE)c << 24 | (BUF_TYPE)c << 32 | (BUF_TYPE)c << 40
+ | (BUF_TYPE)c << 48 | (BUF_TYPE)c << 56;
+ while (n > 0)
+ *((BUF_TYPE*)s + --n) = buf;
return (s);
}
diff --git a/ft_strcat.c b/ft_strcat.c
index e7289db..d5bc7e0 100644
--- a/ft_strcat.c
+++ b/ft_strcat.c
@@ -6,24 +6,14 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:09:41 by cacharle #+# #+# */
-/* Updated: 2019/10/07 10:39:51 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 01:03:38 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
+#include "libft.h"
+
char *ft_strcat(char *dest, const char *src)
{
- int i;
- int j;
-
- i = 0;
- while (dest[i])
- i++;
- j = 0;
- while (src[j])
- {
- dest[i + j] = src[j];
- j++;
- }
- dest[i + j] = '\0';
+ ft_memcpy(dest + ft_strlen(dest), src, ft_strlen(src) + 1);
return (dest);
}
diff --git a/ft_strchr.c b/ft_strchr.c
index 0ae161b..50bfc0a 100644
--- a/ft_strchr.c
+++ b/ft_strchr.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:14:47 by cacharle #+# #+# */
-/* Updated: 2019/11/20 04:02:02 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 01:04:52 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,13 +14,5 @@
char *ft_strchr(const char *s, int c)
{
- while (*s)
- {
- if (*s == (char)c)
- return ((char*)s);
- s++;
- }
- if ((char)c == '\0')
- return ((char*)s);
- return (NULL);
+ return (ft_memchr(s, c, ft_strlen(s) + 1));
}
diff --git a/ft_strclr.c b/ft_strclr.c
index c927a46..7e412fe 100644
--- a/ft_strclr.c
+++ b/ft_strclr.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:15:18 by cacharle #+# #+# */
-/* Updated: 2019/11/20 02:02:02 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 01:11:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,6 +16,5 @@ void ft_strclr(char *s)
{
if (s == NULL)
return ;
- while (*s)
- *s++ = '\0';
+ ft_bzero(s, ft_strlen(s));
}
diff --git a/ft_strcmp.c b/ft_strcmp.c
index df1a2e2..1978286 100644
--- a/ft_strcmp.c
+++ b/ft_strcmp.c
@@ -6,16 +6,13 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:16:07 by cacharle #+# #+# */
-/* Updated: 2019/10/07 10:16:19 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 01:58:27 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
+#include "libft.h"
+
int ft_strcmp(const char *s1, const char *s2)
{
- int i;
-
- i = 0;
- while (s1[i] && s2[i] && s1[i] == s2[i])
- i++;
- return ((unsigned char)s1[i] - (unsigned char)s2[i]);
+ return (ft_memcmp(s1, s2, ft_strlen(s1) + 1));
}
diff --git a/ft_strcpy.c b/ft_strcpy.c
index 6d94aef..9677b24 100644
--- a/ft_strcpy.c
+++ b/ft_strcpy.c
@@ -6,20 +6,13 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:38:36 by cacharle #+# #+# */
-/* Updated: 2019/10/07 10:39:03 by cacharle ### ########.fr */
+/* Updated: 2019/11/20 23:25:57 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
+#include "libft.h"
+
char *ft_strcpy(char *dest, const char *src)
{
- int i;
-
- i = 0;
- while (src[i])
- {
- dest[i] = src[i];
- i++;
- }
- dest[i] = '\0';
- return (dest);
+ return (ft_memcpy(dest, src, ft_strlen(src) + 1));
}
diff --git a/ft_strlen.c b/ft_strlen.c
index dc612a8..0e0a47c 100644
--- a/ft_strlen.c
+++ b/ft_strlen.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:32:48 by cacharle #+# #+# */
-/* Updated: 2019/11/20 03:31:28 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 01:45:42 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,10 +14,28 @@
size_t ft_strlen(const char *s)
{
- size_t counter;
+ unsigned long int *ptr;
+ const char *cpy;
- counter = 0;
- while (s[counter])
- counter++;
- return (counter);
+ ptr = (unsigned long int*)s;
+ while (TRUE)
+ {
+ cpy = (const char*)ptr++;
+ if (cpy[0] == '\0')
+ return (cpy - s);
+ if (cpy[1] == '\0')
+ return (cpy + 1 - s);
+ if (cpy[2] == '\0')
+ return (cpy + 2 - s);
+ if (cpy[3] == '\0')
+ return (cpy + 3 - s);
+ if (cpy[4] == '\0')
+ return (cpy + 4 - s);
+ if (cpy[5] == '\0')
+ return (cpy + 5 - s);
+ if (cpy[6] == '\0')
+ return (cpy + 6 - s);
+ if (cpy[7] == '\0')
+ return (cpy + 7 - s);
+ }
}
diff --git a/ft_strncmp.c b/ft_strncmp.c
index 46f79c2..cd303fc 100644
--- a/ft_strncmp.c
+++ b/ft_strncmp.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:27:34 by cacharle #+# #+# */
-/* Updated: 2019/11/20 03:33:59 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 01:56:32 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,10 +16,10 @@ int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
+ if (n == 0)
+ return (0);
i = 0;
- while (i < n && s1[i] == s2[i] && s1[i] && s2[i])
+ while (i + 1 < n && s1[i] == s2[i] && s1[i])
i++;
- if (i == n)
- i--;
- return (((unsigned char*)s1)[i] - ((unsigned char*)s2)[i]);
+ return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
diff --git a/ft_strncpy.c b/ft_strncpy.c
index 5f4c69f..3dbe2c9 100644
--- a/ft_strncpy.c
+++ b/ft_strncpy.c
@@ -6,22 +6,20 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:26:59 by cacharle #+# #+# */
-/* Updated: 2019/11/20 03:36:05 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 02:01:34 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
+
char *ft_strncpy(char *dest, const char *src, size_t n)
{
- size_t i;
+ size_t len;
- i = 0;
- while (src[i] && i < n)
- {
- dest[i] = src[i];
- i++;
- }
- ft_bzero(dest + i, n - i);
+ len = ft_strlen(src);
+ ft_memcpy(dest, src, MIN(n, len));
+ if (len < n)
+ ft_bzero(dest + len , n - len);
return (dest);
}
diff --git a/libft.h b/libft.h
index 6ac960b..2e1a2f2 100644
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */
-/* Updated: 2019/11/20 04:13:10 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 02:01:31 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,6 +22,9 @@
# define TRUE 1
# define FALSE 0
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+
typedef unsigned char t_byte;
/*