strstr.c 3.5 KB

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