annotate src/strcmp.c @ 185:3d6bfa290dbd r20-3b19

Import from CVS: tag r20-3b19
author cvs
date Mon, 13 Aug 2007 09:55:28 +0200
parents 376386a54a3c
children 850242ba4a81
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #define MAGIC 0x7efefeff
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #define HIGH_BIT_P(c) ((c) & hi_bit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 strcmp (const char *x, const char *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 if (x == y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 else if (ALIGNED (x) && ALIGNED (y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 const unsigned long *x1 = (const unsigned long *) x;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 const unsigned long *y1 = (const unsigned long *) y;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 unsigned long c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 unsigned long magic = MAGIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 unsigned long not_magic = ~magic;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 unsigned long hi_bit = 0x80000000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 while ((c = *x1) == *y1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 if (HAS_ZERO(c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 if (!HIGH_BIT_P (c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 x = (const char *) x1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 y = (const char *) y1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 x1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 y1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 x = (const char *) x1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 y = (const char *) y1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 char c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 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 while ((c = *x) == *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 if (c == (char) 0) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 y++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 return (*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 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 strncmp (const char *x, const char *y, size_t n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 if ((x == y) || (n <= 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 else if (ALIGNED (x) && ALIGNED (y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 const unsigned long *x1 = (const unsigned long *) x;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 const unsigned long *y1 = (const unsigned long *) y;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 unsigned long c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 unsigned long magic = MAGIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 unsigned long not_magic = ~magic;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 unsigned long hi_bit = 0x80000000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 while ((c = *x1) == *y1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 n -= sizeof (unsigned long);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 if (n <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 if (HAS_ZERO(c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 if (!HIGH_BIT_P (c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 x = (const char *) x1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 y = (const char *) y1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 x1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 y1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 x = (const char *) x1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 y = (const char *) y1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 char c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 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 while ((c = *x) == *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 n--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 if (n <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 if (c == (char) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 y++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 return (*x - *y);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 }