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