Initial git import
[sxemacs] / src / strcmp.c
1 /*
2 This file is part of SXEmacs
3
4 SXEmacs is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 SXEmacs is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 /* Synched up with: Not in FSF. */
19
20 /* In SunOS 4.1.1 the strcmp and strncmp functions reference memory
21    past the last byte of the string! This will core dump if the memory 
22    following the last byte is not mapped.
23
24    Here are correct versions by hbs@lucid.com.
25 */
26
27 # include <config.h>
28 # ifndef REGISTER               /* Strictly enforced in 20.3 */
29 # define REGISTER
30 # endif
31
32 #include <string.h>
33 #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
34
35 #define MAGIC    0x7efefeff
36 #define HIGH_BIT_P(c) ((c) & hi_bit)
37 #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
38
39 int strcmp(const char *x, const char *y)
40 {
41         if (x == y)
42                 return 0;
43         else if (ALIGNED(x) && ALIGNED(y)) {
44                 const unsigned long *x1 = (const unsigned long *)x;
45                 const unsigned long *y1 = (const unsigned long *)y;
46                 unsigned long c;
47                 unsigned long magic = MAGIC;
48                 unsigned long not_magic = ~magic;
49                 unsigned long hi_bit = 0x80000000;
50
51                 while ((c = *x1) == *y1) {
52                         if (HAS_ZERO(c)) {
53                                 if (!HIGH_BIT_P(c))
54                                         return 0;
55                                 else {
56                                         x = (const char *)x1;
57                                         y = (const char *)y1;
58                                         goto slow_loop;
59                                 }
60                         }
61
62                         x1++;
63                         y1++;
64                 }
65
66                 x = (const char *)x1;
67                 y = (const char *)y1;
68                 goto slow_loop;
69         } else {
70                 char c;
71
72               slow_loop:
73
74                 while ((c = *x) == *y) {
75                         if (c == (char)0)
76                                 return 0;
77                         x++;
78                         y++;
79                 }
80                 return (*x - *y);
81         }
82 }
83
84 int strncmp(const char *x, const char *y, size_t n)
85 {
86         if ((x == y) || (n <= 0))
87                 return 0;
88         else if (ALIGNED(x) && ALIGNED(y)) {
89                 const unsigned long *x1 = (const unsigned long *)x;
90                 const unsigned long *y1 = (const unsigned long *)y;
91                 unsigned long c;
92                 unsigned long magic = MAGIC;
93                 unsigned long not_magic = ~magic;
94                 unsigned long hi_bit = 0x80000000;
95
96                 while ((c = *x1) == *y1) {
97                         n -= sizeof(unsigned long);
98                         if (n <= 0)
99                                 return 0;
100
101                         if (HAS_ZERO(c)) {
102                                 if (!HIGH_BIT_P(c))
103                                         return 0;
104                                 else {
105                                         x = (const char *)x1;
106                                         y = (const char *)y1;
107                                         goto slow_loop;
108                                 }
109                         }
110
111                         x1++;
112                         y1++;
113                 }
114
115                 x = (const char *)x1;
116                 y = (const char *)y1;
117                 goto slow_loop;
118         } else {
119                 char c;
120
121               slow_loop:
122
123                 while ((c = *x) == *y) {
124                         n--;
125                         if (n <= 0)
126                                 return 0;
127                         if (c == (char)0)
128                                 return 0;
129                         x++;
130                         y++;
131                 }
132                 return (*x - *y);
133         }
134 }