memmem.c 3.3 KB

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