summaryrefslogtreecommitdiff
path: root/ft_memcmp.c
diff options
context:
space:
mode:
authoryctct <yctct@yctct.com>2026-02-05 11:56:49 +0100
committeryctct <yctct@yctct.com>2026-02-05 11:56:49 +0100
commit861a36d86e7587c043131228cf3a08afd7bc43ad (patch)
treea931df9f1845250c0e93adeda52f139c2f5ee1b0 /ft_memcmp.c
Add files, first commit
Diffstat (limited to 'ft_memcmp.c')
-rw-r--r--ft_memcmp.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/ft_memcmp.c b/ft_memcmp.c
new file mode 100644
index 0000000..32f8eec
--- /dev/null
+++ b/ft_memcmp.c
@@ -0,0 +1,122 @@
+/*
+Libft Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include "libft.h"
+
+int ft_memcmp(const void *s1, const void *s2, size_t n)
+{
+ unsigned char *cs1;
+ unsigned char *cs2;
+ size_t i;
+
+ cs1 = (unsigned char *)s1;
+ cs2 = (unsigned char *)s2;
+ i = 0;
+ if (n == 0)
+ return (0);
+ while (i < n - 1 && cs1[i] == cs2[i])
+ i++;
+ return (cs1[i] - cs2[i]);
+}
+
+/*
+#include <string.h>
+
+int main(void)
+{
+ char s[] = {-128, 0, 127, 0};
+ char sCpy[] = {-128, 0, 127, 0};
+
+ printf("%i\n", ft_memcmp(s, sCpy, 4));
+ printf("%i\n", memcmp(s, sCpy, 4));
+ printf("%i\n", ft_memcmp("abcdefghij", "abcdefgxyz", 7));
+ printf("%i\n", memcmp("abcdefghij", "abcdefgxyz", 7));
+}
+int ft_memcmp(const void *s1, const void *s2, size_t n)
+{
+ size_t i;
+ unsigned char *a;
+ unsigned char *b;
+
+ i = 0;
+ a = (unsigned char *)s1;
+ b = (unsigned char *)s2;
+ if (s1 == s2 || n == 0)
+ return (0);
+ while (*a == *b && i < n - 1)
+ {
+ a++;
+ b++;
+ i++;
+ }
+ return (*a - *b);
+}
+*/
+/*
+int main(void)
+{
+ char s[] = {-128, 0, 127, 0};
+ char sCpy[] = {-128, 0, 127, 0};
+ char s2[] = {0, 0, 127, 0};
+ char s3[] = {0, 0, 42, 0};
+
+ printf("\n");
+ printf("%i\n", ft_memcmp(s, sCpy, 4));
+ printf("%i\n", memcmp(s, sCpy, 4));
+ printf("\n");
+ printf("%i\n", ft_memcmp(s, s2, 0));
+ printf("%i\n", memcmp(s, s2, 0));
+ printf("\n");
+ printf("%i\n", ft_memcmp(s, s2, 1));
+ printf("%i\n", memcmp(s, s2, 1));
+ printf("\n");
+ printf("%i\n", ft_memcmp(s2, s, 1));
+ printf("%i\n", memcmp(s2, s, 1));
+ printf("\n");
+ printf("%i\n", ft_memcmp(s2, s3, 4));
+ printf("%i\n", memcmp(s2, s3, 4));
+ printf("\n");
+ printf("\n");
+ printf("%i\n", ft_memcmp("salut", "salut", 5));
+ printf("%i\n", memcmp("salut", "salut", 5));
+ printf("\n");
+ printf("%i\n", ft_memcmp("t\200", "t\0", 2));
+ printf("%i\n", memcmp("t\200", "t\0", 2));
+ printf("\n");
+ printf("%i\n", ft_memcmp("testss", "test", 5));
+ printf("%i\n", memcmp("testss", "test", 5));
+ printf("\n");
+ printf("%i\n", ft_memcmp("test", "tEst", 4));
+ printf("%i\n", memcmp("test", "tEst", 4));
+ printf("\n");
+ printf("%i\n", ft_memcmp("", "test", 4));
+ printf("%i\n", memcmp("", "test", 4));
+ printf("\n");
+ printf("%i\n", ft_memcmp("test", "", 4));
+ printf("%i\n", memcmp("test", "", 4));
+ printf("\n");
+ printf("%i\n", ft_memcmp("abcdefghij", "abcdefgxyz", 7));
+ printf("%i\n", memcmp("abcdefghij", "abcdefgxyz", 7));
+ printf("\n");
+ printf("%i\n", ft_memcmp("abcdefgh", "abcdwxyz", 6));
+ printf("%i\n", memcmp("abcdefgh", "abcdwxyz", 6));
+ printf("\n");
+ printf("%i\n", ft_memcmp("zyxbcdefgh", "abcdefgxyz", 0));
+ printf("%i\n", memcmp("zyxbcdefgh", "abcdefgxyz", 0));
+ return (0);
+}
+*/