strstr.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Return the offset of one string within another.
  2. Copyright (C) 1994-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _LIBC
  16. # include <config.h>
  17. #endif
  18. #include <string.h>
  19. #define RETURN_TYPE char *
  20. #define AVAILABLE(h, h_l, j, n_l) \
  21. (((j) + (n_l) <= (h_l)) \
  22. || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
  23. (j) + (n_l) <= (h_l)))
  24. #include "str-two-way.h"
  25. #undef strstr
  26. #ifndef STRSTR
  27. #define STRSTR strstr
  28. #endif
  29. static inline char *
  30. strstr2 (const unsigned char *hs, const unsigned char *ne)
  31. {
  32. uint32_t h1 = (ne[0] << 16) | ne[1];
  33. uint32_t h2 = 0;
  34. for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
  35. h2 = (h2 << 16) | c;
  36. return h1 == h2 ? (char *)hs - 2 : NULL;
  37. }
  38. static inline char *
  39. strstr3 (const unsigned char *hs, const unsigned char *ne)
  40. {
  41. uint32_t h1 = ((uint32_t)ne[0] << 24) | (ne[1] << 16) | (ne[2] << 8);
  42. uint32_t h2 = 0;
  43. for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
  44. h2 = (h2 | c) << 8;
  45. return h1 == h2 ? (char *)hs - 3 : NULL;
  46. }
  47. /* Hash character pairs so a small shift table can be used. All bits of
  48. p[0] are included, but not all bits from p[-1]. So if two equal hashes
  49. match on p[-1], p[0] matches too. Hash collisions are harmless and result
  50. in smaller shifts. */
  51. #define hash2(p) (((size_t)(p)[0] - ((size_t)(p)[-1] << 3)) % sizeof (shift))
  52. /* Fast strstr algorithm with guaranteed linear-time performance.
  53. Small needles up to size 3 use a dedicated linear search. Longer needles
  54. up to size 256 use a novel modified Horspool algorithm. It hashes pairs
  55. of characters to quickly skip past mismatches. The main search loop only
  56. exits if the last 2 characters match, avoiding unnecessary calls to memcmp
  57. and allowing for a larger skip if there is no match. A self-adapting
  58. filtering check is used to quickly detect mismatches in long needles.
  59. By limiting the needle length to 256, the shift table can be reduced to 8
  60. bits per entry, lowering preprocessing overhead and minimizing cache effects.
  61. The limit also implies worst-case performance is linear.
  62. Needles larger than 256 characters use the linear-time Two-Way algorithm. */
  63. char *
  64. STRSTR (const char *haystack, const char *needle)
  65. {
  66. const unsigned char *hs = (const unsigned char *) haystack;
  67. const unsigned char *ne = (const unsigned char *) needle;
  68. /* Handle short needle special cases first. */
  69. if (ne[0] == '\0')
  70. return (char *)hs;
  71. hs = (const unsigned char *)strchr ((const char*)hs, ne[0]);
  72. if (hs == NULL || ne[1] == '\0')
  73. return (char*)hs;
  74. if (ne[2] == '\0')
  75. return strstr2 (hs, ne);
  76. if (ne[3] == '\0')
  77. return strstr3 (hs, ne);
  78. /* Ensure haystack length is at least as long as needle length.
  79. Since a match may occur early on in a huge haystack, use strnlen
  80. and read ahead a few cachelines for improved performance. */
  81. size_t ne_len = strlen ((const char*)ne);
  82. size_t hs_len = __strnlen ((const char*)hs, ne_len | 512);
  83. if (hs_len < ne_len)
  84. return NULL;
  85. /* Check whether we have a match. This improves performance since we
  86. avoid initialization overheads. */
  87. if (memcmp (hs, ne, ne_len) == 0)
  88. return (char *) hs;
  89. /* Use Two-Way algorithm for very long needles. */
  90. if (__glibc_unlikely (ne_len > 256))
  91. return two_way_long_needle (hs, hs_len, ne, ne_len);
  92. const unsigned char *end = hs + hs_len - ne_len;
  93. uint8_t shift[256];
  94. size_t tmp, shift1;
  95. size_t m1 = ne_len - 1;
  96. size_t offset = 0;
  97. /* Initialize bad character shift hash table. */
  98. memset (shift, 0, sizeof (shift));
  99. for (int i = 1; i < m1; i++)
  100. shift[hash2 (ne + i)] = i;
  101. /* Shift1 is the amount we can skip after matching the hash of the
  102. needle end but not the full needle. */
  103. shift1 = m1 - shift[hash2 (ne + m1)];
  104. shift[hash2 (ne + m1)] = m1;
  105. while (1)
  106. {
  107. if (__glibc_unlikely (hs > end))
  108. {
  109. end += __strnlen ((const char*)end + m1 + 1, 2048);
  110. if (hs > end)
  111. return NULL;
  112. }
  113. /* Skip past character pairs not in the needle. */
  114. do
  115. {
  116. hs += m1;
  117. tmp = shift[hash2 (hs)];
  118. }
  119. while (tmp == 0 && hs <= end);
  120. /* If the match is not at the end of the needle, shift to the end
  121. and continue until we match the hash of the needle end. */
  122. hs -= tmp;
  123. if (tmp < m1)
  124. continue;
  125. /* Hash of the last 2 characters matches. If the needle is long,
  126. try to quickly filter out mismatches. */
  127. if (m1 < 15 || memcmp (hs + offset, ne + offset, 8) == 0)
  128. {
  129. if (memcmp (hs, ne, m1) == 0)
  130. return (void *) hs;
  131. /* Adjust filter offset when it doesn't find the mismatch. */
  132. offset = (offset >= 8 ? offset : m1) - 8;
  133. }
  134. /* Skip based on matching the hash of the needle end. */
  135. hs += shift1;
  136. }
  137. }
  138. libc_hidden_builtin_def (strstr)