You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utstring.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. Copyright (c) 2008-2013, 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.8
  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 <stdarg.h>
  33. #ifndef oom
  34. #define oom() exit(-1)
  35. #endif
  36. typedef struct {
  37. char *d;
  38. void **pd;
  39. size_t n; /* allocd size */
  40. size_t i; /* index of first unused byte */
  41. } UT_string;
  42. #define utstring_reserve(s,amt) \
  43. do { \
  44. if (((s)->n - (s)->i) < (size_t)(amt)) { \
  45. (s)->d = (char*)realloc((s)->d, (s)->n + amt); \
  46. if ((s)->d == NULL) oom(); \
  47. (s)->n += amt; \
  48. if ((s)->pd) *((s)->pd) = (s)->d; \
  49. } \
  50. } while(0)
  51. #define utstring_init(s) \
  52. do { \
  53. (s)->n = 0; (s)->i = 0; (s)->d = NULL; \
  54. utstring_reserve(s,128); \
  55. (s)->d[0] = '\0'; \
  56. } while(0)
  57. #define utstring_done(s) \
  58. do { \
  59. if ((s)->d != NULL) free((s)->d); \
  60. (s)->n = 0; \
  61. } while(0)
  62. #define utstring_free(s) \
  63. do { \
  64. utstring_done(s); \
  65. free(s); \
  66. } while(0)
  67. #define utstring_new(s) \
  68. do { \
  69. s = (UT_string*)calloc(1, sizeof(UT_string)); \
  70. if (!s) oom(); \
  71. utstring_init(s); \
  72. } while(0)
  73. #define utstring_renew(s) \
  74. do { \
  75. if (s) { \
  76. utstring_clear(s); \
  77. } else { \
  78. utstring_new(s); \
  79. } \
  80. } while(0)
  81. #define utstring_clear(s) \
  82. do { \
  83. (s)->i = 0; \
  84. (s)->d[0] = '\0'; \
  85. } while(0)
  86. #define utstring_bincpy(s,b,l) \
  87. do { \
  88. utstring_reserve((s),(l)+1); \
  89. if (l) memcpy(&(s)->d[(s)->i], b, l); \
  90. (s)->i += l; \
  91. (s)->d[(s)->i]='\0'; \
  92. } while(0)
  93. #define utstring_concat(dst,src) \
  94. do { \
  95. utstring_reserve((dst),((src)->i)+1); \
  96. if ((src)->i) memcpy(&(dst)->d[(dst)->i], (src)->d, (src)->i); \
  97. (dst)->i += (src)->i; \
  98. (dst)->d[(dst)->i]='\0'; \
  99. } while(0)
  100. #define utstring_len(s) ((unsigned)((s)->i))
  101. #define utstring_body(s) ((s)->d)
  102. #ifdef __GNUC__
  103. __attribute__((format(printf, 2, 0)))
  104. #endif
  105. _UNUSED_ static void utstring_printf_va(UT_string *s, const char *fmt, va_list ap) {
  106. int n;
  107. va_list cp;
  108. while (1) {
  109. #ifdef _WIN32
  110. cp = ap;
  111. #else
  112. va_copy(cp, ap);
  113. #endif
  114. n = vsnprintf (&s->d[s->i], s->n-s->i, fmt, cp);
  115. va_end(cp);
  116. if ((n > -1) && (n < (int)(s->n-s->i))) {
  117. s->i += n;
  118. return;
  119. }
  120. /* Else try again with more space. */
  121. if (n > -1) utstring_reserve(s,n+1); /* exact */
  122. else utstring_reserve(s,(s->n)*2); /* 2x */
  123. }
  124. }
  125. #ifdef __GNUC__
  126. /* support printf format checking (2=the format string, 3=start of varargs) */
  127. static void utstring_printf(UT_string *s, const char *fmt, ...)
  128. __attribute__ (( format( printf, 2, 3) ));
  129. #endif
  130. _UNUSED_ static void utstring_printf(UT_string *s, const char *fmt, ...) {
  131. va_list ap;
  132. va_start(ap,fmt);
  133. utstring_printf_va(s,fmt,ap);
  134. va_end(ap);
  135. }
  136. #define utstring_append_len(dst, src, len) \
  137. do { \
  138. while ((dst)->n-(dst)->i <= (len)) utstring_reserve((dst),((dst)->n)*2); \
  139. memcpy(&(dst)->d[(dst)->i], (src), (len)); \
  140. (dst)->i+=(len); \
  141. (dst)->d[(dst)->i]='\0'; \
  142. } while(0)
  143. #define utstring_append_c(dst, c) \
  144. do { \
  145. if ((dst)->n-(dst)->i < 2) utstring_reserve((dst),((dst)->n)*2); \
  146. (dst)->d[(dst)->i++] = (c); \
  147. (dst)->d[(dst)->i]='\0'; \
  148. } while(0)
  149. /*******************************************************************************
  150. * begin substring search functions *
  151. ******************************************************************************/
  152. /* Build KMP table from left to right. */
  153. _UNUSED_ static void _utstring_BuildTable(
  154. const char *P_Needle,
  155. ssize_t P_NeedleLen,
  156. long *P_KMP_Table)
  157. {
  158. long i, j;
  159. i = 0;
  160. j = i - 1;
  161. P_KMP_Table[i] = j;
  162. while (i < P_NeedleLen)
  163. {
  164. while ( (j > -1) && (P_Needle[i] != P_Needle[j]) )
  165. {
  166. j = P_KMP_Table[j];
  167. }
  168. i++;
  169. j++;
  170. if (i < P_NeedleLen)
  171. {
  172. if (P_Needle[i] == P_Needle[j])
  173. {
  174. P_KMP_Table[i] = P_KMP_Table[j];
  175. }
  176. else
  177. {
  178. P_KMP_Table[i] = j;
  179. }
  180. }
  181. else
  182. {
  183. P_KMP_Table[i] = j;
  184. }
  185. }
  186. return;
  187. }
  188. /* Build KMP table from right to left. */
  189. _UNUSED_ static void _utstring_BuildTableR(
  190. const char *P_Needle,
  191. ssize_t P_NeedleLen,
  192. long *P_KMP_Table)
  193. {
  194. long i, j;
  195. i = P_NeedleLen - 1;
  196. j = i + 1;
  197. P_KMP_Table[i + 1] = j;
  198. while (i >= 0)
  199. {
  200. while ( (j < P_NeedleLen) && (P_Needle[i] != P_Needle[j]) )
  201. {
  202. j = P_KMP_Table[j + 1];
  203. }
  204. i--;
  205. j--;
  206. if (i >= 0)
  207. {
  208. if (P_Needle[i] == P_Needle[j])
  209. {
  210. P_KMP_Table[i + 1] = P_KMP_Table[j + 1];
  211. }
  212. else
  213. {
  214. P_KMP_Table[i + 1] = j;
  215. }
  216. }
  217. else
  218. {
  219. P_KMP_Table[i + 1] = j;
  220. }
  221. }
  222. return;
  223. }
  224. /* Search data from left to right. ( Multiple search mode. ) */
  225. _UNUSED_ static long _utstring_find(
  226. const char *P_Haystack,
  227. size_t P_HaystackLen,
  228. const char *P_Needle,
  229. size_t P_NeedleLen,
  230. long *P_KMP_Table)
  231. {
  232. long i, j;
  233. long V_FindPosition = -1;
  234. /* Search from left to right. */
  235. i = j = 0;
  236. while ( (j < (int)P_HaystackLen) && (((P_HaystackLen - j) + i) >= P_NeedleLen) )
  237. {
  238. while ( (i > -1) && (P_Needle[i] != P_Haystack[j]) )
  239. {
  240. i = P_KMP_Table[i];
  241. }
  242. i++;
  243. j++;
  244. if (i >= (int)P_NeedleLen)
  245. {
  246. /* Found. */
  247. V_FindPosition = j - i;
  248. break;
  249. }
  250. }
  251. return V_FindPosition;
  252. }
  253. /* Search data from right to left. ( Multiple search mode. ) */
  254. _UNUSED_ static long _utstring_findR(
  255. const char *P_Haystack,
  256. size_t P_HaystackLen,
  257. const char *P_Needle,
  258. size_t P_NeedleLen,
  259. long *P_KMP_Table)
  260. {
  261. long i, j;
  262. long V_FindPosition = -1;
  263. /* Search from right to left. */
  264. j = (P_HaystackLen - 1);
  265. i = (P_NeedleLen - 1);
  266. while ( (j >= 0) && (j >= i) )
  267. {
  268. while ( (i < (int)P_NeedleLen) && (P_Needle[i] != P_Haystack[j]) )
  269. {
  270. i = P_KMP_Table[i + 1];
  271. }
  272. i--;
  273. j--;
  274. if (i < 0)
  275. {
  276. /* Found. */
  277. V_FindPosition = j + 1;
  278. break;
  279. }
  280. }
  281. return V_FindPosition;
  282. }
  283. /* Search data from left to right. ( One time search mode. ) */
  284. _UNUSED_ static long utstring_find(
  285. UT_string *s,
  286. long P_StartPosition, /* Start from 0. -1 means last position. */
  287. const char *P_Needle,
  288. ssize_t P_NeedleLen)
  289. {
  290. long V_StartPosition;
  291. long V_HaystackLen;
  292. long *V_KMP_Table;
  293. long V_FindPosition = -1;
  294. if (P_StartPosition < 0)
  295. {
  296. V_StartPosition = s->i + P_StartPosition;
  297. }
  298. else
  299. {
  300. V_StartPosition = P_StartPosition;
  301. }
  302. V_HaystackLen = s->i - V_StartPosition;
  303. if ( (V_HaystackLen >= P_NeedleLen) && (P_NeedleLen > 0) )
  304. {
  305. V_KMP_Table = (long *)malloc(sizeof(long) * (P_NeedleLen + 1));
  306. if (V_KMP_Table != NULL)
  307. {
  308. _utstring_BuildTable(P_Needle, P_NeedleLen, V_KMP_Table);
  309. V_FindPosition = _utstring_find(s->d + V_StartPosition,
  310. V_HaystackLen,
  311. P_Needle,
  312. P_NeedleLen,
  313. V_KMP_Table);
  314. if (V_FindPosition >= 0)
  315. {
  316. V_FindPosition += V_StartPosition;
  317. }
  318. free(V_KMP_Table);
  319. }
  320. }
  321. return V_FindPosition;
  322. }
  323. /* Search data from right to left. ( One time search mode. ) */
  324. _UNUSED_ static long utstring_findR(
  325. UT_string *s,
  326. long P_StartPosition, /* Start from 0. -1 means last position. */
  327. const char *P_Needle,
  328. ssize_t P_NeedleLen)
  329. {
  330. long V_StartPosition;
  331. long V_HaystackLen;
  332. long *V_KMP_Table;
  333. long V_FindPosition = -1;
  334. if (P_StartPosition < 0)
  335. {
  336. V_StartPosition = s->i + P_StartPosition;
  337. }
  338. else
  339. {
  340. V_StartPosition = P_StartPosition;
  341. }
  342. V_HaystackLen = V_StartPosition + 1;
  343. if ( (V_HaystackLen >= P_NeedleLen) && (P_NeedleLen > 0) )
  344. {
  345. V_KMP_Table = (long *)malloc(sizeof(long) * (P_NeedleLen + 1));
  346. if (V_KMP_Table != NULL)
  347. {
  348. _utstring_BuildTableR(P_Needle, P_NeedleLen, V_KMP_Table);
  349. V_FindPosition = _utstring_findR(s->d,
  350. V_HaystackLen,
  351. P_Needle,
  352. P_NeedleLen,
  353. V_KMP_Table);
  354. free(V_KMP_Table);
  355. }
  356. }
  357. return V_FindPosition;
  358. }
  359. /*******************************************************************************
  360. * end substring search functions *
  361. ******************************************************************************/
  362. #endif /* UTSTRING_H */