dstr.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. EXPORT int astrcmpi(const char *str1, const char *str2);
  36. EXPORT int wstrcmpi(const wchar_t *str1, const wchar_t *str2);
  37. EXPORT int astrcmp_n(const char *str1, const char *str2, size_t n);
  38. EXPORT int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n);
  39. EXPORT int astrcmpi_n(const char *str1, const char *str2, size_t n);
  40. EXPORT int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n);
  41. EXPORT char *astrstri(char *str, const char *find);
  42. EXPORT char *strdepad(char *str);
  43. EXPORT wchar_t *wcsdepad(wchar_t *str);
  44. static inline void dstr_init(struct dstr *dst);
  45. static inline void dstr_init_move(struct dstr *dst, struct dstr *src);
  46. static inline void dstr_init_move_array(struct dstr *dst, char *str);
  47. static inline void dstr_init_copy(struct dstr *dst, const char *src);
  48. static inline void dstr_init_copy_dstr(struct dstr *dst,
  49. const struct dstr *src);
  50. EXPORT void dstr_init_strref(struct dstr *dst, const struct strref *src);
  51. static inline void dstr_free(struct dstr *dst);
  52. static inline void dstr_array_free(struct dstr *array, const size_t count);
  53. static inline void dstr_move(struct dstr *dst, struct dstr *src);
  54. static inline void dstr_move_array(struct dstr *dst, char *str);
  55. EXPORT void dstr_copy(struct dstr *dst, const char *array);
  56. static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src);
  57. EXPORT void dstr_copy_strref(struct dstr *dst, const struct strref *src);
  58. EXPORT void dstr_ncopy(struct dstr *dst, const char *array,
  59. const size_t len);
  60. EXPORT void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src,
  61. const size_t len);
  62. static inline void dstr_resize(struct dstr *dst, const size_t num);
  63. static inline void dstr_reserve(struct dstr *dst, const size_t num);
  64. static inline bool dstr_isempty(const struct dstr *str);
  65. static inline void dstr_cat(struct dstr *dst, const char *array);
  66. EXPORT void dstr_cat_dstr(struct dstr *dst, const struct dstr *str);
  67. EXPORT void dstr_cat_strref(struct dstr *dst, const struct strref *str);
  68. static inline void dstr_cat_ch(struct dstr *dst, char ch);
  69. EXPORT void dstr_ncat(struct dstr *dst, const char *array, const size_t len);
  70. EXPORT void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str,
  71. const size_t len);
  72. EXPORT void dstr_cat_strref(struct dstr *dst, const struct strref *str);
  73. EXPORT void dstr_insert(struct dstr *dst, const size_t idx,
  74. const char *array);
  75. EXPORT void dstr_insert_dstr(struct dstr *dst, const size_t idx,
  76. const struct dstr *str);
  77. EXPORT void dstr_insert_ch(struct dstr *dst, const size_t idx,
  78. const char ch);
  79. EXPORT void dstr_remove(struct dstr *dst, const size_t idx, const size_t count);
  80. EXPORT void dstr_printf(struct dstr *dst, const char *format, ...);
  81. EXPORT void dstr_catf(struct dstr *dst, const char *format, ...);
  82. EXPORT void dstr_vprintf(struct dstr *dst, const char *format, va_list args);
  83. EXPORT void dstr_vcatf(struct dstr *dst, const char *format, va_list args);
  84. EXPORT void dstr_safe_printf(struct dstr *dst, const char *format,
  85. const char *val1, const char *val2, const char *val3,
  86. const char *val4);
  87. static inline const char *dstr_find_i(const struct dstr *str,
  88. const char *find);
  89. static inline const char *dstr_find(const struct dstr *str,
  90. const char *find);
  91. EXPORT void dstr_replace(struct dstr *str, const char *find,
  92. const char *replace);
  93. static inline int dstr_cmp(const struct dstr *str1, const char *str2);
  94. static inline int dstr_cmpi(const struct dstr *str1, const char *str2);
  95. static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
  96. const size_t n);
  97. static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
  98. const size_t n);
  99. EXPORT void dstr_depad(struct dstr *dst);
  100. EXPORT void dstr_left(struct dstr *dst, const struct dstr *str,
  101. const size_t pos);
  102. EXPORT void dstr_mid(struct dstr *dst, const struct dstr *str,
  103. const size_t start, const size_t count);
  104. EXPORT void dstr_right(struct dstr *dst, const struct dstr *str,
  105. const size_t pos);
  106. EXPORT void dstr_from_mbs(struct dstr *dst, const char *mbstr);
  107. EXPORT char *dstr_to_mbs(const struct dstr *str);
  108. EXPORT void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr);
  109. EXPORT wchar_t *dstr_to_wcs(const struct dstr *str);
  110. /* ------------------------------------------------------------------------- */
  111. static inline void dstr_init(struct dstr *dst)
  112. {
  113. dst->array = NULL;
  114. dst->len = 0;
  115. dst->capacity = 0;
  116. }
  117. static inline void dstr_init_move_array(struct dstr *dst, char *str)
  118. {
  119. dst->array = str;
  120. dst->len = (!str) ? 0 : strlen(str);
  121. dst->capacity = dst->len + 1;
  122. }
  123. static inline void dstr_init_move(struct dstr *dst, struct dstr *src)
  124. {
  125. *dst = *src;
  126. dstr_init(src);
  127. }
  128. static inline void dstr_init_copy(struct dstr *dst, const char *str)
  129. {
  130. dstr_init(dst);
  131. dstr_copy(dst, str);
  132. }
  133. static inline void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)
  134. {
  135. dstr_init(dst);
  136. dstr_copy_dstr(dst, src);
  137. }
  138. static inline void dstr_free(struct dstr *dst)
  139. {
  140. bfree(dst->array);
  141. dst->array = NULL;
  142. dst->len = 0;
  143. dst->capacity = 0;
  144. }
  145. static inline void dstr_array_free(struct dstr *array, const size_t count)
  146. {
  147. size_t i;
  148. for (i = 0; i < count; i++)
  149. dstr_free(array+i);
  150. }
  151. static inline void dstr_move_array(struct dstr *dst, char *str)
  152. {
  153. dstr_free(dst);
  154. dst->array = str;
  155. dst->len = (!str) ? 0 : strlen(str);
  156. dst->capacity = dst->len + 1;
  157. }
  158. static inline void dstr_move(struct dstr *dst, struct dstr *src)
  159. {
  160. dstr_free(dst);
  161. dstr_init_move(dst, src);
  162. }
  163. static inline void dstr_ensure_capacity(struct dstr *dst, const size_t new_size)
  164. {
  165. size_t new_cap;
  166. if (new_size <= dst->capacity)
  167. return;
  168. new_cap = (!dst->capacity) ? new_size : dst->capacity*2;
  169. if (new_size > new_cap)
  170. new_cap = new_size;
  171. dst->array = (char*)brealloc(dst->array, new_cap);
  172. dst->capacity = new_cap;
  173. }
  174. static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src)
  175. {
  176. if (dst->array)
  177. dstr_free(dst);
  178. dstr_ensure_capacity(dst, src->len + 1);
  179. memcpy(dst->array, src->array, src->len + 1);
  180. dst->len = src->len;
  181. }
  182. static inline void dstr_reserve(struct dstr *dst, const size_t capacity)
  183. {
  184. if (capacity == 0 || capacity <= dst->len)
  185. return;
  186. dst->array = (char*)brealloc(dst->array, capacity);
  187. dst->capacity = capacity;
  188. }
  189. static inline void dstr_resize(struct dstr *dst, const size_t num)
  190. {
  191. if (!num) {
  192. dstr_free(dst);
  193. return;
  194. }
  195. dstr_ensure_capacity(dst, num + 1);
  196. dst->array[num] = 0;
  197. dst->len = num;
  198. }
  199. static inline bool dstr_isempty(const struct dstr *str)
  200. {
  201. if (!str->array || !str->len)
  202. return true;
  203. if (!*str->array)
  204. return true;
  205. return false;
  206. }
  207. static inline void dstr_cat(struct dstr *dst, const char *array)
  208. {
  209. size_t len;
  210. if (!array || !*array)
  211. return;
  212. len = strlen(array);
  213. dstr_ncat(dst, array, len);
  214. }
  215. static inline void dstr_cat_ch(struct dstr *dst, char ch)
  216. {
  217. dstr_ensure_capacity(dst, ++dst->len + 1);
  218. dst->array[dst->len-1] = ch;
  219. dst->array[dst->len] = 0;
  220. }
  221. static inline const char *dstr_find_i(const struct dstr *str, const char *find)
  222. {
  223. return astrstri(str->array, find);
  224. }
  225. static inline const char *dstr_find(const struct dstr *str, const char *find)
  226. {
  227. return strstr(str->array, find);
  228. }
  229. static inline int dstr_cmp(const struct dstr *str1, const char *str2)
  230. {
  231. return strcmp(str1->array, str2);
  232. }
  233. static inline int dstr_cmpi(const struct dstr *str1, const char *str2)
  234. {
  235. return astrcmpi(str1->array, str2);
  236. }
  237. static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
  238. const size_t n)
  239. {
  240. return astrcmp_n(str1->array, str2, n);
  241. }
  242. static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
  243. const size_t n)
  244. {
  245. return astrcmpi_n(str1->array, str2, n);
  246. }
  247. #ifdef __cplusplus
  248. }
  249. #endif