summaryrefslogtreecommitdiff
path: root/ft_memcmp.c
blob: 32f8eecc24a857089663869b3cee3d61f41e86c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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);
}
*/