strstr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
  5. {
  6. uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
  7. for (h++; *h && hw != nw; hw = hw<<8 | *++h);
  8. return *h ? (char *)h-1 : 0;
  9. }
  10. static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
  11. {
  12. uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
  13. uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
  14. for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
  15. return *h ? (char *)h-2 : 0;
  16. }
  17. static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
  18. {
  19. uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
  20. uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
  21. for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
  22. return *h ? (char *)h-3 : 0;
  23. }
  24. #if 0
  25. static char *naive_strstr(const char *h, const char *n)
  26. {
  27. size_t i;
  28. for (i=0; n[i] && h[i]; i++)
  29. for ( ; n[i] != h[i]; h++, i=0);
  30. return n[i] ? 0 : (char *)h;
  31. }
  32. #endif
  33. #define MAX(a,b) ((a)>(b)?(a):(b))
  34. #define MIN(a,b) ((a)<(b)?(a):(b))
  35. #define BITOP(a,b,op) \
  36. ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
  37. static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
  38. {
  39. const unsigned char *z;
  40. size_t l, ip, jp, k, p, ms, p0, mem, mem0;
  41. size_t byteset[32 / sizeof(size_t)] = { 0 };
  42. size_t shift[256];
  43. /* Computing length of needle and fill shift table */
  44. for (l=0; n[l] && h[l]; l++)
  45. BITOP(byteset, n[l], |=), shift[n[l]] = l+1;
  46. if (n[l]) return 0; /* hit the end of h */
  47. /* Compute maximal suffix */
  48. ip = -1; jp = 0; k = p = 1;
  49. while (jp+k<l) {
  50. if (n[ip+k] == n[jp+k]) {
  51. if (k == p) {
  52. jp += p;
  53. k = 1;
  54. } else k++;
  55. } else if (n[ip+k] > n[jp+k]) {
  56. jp += k;
  57. k = 1;
  58. p = jp - ip;
  59. } else {
  60. ip = jp++;
  61. k = p = 1;
  62. }
  63. }
  64. ms = ip;
  65. p0 = p;
  66. /* And with the opposite comparison */
  67. ip = -1; jp = 0; k = p = 1;
  68. while (jp+k<l) {
  69. if (n[ip+k] == n[jp+k]) {
  70. if (k == p) {
  71. jp += p;
  72. k = 1;
  73. } else k++;
  74. } else if (n[ip+k] < n[jp+k]) {
  75. jp += k;
  76. k = 1;
  77. p = jp - ip;
  78. } else {
  79. ip = jp++;
  80. k = p = 1;
  81. }
  82. }
  83. if (ip+1 > ms+1) ms = ip;
  84. else p = p0;
  85. /* Periodic needle? */
  86. if (memcmp(n, n+p, ms+1)) {
  87. mem0 = 0;
  88. p = MAX(ms, l-ms-1) + 1;
  89. } else mem0 = l-p;
  90. mem = 0;
  91. /* Initialize incremental end-of-haystack pointer */
  92. z = h;
  93. /* Search loop */
  94. for (;;) {
  95. /* Update incremental end-of-haystack pointer */
  96. if (z-h < l) {
  97. /* Fast estimate for MIN(l,63) */
  98. size_t grow = l | 63;
  99. const char *z2 = memchr(z, 0, grow);
  100. if (z2) {
  101. z = z2;
  102. if (z-h < l) return 0;
  103. } else z += grow;
  104. }
  105. /* Check last byte first; advance by shift on mismatch */
  106. if (BITOP(byteset, h[l-1], &)) {
  107. k = l-shift[h[l-1]];
  108. //printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l);
  109. if (k) {
  110. if (mem0 && mem && k < p) k = l-p;
  111. h += k;
  112. mem = 0;
  113. continue;
  114. }
  115. } else {
  116. h += l;
  117. mem = 0;
  118. continue;
  119. }
  120. /* Compare right half */
  121. for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
  122. if (n[k]) {
  123. h += k-ms;
  124. mem = 0;
  125. continue;
  126. }
  127. /* Compare left half */
  128. for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
  129. if (k == mem) return (char *)h;
  130. h += p;
  131. mem = mem0;
  132. }
  133. }
  134. char *strstr(const char *h, const char *n)
  135. {
  136. /* Return immediately on empty needle */
  137. if (!n[0]) return (char *)h;
  138. /* Use faster algorithms for short needles */
  139. h = strchr(h, *n);
  140. if (!h || !n[1]) return (char *)h;
  141. if (!h[1]) return 0;
  142. if (!n[2]) return twobyte_strstr(h, n);
  143. if (!h[2]) return 0;
  144. if (!n[3]) return threebyte_strstr(h, n);
  145. if (!h[3]) return 0;
  146. if (!n[4]) return fourbyte_strstr(h, n);
  147. return twoway_strstr(h, n);
  148. }