New module 'streq'.
[gnulib.git] / lib / streq.h
1 /* Optimized string comparison.
2    Copyright (C) 2001-2002, 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU Library General Public License as published
6    by the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program 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 GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17    USA.  */
18
19 /* Written by Bruno Haible <bruno@clisp.org>.  */
20
21 #include <string.h>
22
23 /* STREQ allows to optimize string comparison with a small literal string.
24      STREQ (s, "EUC-KR", 'E', 'U', 'C', '-', 'K', 'R', 0, 0, 0)
25    is semantically equivalent to
26      strcmp (s, "EUC-KR") == 0
27    just faster.  */
28
29 /* Help GCC to generate good code for string comparisons with
30    immediate strings. */
31 #if defined (__GNUC__) && defined (__OPTIMIZE__)
32
33 static inline int
34 streq9 (const char *s1, const char *s2)
35 {
36   return strcmp (s1 + 9, s2 + 9) == 0;
37 }
38
39 static inline int
40 streq8 (const char *s1, const char *s2, char s28)
41 {
42   if (s1[8] == s28)
43     {
44       if (s28 == 0)
45         return 1;
46       else
47         return streq9 (s1, s2);
48     }
49   else
50     return 0;
51 }
52
53 static inline int
54 streq7 (const char *s1, const char *s2, char s27, char s28)
55 {
56   if (s1[7] == s27)
57     {
58       if (s27 == 0)
59         return 1;
60       else
61         return streq8 (s1, s2, s28);
62     }
63   else
64     return 0;
65 }
66
67 static inline int
68 streq6 (const char *s1, const char *s2, char s26, char s27, char s28)
69 {
70   if (s1[6] == s26)
71     {
72       if (s26 == 0)
73         return 1;
74       else
75         return streq7 (s1, s2, s27, s28);
76     }
77   else
78     return 0;
79 }
80
81 static inline int
82 streq5 (const char *s1, const char *s2, char s25, char s26, char s27, char s28)
83 {
84   if (s1[5] == s25)
85     {
86       if (s25 == 0)
87         return 1;
88       else
89         return streq6 (s1, s2, s26, s27, s28);
90     }
91   else
92     return 0;
93 }
94
95 static inline int
96 streq4 (const char *s1, const char *s2, char s24, char s25, char s26, char s27, char s28)
97 {
98   if (s1[4] == s24)
99     {
100       if (s24 == 0)
101         return 1;
102       else
103         return streq5 (s1, s2, s25, s26, s27, s28);
104     }
105   else
106     return 0;
107 }
108
109 static inline int
110 streq3 (const char *s1, const char *s2, char s23, char s24, char s25, char s26, char s27, char s28)
111 {
112   if (s1[3] == s23)
113     {
114       if (s23 == 0)
115         return 1;
116       else
117         return streq4 (s1, s2, s24, s25, s26, s27, s28);
118     }
119   else
120     return 0;
121 }
122
123 static inline int
124 streq2 (const char *s1, const char *s2, char s22, char s23, char s24, char s25, char s26, char s27, char s28)
125 {
126   if (s1[2] == s22)
127     {
128       if (s22 == 0)
129         return 1;
130       else
131         return streq3 (s1, s2, s23, s24, s25, s26, s27, s28);
132     }
133   else
134     return 0;
135 }
136
137 static inline int
138 streq1 (const char *s1, const char *s2, char s21, char s22, char s23, char s24, char s25, char s26, char s27, char s28)
139 {
140   if (s1[1] == s21)
141     {
142       if (s21 == 0)
143         return 1;
144       else
145         return streq2 (s1, s2, s22, s23, s24, s25, s26, s27, s28);
146     }
147   else
148     return 0;
149 }
150
151 static inline int
152 streq0 (const char *s1, const char *s2, char s20, char s21, char s22, char s23, char s24, char s25, char s26, char s27, char s28)
153 {
154   if (s1[0] == s20)
155     {
156       if (s20 == 0)
157         return 1;
158       else
159         return streq1 (s1, s2, s21, s22, s23, s24, s25, s26, s27, s28);
160     }
161   else
162     return 0;
163 }
164
165 #define STREQ(s1,s2,s20,s21,s22,s23,s24,s25,s26,s27,s28) \
166   streq0 (s1, s2, s20, s21, s22, s23, s24, s25, s26, s27, s28)
167
168 #else
169
170 #define STREQ(s1,s2,s20,s21,s22,s23,s24,s25,s26,s27,s28) \
171   (strcmp (s1, s2) == 0)
172
173 #endif