dstr.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include "c99defs.h"
  20. #include "bmem.h"
  21. /*
  22. * Dynamic string
  23. *
  24. * Helper struct/functions for dynamically sizing string buffers.
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. struct strref;
  30. struct dstr {
  31. char *array;
  32. size_t len; /* number of characters, excluding null terminator */
  33. size_t capacity;
  34. };
  35. #ifndef _MSC_VER
  36. #define PRINTFATTR(f, a) __attribute__((__format__(__printf__, f, a)))
  37. #else
  38. #define PRINTFATTR(f, a)
  39. #endif
  40. EXPORT int astrcmpi(const char *str1, const char *str2);
  41. EXPORT int wstrcmpi(const wchar_t *str1, const wchar_t *str2);
  42. EXPORT int astrcmp_n(const char *str1, const char *str2, size_t n);
  43. EXPORT int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n);
  44. EXPORT int astrcmpi_n(const char *str1, const char *str2, size_t n);
  45. EXPORT int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n);
  46. EXPORT char *astrstri(const char *str, const char *find);
  47. EXPORT wchar_t *wstrstri(const wchar_t *str, const wchar_t *find);
  48. EXPORT char *strdepad(char *str);
  49. EXPORT wchar_t *wcsdepad(wchar_t *str);
  50. EXPORT char **strlist_split(const char *str, char split_ch, bool include_empty);
  51. EXPORT void strlist_free(char **strlist);
  52. static inline void dstr_init(struct dstr *dst);
  53. static inline void dstr_init_move(struct dstr *dst, struct dstr *src);
  54. static inline void dstr_init_move_array(struct dstr *dst, char *str);
  55. static inline void dstr_init_copy(struct dstr *dst, const char *src);
  56. static inline void dstr_init_copy_dstr(struct dstr *dst,
  57. const struct dstr *src);
  58. EXPORT void dstr_init_copy_strref(struct dstr *dst, const struct strref *src);
  59. static inline void dstr_free(struct dstr *dst);
  60. static inline void dstr_array_free(struct dstr *array, const size_t count);
  61. static inline void dstr_move(struct dstr *dst, struct dstr *src);
  62. static inline void dstr_move_array(struct dstr *dst, char *str);
  63. EXPORT void dstr_copy(struct dstr *dst, const char *array);
  64. static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src);
  65. EXPORT void dstr_copy_strref(struct dstr *dst, const struct strref *src);
  66. EXPORT void dstr_ncopy(struct dstr *dst, const char *array,
  67. const size_t len);
  68. EXPORT void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src,
  69. const size_t len);
  70. static inline void dstr_resize(struct dstr *dst, const size_t num);
  71. static inline void dstr_reserve(struct dstr *dst, const size_t num);
  72. static inline bool dstr_is_empty(const struct dstr *str);
  73. static inline void dstr_cat(struct dstr *dst, const char *array);
  74. EXPORT void dstr_cat_dstr(struct dstr *dst, const struct dstr *str);
  75. EXPORT void dstr_cat_strref(struct dstr *dst, const struct strref *str);
  76. static inline void dstr_cat_ch(struct dstr *dst, char ch);
  77. EXPORT void dstr_ncat(struct dstr *dst, const char *array, const size_t len);
  78. EXPORT void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str,
  79. const size_t len);
  80. EXPORT void dstr_insert(struct dstr *dst, const size_t idx,
  81. const char *array);
  82. EXPORT void dstr_insert_dstr(struct dstr *dst, const size_t idx,
  83. const struct dstr *str);
  84. EXPORT void dstr_insert_ch(struct dstr *dst, const size_t idx,
  85. const char ch);
  86. EXPORT void dstr_remove(struct dstr *dst, const size_t idx, const size_t count);
  87. PRINTFATTR(2, 3)
  88. EXPORT void dstr_printf(struct dstr *dst, const char *format, ...);
  89. PRINTFATTR(2, 3)
  90. EXPORT void dstr_catf(struct dstr *dst, const char *format, ...);
  91. EXPORT void dstr_vprintf(struct dstr *dst, const char *format, va_list args);
  92. EXPORT void dstr_vcatf(struct dstr *dst, const char *format, va_list args);
  93. EXPORT void dstr_safe_printf(struct dstr *dst, const char *format,
  94. const char *val1, const char *val2, const char *val3,
  95. const char *val4);
  96. static inline const char *dstr_find_i(const struct dstr *str,
  97. const char *find);
  98. static inline const char *dstr_find(const struct dstr *str,
  99. const char *find);
  100. EXPORT void dstr_replace(struct dstr *str, const char *find,
  101. const char *replace);
  102. static inline int dstr_cmp(const struct dstr *str1, const char *str2);
  103. static inline int dstr_cmpi(const struct dstr *str1, const char *str2);
  104. static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
  105. const size_t n);
  106. static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
  107. const size_t n);
  108. EXPORT void dstr_depad(struct dstr *dst);
  109. EXPORT void dstr_left(struct dstr *dst, const struct dstr *str,
  110. const size_t pos);
  111. EXPORT void dstr_mid(struct dstr *dst, const struct dstr *str,
  112. const size_t start, const size_t count);
  113. EXPORT void dstr_right(struct dstr *dst, const struct dstr *str,
  114. const size_t pos);
  115. static inline char dstr_end(const struct dstr *str);
  116. EXPORT void dstr_from_mbs(struct dstr *dst, const char *mbstr);
  117. EXPORT char *dstr_to_mbs(const struct dstr *str);
  118. EXPORT void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr);
  119. EXPORT wchar_t *dstr_to_wcs(const struct dstr *str);
  120. EXPORT void dstr_to_upper(struct dstr *str);
  121. EXPORT void dstr_to_lower(struct dstr *str);
  122. #undef PRINTFATTR
  123. /* ------------------------------------------------------------------------- */
  124. static inline void dstr_init(struct dstr *dst)
  125. {
  126. dst->array = NULL;
  127. dst->len = 0;
  128. dst->capacity = 0;
  129. }
  130. static inline void dstr_init_move_array(struct dstr *dst, char *str)
  131. {
  132. dst->array = str;
  133. dst->len = (!str) ? 0 : strlen(str);
  134. dst->capacity = dst->len + 1;
  135. }
  136. static inline void dstr_init_move(struct dstr *dst, struct dstr *src)
  137. {
  138. *dst = *src;
  139. dstr_init(src);
  140. }
  141. static inline void dstr_init_copy(struct dstr *dst, const char *str)
  142. {
  143. dstr_init(dst);
  144. dstr_copy(dst, str);
  145. }
  146. static inline void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)
  147. {
  148. dstr_init(dst);
  149. dstr_copy_dstr(dst, src);
  150. }
  151. static inline void dstr_free(struct dstr *dst)
  152. {
  153. bfree(dst->array);
  154. dst->array = NULL;
  155. dst->len = 0;
  156. dst->capacity = 0;
  157. }
  158. static inline void dstr_array_free(struct dstr *array, const size_t count)
  159. {
  160. size_t i;
  161. for (i = 0; i < count; i++)
  162. dstr_free(array+i);
  163. }
  164. static inline void dstr_move_array(struct dstr *dst, char *str)
  165. {
  166. dstr_free(dst);
  167. dst->array = str;
  168. dst->len = (!str) ? 0 : strlen(str);
  169. dst->capacity = dst->len + 1;
  170. }
  171. static inline void dstr_move(struct dstr *dst, struct dstr *src)
  172. {
  173. dstr_free(dst);
  174. dstr_init_move(dst, src);
  175. }
  176. static inline void dstr_ensure_capacity(struct dstr *dst, const size_t new_size)
  177. {
  178. size_t new_cap;
  179. if (new_size <= dst->capacity)
  180. return;
  181. new_cap = (!dst->capacity) ? new_size : dst->capacity*2;
  182. if (new_size > new_cap)
  183. new_cap = new_size;
  184. dst->array = (char*)brealloc(dst->array, new_cap);
  185. dst->capacity = new_cap;
  186. }
  187. static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src)
  188. {
  189. if (dst->array)
  190. dstr_free(dst);
  191. dstr_ensure_capacity(dst, src->len + 1);
  192. memcpy(dst->array, src->array, src->len + 1);
  193. dst->len = src->len;
  194. }
  195. static inline void dstr_reserve(struct dstr *dst, const size_t capacity)
  196. {
  197. if (capacity == 0 || capacity <= dst->len)
  198. return;
  199. dst->array = (char*)brealloc(dst->array, capacity);
  200. dst->capacity = capacity;
  201. }
  202. static inline void dstr_resize(struct dstr *dst, const size_t num)
  203. {
  204. if (!num) {
  205. dstr_free(dst);
  206. return;
  207. }
  208. dstr_ensure_capacity(dst, num + 1);
  209. dst->array[num] = 0;
  210. dst->len = num;
  211. }
  212. static inline bool dstr_is_empty(const struct dstr *str)
  213. {
  214. if (!str->array || !str->len)
  215. return true;
  216. if (!*str->array)
  217. return true;
  218. return false;
  219. }
  220. static inline void dstr_cat(struct dstr *dst, const char *array)
  221. {
  222. size_t len;
  223. if (!array || !*array)
  224. return;
  225. len = strlen(array);
  226. dstr_ncat(dst, array, len);
  227. }
  228. static inline void dstr_cat_ch(struct dstr *dst, char ch)
  229. {
  230. dstr_ensure_capacity(dst, ++dst->len + 1);
  231. dst->array[dst->len-1] = ch;
  232. dst->array[dst->len] = 0;
  233. }
  234. static inline const char *dstr_find_i(const struct dstr *str, const char *find)
  235. {
  236. return astrstri(str->array, find);
  237. }
  238. static inline const char *dstr_find(const struct dstr *str, const char *find)
  239. {
  240. return strstr(str->array, find);
  241. }
  242. static inline int dstr_cmp(const struct dstr *str1, const char *str2)
  243. {
  244. return strcmp(str1->array, str2);
  245. }
  246. static inline int dstr_cmpi(const struct dstr *str1, const char *str2)
  247. {
  248. return astrcmpi(str1->array, str2);
  249. }
  250. static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
  251. const size_t n)
  252. {
  253. return astrcmp_n(str1->array, str2, n);
  254. }
  255. static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
  256. const size_t n)
  257. {
  258. return astrcmpi_n(str1->array, str2, n);
  259. }
  260. static inline char dstr_end(const struct dstr *str)
  261. {
  262. if (dstr_is_empty(str))
  263. return 0;
  264. return str->array[str->len - 1];
  265. }
  266. #ifdef __cplusplus
  267. }
  268. #endif