strstr.c 3.5 KB

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