annotate src/strcmp.c @ 335:54f7aa390f4f r21-0-65

Import from CVS: tag r21-0-65
author cvs
date Mon, 13 Aug 2007 10:50:39 +0200
parents 850242ba4a81
children 74fd4e045ea6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 /* In SunOS 4.1.1 the strcmp and strncmp functions reference memory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 past the last byte of the string! This will core dump if the memory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 following the last byte is not mapped.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 Here are correct versions by hbs@lucid.com.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
27 # include <config.h>
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
28 # ifndef REGISTER /* Strictly enforced in 20.3 */
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
29 # define REGISTER
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
30 # endif
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
31
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #define MAGIC 0x7efefeff
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 #define HIGH_BIT_P(c) ((c) & hi_bit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
39 /* CONST IS LOSING, but const is part of the interface of strcmp */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 strcmp (const char *x, const char *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 if (x == y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 else if (ALIGNED (x) && ALIGNED (y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
47 CONST unsigned long *x1 = (CONST unsigned long *) x;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
48 CONST unsigned long *y1 = (CONST unsigned long *) y;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 unsigned long c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 unsigned long magic = MAGIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 unsigned long not_magic = ~magic;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 unsigned long hi_bit = 0x80000000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 while ((c = *x1) == *y1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 if (HAS_ZERO(c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 if (!HIGH_BIT_P (c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
62 x = (CONST char *) x1;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
63 y = (CONST char *) y1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 x1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 y1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
72 x = (CONST char *) x1;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
73 y = (CONST char *) y1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 char c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 slow_loop:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 while ((c = *x) == *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 if (c == (char) 0) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 y++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 return (*x - *y);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 int
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
94 strncmp (CONST char *x, CONST char *y, size_t n)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 if ((x == y) || (n <= 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 else if (ALIGNED (x) && ALIGNED (y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
100 CONST unsigned long *x1 = (CONST unsigned long *) x;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
101 CONST unsigned long *y1 = (CONST unsigned long *) y;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 unsigned long c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 unsigned long magic = MAGIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 unsigned long not_magic = ~magic;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 unsigned long hi_bit = 0x80000000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 while ((c = *x1) == *y1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 n -= sizeof (unsigned long);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 if (n <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 if (HAS_ZERO(c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 if (!HIGH_BIT_P (c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
119 x = (CONST char *) x1;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
120 y = (CONST char *) y1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 x1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 y1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
129 x = (CONST char *) x1;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 0
diff changeset
130 y = (CONST char *) y1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 char c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 slow_loop:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 while ((c = *x) == *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 n--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 if (n <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 if (c == (char) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 y++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 return (*x - *y);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 }