annotate src/strcmp.c @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents 850242ba4a81
children 697ef44129c6
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 strcmp (const char *x, const char *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 if (x == y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 else if (ALIGNED (x) && ALIGNED (y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
46 const unsigned long *x1 = (const unsigned long *) x;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
47 const unsigned long *y1 = (const unsigned long *) y;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 unsigned long c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 unsigned long magic = MAGIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 unsigned long not_magic = ~magic;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 unsigned long hi_bit = 0x80000000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 while ((c = *x1) == *y1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 if (HAS_ZERO(c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 if (!HIGH_BIT_P (c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
61 x = (const char *) x1;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
62 y = (const char *) y1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 goto slow_loop;
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 x1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 y1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
71 x = (const char *) x1;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
72 y = (const char *) y1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 char c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 slow_loop:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 while ((c = *x) == *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 if (c == (char) 0) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 y++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 return (*x - *y);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 }
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 int
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
93 strncmp (const char *x, const char *y, size_t n)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 if ((x == y) || (n <= 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 else if (ALIGNED (x) && ALIGNED (y))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
99 const unsigned long *x1 = (const unsigned long *) x;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
100 const unsigned long *y1 = (const unsigned long *) y;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 unsigned long c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 unsigned long magic = MAGIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 unsigned long not_magic = ~magic;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 unsigned long hi_bit = 0x80000000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 while ((c = *x1) == *y1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 n -= sizeof (unsigned long);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 if (n <= 0)
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 if (HAS_ZERO(c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 if (!HIGH_BIT_P (c))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
118 x = (const char *) x1;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
119 y = (const char *) y1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 goto slow_loop;
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 x1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 y1++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
128 x = (const char *) x1;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 203
diff changeset
129 y = (const char *) y1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 goto slow_loop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 char c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 slow_loop:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 while ((c = *x) == *y)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 n--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 if (n <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 if (c == (char) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 y++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 return (*x - *y);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 }