utstring.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. Copyright (c) 2008-2014, Troy D. Hanson http://troydhanson.github.com/uthash/
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  10. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  11. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  12. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  16. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19. */
  20. /* a dynamic string implementation using macros
  21. */
  22. #ifndef UTSTRING_H
  23. #define UTSTRING_H
  24. #define UTSTRING_VERSION 1.9.9
  25. #ifdef __GNUC__
  26. #define _UNUSED_ __attribute__ ((__unused__))
  27. #else
  28. #define _UNUSED_
  29. #endif
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <stdarg.h>
  34. #define oom() exit(-1)
  35. typedef struct {
  36. char *d;
  37. size_t n; /* allocd size */
  38. size_t i; /* index of first unused byte */
  39. } UT_string;
  40. #define utstring_reserve(s,amt) \
  41. do { \
  42. if (((s)->n - (s)->i) < (size_t)(amt)) { \
  43. (s)->d = (char*)realloc((s)->d, (s)->n + amt); \
  44. if ((s)->d == NULL) oom(); \
  45. (s)->n += amt; \
  46. } \
  47. } while(0)
  48. #define utstring_init(s) \
  49. do { \
  50. (s)->n = 0; (s)->i = 0; (s)->d = NULL; \
  51. utstring_reserve(s,100); \
  52. (s)->d[0] = '\0'; \
  53. } while(0)
  54. #define utstring_done(s) \
  55. do { \
  56. if ((s)->d != NULL) free((s)->d); \
  57. (s)->n = 0; \
  58. } while(0)
  59. #define utstring_free(s) \
  60. do { \
  61. utstring_done(s); \
  62. free(s); \
  63. } while(0)
  64. #define utstring_new(s) \
  65. do { \
  66. s = (UT_string*)calloc(sizeof(UT_string),1); \
  67. if (!s) oom(); \
  68. utstring_init(s); \
  69. } while(0)
  70. #define utstring_renew(s) \
  71. do { \
  72. if (s) { \
  73. utstring_clear(s); \
  74. } else { \
  75. utstring_new(s); \
  76. } \
  77. } while(0)
  78. #define utstring_clear(s) \
  79. do { \
  80. (s)->i = 0; \
  81. (s)->d[0] = '\0'; \
  82. } while(0)
  83. #define utstring_bincpy(s,b,l) \
  84. do { \
  85. utstring_reserve((s),(l)+1); \
  86. if (l) memcpy(&(s)->d[(s)->i], b, l); \
  87. (s)->i += l; \
  88. (s)->d[(s)->i]='\0'; \
  89. } while(0)
  90. #define utstring_concat(dst,src) \
  91. do { \
  92. utstring_reserve((dst),((src)->i)+1); \
  93. if ((src)->i) memcpy(&(dst)->d[(dst)->i], (src)->d, (src)->i); \
  94. (dst)->i += (src)->i; \
  95. (dst)->d[(dst)->i]='\0'; \
  96. } while(0)
  97. #define utstring_len(s) ((unsigned)((s)->i))
  98. #define utstring_body(s) ((s)->d)
  99. _UNUSED_ static void utstring_printf_va(UT_string *s, const char *fmt, va_list ap) {
  100. int n;
  101. va_list cp;
  102. while (1) {
  103. #ifdef _WIN32
  104. cp = ap;
  105. #else
  106. va_copy(cp, ap);
  107. #endif
  108. n = vsnprintf (&s->d[s->i], s->n-s->i, fmt, cp);
  109. va_end(cp);
  110. if ((n > -1) && ((size_t) n < (s->n-s->i))) {
  111. s->i += n;
  112. return;
  113. }
  114. /* Else try again with more space. */
  115. if (n > -1) utstring_reserve(s,n+1); /* exact */
  116. else utstring_reserve(s,(s->n)*2); /* 2x */
  117. }
  118. }
  119. #ifdef __GNUC__
  120. /* support printf format checking (2=the format string, 3=start of varargs) */
  121. static void utstring_printf(UT_string *s, const char *fmt, ...)
  122. __attribute__ (( format( printf, 2, 3) ));
  123. #endif
  124. _UNUSED_ static void utstring_printf(UT_string *s, const char *fmt, ...) {
  125. va_list ap;
  126. va_start(ap,fmt);
  127. utstring_printf_va(s,fmt,ap);
  128. va_end(ap);
  129. }
  130. /*******************************************************************************
  131. * begin substring search functions *
  132. ******************************************************************************/
  133. /* Build KMP table from left to right. */
  134. _UNUSED_ static void _utstring_BuildTable(
  135. const char *P_Needle,
  136. size_t P_NeedleLen,
  137. long *P_KMP_Table)
  138. {
  139. long i, j;
  140. i = 0;
  141. j = i - 1;
  142. P_KMP_Table[i] = j;
  143. while (i < (long) P_NeedleLen)
  144. {
  145. while ( (j > -1) && (P_Needle[i] != P_Needle[j]) )
  146. {
  147. j = P_KMP_Table[j];
  148. }
  149. i++;
  150. j++;
  151. if (i < (long) P_NeedleLen)
  152. {
  153. if (P_Needle[i] == P_Needle[j])
  154. {
  155. P_KMP_Table[i] = P_KMP_Table[j];
  156. }
  157. else
  158. {
  159. P_KMP_Table[i] = j;
  160. }
  161. }
  162. else
  163. {
  164. P_KMP_Table[i] = j;
  165. }
  166. }
  167. return;
  168. }
  169. /* Build KMP table from right to left. */
  170. _UNUSED_ static void _utstring_BuildTableR(
  171. const char *P_Needle,
  172. size_t P_NeedleLen,
  173. long *P_KMP_Table)
  174. {
  175. long i, j;
  176. i = P_NeedleLen - 1;
  177. j = i + 1;
  178. P_KMP_Table[i + 1] = j;
  179. while (i >= 0)
  180. {
  181. while ( (j < (long) P_NeedleLen) && (P_Needle[i] != P_Needle[j]) )
  182. {
  183. j = P_KMP_Table[j + 1];
  184. }
  185. i--;
  186. j--;
  187. if (i >= 0)
  188. {
  189. if (P_Needle[i] == P_Needle[j])
  190. {
  191. P_KMP_Table[i + 1] = P_KMP_Table[j + 1];
  192. }
  193. else
  194. {
  195. P_KMP_Table[i + 1] = j;
  196. }
  197. }
  198. else
  199. {
  200. P_KMP_Table[i + 1] = j;
  201. }
  202. }
  203. return;
  204. }
  205. /* Search data from left to right. ( Multiple search mode. ) */
  206. _UNUSED_ static long _utstring_find(
  207. const char *P_Haystack,
  208. size_t P_HaystackLen,
  209. const char *P_Needle,
  210. size_t P_NeedleLen,
  211. long *P_KMP_Table)
  212. {
  213. long i, j;
  214. long V_FindPosition = -1;
  215. /* Search from left to right. */
  216. i = j = 0;
  217. while ( (j < (int)P_HaystackLen) && (((P_HaystackLen - j) + i) >= P_NeedleLen) )
  218. {
  219. while ( (i > -1) && (P_Needle[i] != P_Haystack[j]) )
  220. {
  221. i = P_KMP_Table[i];
  222. }
  223. i++;
  224. j++;
  225. if (i >= (int)P_NeedleLen)
  226. {
  227. /* Found. */
  228. V_FindPosition = j - i;
  229. break;
  230. }
  231. }
  232. return V_FindPosition;
  233. }
  234. /* Search data from right to left. ( Multiple search mode. ) */
  235. _UNUSED_ static long _utstring_findR(
  236. const char *P_Haystack,
  237. size_t P_HaystackLen,
  238. const char *P_Needle,
  239. size_t P_NeedleLen,
  240. long *P_KMP_Table)
  241. {
  242. long i, j;
  243. long V_FindPosition = -1;
  244. /* Search from right to left. */
  245. j = (P_HaystackLen - 1);
  246. i = (P_NeedleLen - 1);
  247. while ( (j >= 0) && (j >= i) )
  248. {
  249. while ( (i < (int)P_NeedleLen) && (P_Needle[i] != P_Haystack[j]) )
  250. {
  251. i = P_KMP_Table[i + 1];
  252. }
  253. i--;
  254. j--;
  255. if (i < 0)
  256. {
  257. /* Found. */
  258. V_FindPosition = j + 1;
  259. break;
  260. }
  261. }
  262. return V_FindPosition;
  263. }
  264. /* Search data from left to right. ( One time search mode. ) */
  265. _UNUSED_ static long utstring_find(
  266. UT_string *s,
  267. long P_StartPosition, /* Start from 0. -1 means last position. */
  268. const char *P_Needle,
  269. size_t P_NeedleLen)
  270. {
  271. long V_StartPosition;
  272. long V_HaystackLen;
  273. long *V_KMP_Table;
  274. long V_FindPosition = -1;
  275. if (P_StartPosition < 0)
  276. {
  277. V_StartPosition = s->i + P_StartPosition;
  278. }
  279. else
  280. {
  281. V_StartPosition = P_StartPosition;
  282. }
  283. V_HaystackLen = s->i - V_StartPosition;
  284. if ( (V_HaystackLen >= (long) P_NeedleLen) && (P_NeedleLen > 0) )
  285. {
  286. V_KMP_Table = (long *)malloc(sizeof(long) * (P_NeedleLen + 1));
  287. if (V_KMP_Table != NULL)
  288. {
  289. _utstring_BuildTable(P_Needle, P_NeedleLen, V_KMP_Table);
  290. V_FindPosition = _utstring_find(s->d + V_StartPosition,
  291. V_HaystackLen,
  292. P_Needle,
  293. P_NeedleLen,
  294. V_KMP_Table);
  295. if (V_FindPosition >= 0)
  296. {
  297. V_FindPosition += V_StartPosition;
  298. }
  299. free(V_KMP_Table);
  300. }
  301. }
  302. return V_FindPosition;
  303. }
  304. /* Search data from right to left. ( One time search mode. ) */
  305. _UNUSED_ static long utstring_findR(
  306. UT_string *s,
  307. long P_StartPosition, /* Start from 0. -1 means last position. */
  308. const char *P_Needle,
  309. size_t P_NeedleLen)
  310. {
  311. long V_StartPosition;
  312. long V_HaystackLen;
  313. long *V_KMP_Table;
  314. long V_FindPosition = -1;
  315. if (P_StartPosition < 0)
  316. {
  317. V_StartPosition = s->i + P_StartPosition;
  318. }
  319. else
  320. {
  321. V_StartPosition = P_StartPosition;
  322. }
  323. V_HaystackLen = V_StartPosition + 1;
  324. if ( (V_HaystackLen >= (long) P_NeedleLen) && (P_NeedleLen > 0) )
  325. {
  326. V_KMP_Table = (long *)malloc(sizeof(long) * (P_NeedleLen + 1));
  327. if (V_KMP_Table != NULL)
  328. {
  329. _utstring_BuildTableR(P_Needle, P_NeedleLen, V_KMP_Table);
  330. V_FindPosition = _utstring_findR(s->d,
  331. V_HaystackLen,
  332. P_Needle,
  333. P_NeedleLen,
  334. V_KMP_Table);
  335. free(V_KMP_Table);
  336. }
  337. }
  338. return V_FindPosition;
  339. }
  340. /*******************************************************************************
  341. * end substring search functions *
  342. ******************************************************************************/
  343. #endif /* UTSTRING_H */