Back to index

nagios-plugins  1.4.16
streq.h
Go to the documentation of this file.
00001 /* Optimized string comparison.
00002    Copyright (C) 2001-2002, 2007, 2009-2010 Free Software Foundation, Inc.
00003 
00004    This program is free software: you can redistribute it and/or modify it
00005    under the terms of the GNU General Public License as published
00006    by the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00016 
00017 /* Written by Bruno Haible <bruno@clisp.org>.  */
00018 
00019 #ifndef _GL_STREQ_H
00020 #define _GL_STREQ_H
00021 
00022 #include <string.h>
00023 
00024 /* STREQ allows to optimize string comparison with a small literal string.
00025      STREQ (s, "EUC-KR", 'E', 'U', 'C', '-', 'K', 'R', 0, 0, 0)
00026    is semantically equivalent to
00027      strcmp (s, "EUC-KR") == 0
00028    just faster.  */
00029 
00030 /* Help GCC to generate good code for string comparisons with
00031    immediate strings. */
00032 #if defined (__GNUC__) && defined (__OPTIMIZE__)
00033 
00034 static inline int
00035 streq9 (const char *s1, const char *s2)
00036 {
00037   return strcmp (s1 + 9, s2 + 9) == 0;
00038 }
00039 
00040 static inline int
00041 streq8 (const char *s1, const char *s2, char s28)
00042 {
00043   if (s1[8] == s28)
00044     {
00045       if (s28 == 0)
00046         return 1;
00047       else
00048         return streq9 (s1, s2);
00049     }
00050   else
00051     return 0;
00052 }
00053 
00054 static inline int
00055 streq7 (const char *s1, const char *s2, char s27, char s28)
00056 {
00057   if (s1[7] == s27)
00058     {
00059       if (s27 == 0)
00060         return 1;
00061       else
00062         return streq8 (s1, s2, s28);
00063     }
00064   else
00065     return 0;
00066 }
00067 
00068 static inline int
00069 streq6 (const char *s1, const char *s2, char s26, char s27, char s28)
00070 {
00071   if (s1[6] == s26)
00072     {
00073       if (s26 == 0)
00074         return 1;
00075       else
00076         return streq7 (s1, s2, s27, s28);
00077     }
00078   else
00079     return 0;
00080 }
00081 
00082 static inline int
00083 streq5 (const char *s1, const char *s2, char s25, char s26, char s27, char s28)
00084 {
00085   if (s1[5] == s25)
00086     {
00087       if (s25 == 0)
00088         return 1;
00089       else
00090         return streq6 (s1, s2, s26, s27, s28);
00091     }
00092   else
00093     return 0;
00094 }
00095 
00096 static inline int
00097 streq4 (const char *s1, const char *s2, char s24, char s25, char s26, char s27, char s28)
00098 {
00099   if (s1[4] == s24)
00100     {
00101       if (s24 == 0)
00102         return 1;
00103       else
00104         return streq5 (s1, s2, s25, s26, s27, s28);
00105     }
00106   else
00107     return 0;
00108 }
00109 
00110 static inline int
00111 streq3 (const char *s1, const char *s2, char s23, char s24, char s25, char s26, char s27, char s28)
00112 {
00113   if (s1[3] == s23)
00114     {
00115       if (s23 == 0)
00116         return 1;
00117       else
00118         return streq4 (s1, s2, s24, s25, s26, s27, s28);
00119     }
00120   else
00121     return 0;
00122 }
00123 
00124 static inline int
00125 streq2 (const char *s1, const char *s2, char s22, char s23, char s24, char s25, char s26, char s27, char s28)
00126 {
00127   if (s1[2] == s22)
00128     {
00129       if (s22 == 0)
00130         return 1;
00131       else
00132         return streq3 (s1, s2, s23, s24, s25, s26, s27, s28);
00133     }
00134   else
00135     return 0;
00136 }
00137 
00138 static inline int
00139 streq1 (const char *s1, const char *s2, char s21, char s22, char s23, char s24, char s25, char s26, char s27, char s28)
00140 {
00141   if (s1[1] == s21)
00142     {
00143       if (s21 == 0)
00144         return 1;
00145       else
00146         return streq2 (s1, s2, s22, s23, s24, s25, s26, s27, s28);
00147     }
00148   else
00149     return 0;
00150 }
00151 
00152 static inline int
00153 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)
00154 {
00155   if (s1[0] == s20)
00156     {
00157       if (s20 == 0)
00158         return 1;
00159       else
00160         return streq1 (s1, s2, s21, s22, s23, s24, s25, s26, s27, s28);
00161     }
00162   else
00163     return 0;
00164 }
00165 
00166 #define STREQ(s1,s2,s20,s21,s22,s23,s24,s25,s26,s27,s28) \
00167   streq0 (s1, s2, s20, s21, s22, s23, s24, s25, s26, s27, s28)
00168 
00169 #else
00170 
00171 #define STREQ(s1,s2,s20,s21,s22,s23,s24,s25,s26,s27,s28) \
00172   (strcmp (s1, s2) == 0)
00173 
00174 #endif
00175 
00176 #endif /* _GL_STREQ_H */