streq.h 3.9 KB

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