memmem.c 3.3 KB

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