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