xref: /reactos/sdk/lib/crt/mbstring/mbsncmp.c (revision c2c66aff)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/sdk/crt/mbstring/mbsncmp.c
5  * PURPOSE:     Compares two strings to a maximum of n bytes or characters
6  * PROGRAMER:   Ariadne
7  * UPDATE HISTORY:
8  *              12/04/99: Created
9  */
10 
11 #include <mbstring.h>
12 
13 /*
14  * @implemented
15  */
_mbsncmp(const unsigned char * str1,const unsigned char * str2,size_t n)16 int _mbsncmp(const unsigned char *str1, const unsigned char *str2, size_t n)
17 {
18 	unsigned char *s1 = (unsigned char *)str1;
19 	unsigned char *s2 = (unsigned char *)str2;
20 
21 	unsigned short *short_s1, *short_s2;
22 
23 	int l1, l2;
24 
25 	if (n == 0)
26 		return 0;
27 	do {
28 
29 		if (*s1 == 0)
30 			break;
31 
32 		l1 = _ismbblead(*s1);
33 		l2 = _ismbblead(*s2);
34 		if ( !l1 &&  !l2  ) {
35 
36 			if (*s1 != *s2)
37 				return *s1 - *s2;
38 			else {
39 				s1 += 1;
40 				s2 += 1;
41 				n--;
42 			}
43 		}
44 		else if ( l1 && l2 ){
45 			short_s1 = (unsigned short *)s1;
46 			short_s2 = (unsigned short *)s2;
47 			if ( *short_s1 != *short_s2 )
48 				return *short_s1 - *short_s2;
49 			else {
50 				s1 += 2;
51 				s2 += 2;
52 				n--;
53 
54 			}
55 		}
56 		else
57 			return *s1 - *s2;
58 	} while (n > 0);
59 	return 0;
60 }
61 
62 /*
63  * @implemented
64  */
_mbsnbcmp(const unsigned char * str1,const unsigned char * str2,size_t n)65 int _mbsnbcmp(const unsigned char *str1, const unsigned char *str2, size_t n)
66 {
67 	unsigned char *s1 = (unsigned char *)str1;
68 	unsigned char *s2 = (unsigned char *)str2;
69 
70 	unsigned short *short_s1, *short_s2;
71 
72 	int l1, l2;
73 
74 	if (n == 0)
75 		return 0;
76 	do {
77 
78 		if (*s1 == 0)
79 			break;
80 
81 		l1 = _ismbblead(*s1);
82 		l2 = _ismbblead(*s2);
83 		if ( !l1 &&  !l2  ) {
84 
85 			if (*s1 != *s2)
86 				return *s1 - *s2;
87 			else {
88 				s1 += 1;
89 				s2 += 1;
90 				n--;
91 			}
92 		}
93 		else if ( l1 && l2 ){
94 			short_s1 = (unsigned short *)s1;
95 			short_s2 = (unsigned short *)s2;
96 			if ( *short_s1 != *short_s2 )
97 				return *short_s1 - *short_s2;
98 			else {
99 				s1 += 2;
100 				s2 += 2;
101 				n-=2;
102 
103 			}
104 		}
105 		else
106 			return *s1 - *s2;
107 	} while (n > 0);
108 	return 0;
109 }
110