su_bm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@internal
  25. * @file su_bm.c
  26. * @brief Search with Boyer-Moore algorithm
  27. *
  28. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  29. *
  30. * @date Created: Mon Apr 11 16:35:16 2005 ppessi
  31. *
  32. */
  33. #include "config.h"
  34. #include <sofia-sip/su_bm.h>
  35. #include <sys/types.h>
  36. #include <stddef.h>
  37. #include <limits.h>
  38. #include <ctype.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #ifndef TORTURELOG
  42. #define TORTURELOG(x) (void)0
  43. #endif
  44. struct bw_fwd_table {
  45. unsigned char table[UCHAR_MAX + 1];
  46. };
  47. /** Build forward skip table #bm_fwd_table_t for Boyer-Moore algorithm. */
  48. static
  49. bm_fwd_table_t *
  50. bm_memmem_study0(char const *needle, size_t nlen, bm_fwd_table_t *fwd)
  51. {
  52. size_t i;
  53. if (nlen >= UCHAR_MAX) {
  54. needle += nlen - UCHAR_MAX;
  55. nlen = UCHAR_MAX;
  56. }
  57. memset(&fwd->table, (unsigned char)nlen, sizeof fwd->table);
  58. for (i = 0; i < nlen; i++) {
  59. fwd->table[(unsigned short)needle[i]] = (unsigned char)(nlen - i - 1);
  60. }
  61. return fwd;
  62. }
  63. /** @defgroup su_bm Fast string searching with Boyer-Moore algorithm
  64. *
  65. * The Boyer-Moore algorithm is used to implement fast substring search. The
  66. * algorithm has some overhead caused by filling a table. Substring search
  67. * then requires at most 1 / substring-length less string comparisons. On
  68. * modern desktop hardware, Boyer-Moore algorithm is seldom faster than the
  69. * naive implementation if the searched substring is shorter than the cache
  70. * line.
  71. *
  72. */
  73. /**@ingroup su_bm
  74. * @typedef struct bw_fwd_table bm_fwd_table_t;
  75. *
  76. * Forward skip table for Boyer-Moore algorithm.
  77. *
  78. */
  79. /** Build case-sensitive forward skip table #bm_fwd_table_t
  80. * for Boyer-Moore algorithm.
  81. * @ingroup su_bm
  82. */
  83. bm_fwd_table_t *
  84. bm_memmem_study(char const *needle, size_t nlen)
  85. {
  86. bm_fwd_table_t *fwd = malloc(sizeof *fwd);
  87. if (fwd)
  88. bm_memmem_study0(needle, nlen, fwd);
  89. return fwd;
  90. }
  91. /** Search for a substring using Boyer-Moore algorithm.
  92. * @ingroup su_bm
  93. */
  94. char *
  95. bm_memmem(char const *haystack, size_t hlen,
  96. char const *needle, size_t nlen,
  97. bm_fwd_table_t *fwd)
  98. {
  99. size_t i, j;
  100. bm_fwd_table_t fwd0[1];
  101. if (nlen == 0)
  102. return (char *)haystack;
  103. if (needle == NULL || haystack == NULL || nlen > hlen)
  104. return NULL;
  105. if (nlen == 1) {
  106. for (i = 0; i < hlen; i++)
  107. if (haystack[i] == needle[0])
  108. return (char *)haystack + i;
  109. return NULL;
  110. }
  111. if (!fwd)
  112. fwd = bm_memmem_study0(needle, nlen, fwd0);
  113. for (i = j = nlen - 1; i < hlen;) {
  114. unsigned char h = haystack[i];
  115. if (h == needle[j]) {
  116. TORTURELOG(("match \"%s\" at %u\nwith %*s\"%.*s*%s\": %s\n",
  117. haystack, (unsigned)i,
  118. (int)(i - j), "", (int)j, needle, needle + j + 1,
  119. j == 0 ? "match!" : "back by 1"));
  120. if (j == 0)
  121. return (char *)haystack + i;
  122. i--, j--;
  123. }
  124. else {
  125. if (fwd->table[h] > nlen - j) {
  126. TORTURELOG(("match \"%s\" at %u\n"
  127. "last %*s\"%.*s*%s\": (by %u)\n",
  128. haystack, (unsigned)i,
  129. (int)(i - j), "",
  130. (int)j, needle, needle + j + 1, fwd->table[h]));
  131. i += fwd->table[h];
  132. }
  133. else {
  134. TORTURELOG(("match \"%s\" at %u\n"
  135. "2nd %*s\"%.*s*%s\": (by %u)\n",
  136. haystack, (unsigned)i,
  137. (int)(i - j), "",
  138. (int)j, needle, needle + j + 1, (unsigned)(nlen - j)));
  139. i += nlen - j;
  140. }
  141. j = nlen - 1;
  142. }
  143. }
  144. return NULL;
  145. }
  146. /** Build forward skip table for Boyer-Moore algorithm */
  147. static
  148. bm_fwd_table_t *
  149. bm_memcasemem_study0(char const *needle, size_t nlen, bm_fwd_table_t *fwd)
  150. {
  151. size_t i;
  152. if (nlen >= UCHAR_MAX) {
  153. needle += nlen - UCHAR_MAX;
  154. nlen = UCHAR_MAX;
  155. }
  156. for (i = 0; i < UCHAR_MAX; i++)
  157. fwd->table[i] = (unsigned char)nlen;
  158. for (i = 0; i < nlen; i++) {
  159. unsigned char n = tolower((const unsigned char)needle[i]);
  160. fwd->table[n] = (unsigned char)(nlen - i - 1);
  161. }
  162. return fwd;
  163. }
  164. /** Build case-insensitive forward skip table for Boyer-Moore algorithm.
  165. * @ingroup su_bm
  166. */
  167. bm_fwd_table_t *
  168. bm_memcasemem_study(char const *needle, size_t nlen)
  169. {
  170. bm_fwd_table_t *fwd = malloc(sizeof *fwd);
  171. if (fwd)
  172. bm_memcasemem_study0(needle, nlen, fwd);
  173. return fwd;
  174. }
  175. /** Search for substring using Boyer-Moore algorithm.
  176. * @ingroup su_bm
  177. */
  178. char *
  179. bm_memcasemem(char const *haystack, size_t hlen,
  180. char const *needle, size_t nlen,
  181. bm_fwd_table_t *fwd)
  182. {
  183. size_t i, j;
  184. bm_fwd_table_t fwd0[1];
  185. if (nlen == 0)
  186. return (char *)haystack;
  187. if (needle == 0 || haystack == 0 || nlen > hlen)
  188. return NULL;
  189. if (nlen == 1) {
  190. for (i = 0; i < hlen; i++)
  191. if (haystack[i] == needle[0])
  192. return (char *)haystack + i;
  193. return NULL;
  194. }
  195. if (!fwd) {
  196. fwd = bm_memcasemem_study0(needle, nlen, fwd0);
  197. }
  198. for (i = j = nlen - 1; i < hlen;) {
  199. unsigned char h = haystack[i], n = needle[j];
  200. if (isupper(h))
  201. h = tolower(h);
  202. if (isupper(n))
  203. n = tolower(n);
  204. if (h == n) {
  205. TORTURELOG(("match \"%s\" at %u\n"
  206. "with %*s\"%.*s*%s\": %s\n",
  207. haystack, (unsigned)i,
  208. (int)(i - j), "", (int)j, needle, needle + j + 1,
  209. j == 0 ? "match!" : "back by 1"));
  210. if (j == 0)
  211. return (char *)haystack + i;
  212. i--, j--;
  213. }
  214. else {
  215. if (fwd->table[h] > nlen - j) {
  216. TORTURELOG(("match \"%s\" at %u\n"
  217. "last %*s\"%.*s*%s\": (by %u)\n",
  218. haystack, (unsigned)i,
  219. (int)(i - j), "", (int)j, needle, needle + j + 1,
  220. fwd->table[h]));
  221. i += fwd->table[h];
  222. }
  223. else {
  224. TORTURELOG(("match \"%s\" at %u\n"
  225. "2nd %*s\"%.*s*%s\": (by %u)\n",
  226. haystack, (unsigned)i,
  227. (int)(i - j), "", (int)j, needle, needle + j + 1,
  228. (unsigned)(nlen - j)));
  229. i += nlen - j;
  230. }
  231. j = nlen - 1;
  232. }
  233. }
  234. return NULL;
  235. }