utstring.h 12 KB

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