aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.c
blob: 6219935317e3df92e5809f0076485581f81daf06 (plain)
  1. #include <stdarg.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/param.h>
  9. #include "buffer.h"
  10. /* Used as default value for strbuf->ptr so that people can always
  11. * assume ptr is non-NULL and zero terminated even for new strbufs.
  12. */
  13. unsigned char cmark_strbuf__initbuf[1];
  14. unsigned char cmark_strbuf__oom[1];
  15. #define ENSURE_SIZE(b, d) \
  16. if ((d) > buf->asize && strbuf_grow(b, (d)) < 0) \
  17. return -1;
  18. #ifndef MIN
  19. #define MIN(x,y) ((x<y) ? x : y)
  20. #endif
  21. void cmark_strbuf_init(strbuf *buf, int initial_size)
  22. {
  23. buf->asize = 0;
  24. buf->size = 0;
  25. buf->ptr = cmark_strbuf__initbuf;
  26. if (initial_size)
  27. cmark_strbuf_grow(buf, initial_size);
  28. }
  29. int cmark_strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom)
  30. {
  31. unsigned char *new_ptr;
  32. int new_size;
  33. if (buf->ptr == cmark_strbuf__oom)
  34. return -1;
  35. if (target_size <= buf->asize)
  36. return 0;
  37. if (buf->asize == 0) {
  38. new_size = target_size;
  39. new_ptr = NULL;
  40. } else {
  41. new_size = buf->asize;
  42. new_ptr = buf->ptr;
  43. }
  44. /* grow the buffer size by 1.5, until it's big enough
  45. * to fit our target size */
  46. while (new_size < target_size)
  47. new_size = (new_size << 1) - (new_size >> 1);
  48. /* round allocation up to multiple of 8 */
  49. new_size = (new_size + 7) & ~7;
  50. new_ptr = realloc(new_ptr, new_size);
  51. if (!new_ptr) {
  52. if (mark_oom)
  53. buf->ptr = cmark_strbuf__oom;
  54. return -1;
  55. }
  56. buf->asize = new_size;
  57. buf->ptr = new_ptr;
  58. /* truncate the existing buffer size if necessary */
  59. if (buf->size >= buf->asize)
  60. buf->size = buf->asize - 1;
  61. buf->ptr[buf->size] = '\0';
  62. return 0;
  63. }
  64. int cmark_strbuf_grow(cmark_strbuf *buf, int target_size)
  65. {
  66. return cmark_strbuf_try_grow(buf, target_size, true);
  67. }
  68. bool cmark_strbuf_oom(const cmark_strbuf *buf)
  69. {
  70. return (buf->ptr == cmark_strbuf__oom);
  71. }
  72. size_t cmark_strbuf_len(const cmark_strbuf *buf)
  73. {
  74. return buf->size;
  75. }
  76. void cmark_strbuf_free(strbuf *buf)
  77. {
  78. if (!buf) return;
  79. if (buf->ptr != cmark_strbuf__initbuf && buf->ptr != cmark_strbuf__oom)
  80. free(buf->ptr);
  81. cmark_strbuf_init(buf, 0);
  82. }
  83. void cmark_strbuf_clear(strbuf *buf)
  84. {
  85. buf->size = 0;
  86. if (buf->asize > 0)
  87. buf->ptr[0] = '\0';
  88. }
  89. int cmark_strbuf_set(strbuf *buf, const unsigned char *data, int len)
  90. {
  91. if (len <= 0 || data == NULL) {
  92. cmark_strbuf_clear(buf);
  93. } else {
  94. if (data != buf->ptr) {
  95. ENSURE_SIZE(buf, len + 1);
  96. memmove(buf->ptr, data, len);
  97. }
  98. buf->size = len;
  99. buf->ptr[buf->size] = '\0';
  100. }
  101. return 0;
  102. }
  103. int cmark_strbuf_sets(strbuf *buf, const char *string)
  104. {
  105. return cmark_strbuf_set(buf,
  106. (const unsigned char *)string,
  107. string ? strlen(string) : 0);
  108. }
  109. int cmark_strbuf_putc(strbuf *buf, int c)
  110. {
  111. ENSURE_SIZE(buf, buf->size + 2);
  112. buf->ptr[buf->size++] = c;
  113. buf->ptr[buf->size] = '\0';
  114. return 0;
  115. }
  116. int cmark_strbuf_put(strbuf *buf, const unsigned char *data, int len)
  117. {
  118. if (len <= 0)
  119. return 0;
  120. ENSURE_SIZE(buf, buf->size + len + 1);
  121. memmove(buf->ptr + buf->size, data, len);
  122. buf->size += len;
  123. buf->ptr[buf->size] = '\0';
  124. return 0;
  125. }
  126. int cmark_strbuf_puts(strbuf *buf, const char *string)
  127. {
  128. return cmark_strbuf_put(buf, (const unsigned char *)string, strlen(string));
  129. }
  130. int cmark_strbuf_vprintf(strbuf *buf, const char *format, va_list ap)
  131. {
  132. const int expected_size = buf->size + (strlen(format) * 2);
  133. int len;
  134. ENSURE_SIZE(buf, expected_size);
  135. while (1) {
  136. va_list args;
  137. va_copy(args, ap);
  138. len = vsnprintf(
  139. (char *)buf->ptr + buf->size,
  140. buf->asize - buf->size,
  141. format, args
  142. );
  143. if (len < 0) {
  144. free(buf->ptr);
  145. buf->ptr = cmark_strbuf__oom;
  146. return -1;
  147. }
  148. if (len + 1 <= buf->asize - buf->size) {
  149. buf->size += len;
  150. break;
  151. }
  152. ENSURE_SIZE(buf, buf->size + len + 1);
  153. }
  154. return 0;
  155. }
  156. int cmark_strbuf_printf(strbuf *buf, const char *format, ...)
  157. {
  158. int r;
  159. va_list ap;
  160. va_start(ap, format);
  161. r = cmark_strbuf_vprintf(buf, format, ap);
  162. va_end(ap);
  163. return r;
  164. }
  165. static inline const char *cmark_strbuf_cstr(const cmark_strbuf *buf)
  166. {
  167. return (char *)buf->ptr;
  168. }
  169. void cmark_strbuf_copy_cstr(char *data, int datasize, const strbuf *buf)
  170. {
  171. int copylen;
  172. assert(data && datasize && buf);
  173. data[0] = '\0';
  174. if (buf->size == 0 || buf->asize <= 0)
  175. return;
  176. copylen = buf->size;
  177. if (copylen > datasize - 1)
  178. copylen = datasize - 1;
  179. memmove(data, buf->ptr, copylen);
  180. data[copylen] = '\0';
  181. }
  182. void cmark_strbuf_swap(strbuf *buf_a, strbuf *buf_b)
  183. {
  184. strbuf t = *buf_a;
  185. *buf_a = *buf_b;
  186. *buf_b = t;
  187. }
  188. unsigned char *cmark_strbuf_detach(strbuf *buf)
  189. {
  190. unsigned char *data = buf->ptr;
  191. if (buf->asize == 0 || buf->ptr == cmark_strbuf__oom) {
  192. /* return an empty string */
  193. return calloc(1, 1);
  194. }
  195. cmark_strbuf_init(buf, 0);
  196. return data;
  197. }
  198. void cmark_strbuf_attach(strbuf *buf, unsigned char *ptr, int asize)
  199. {
  200. cmark_strbuf_free(buf);
  201. if (ptr) {
  202. buf->ptr = ptr;
  203. buf->size = strlen((char *)ptr);
  204. if (asize)
  205. buf->asize = (asize < buf->size) ? buf->size + 1 : asize;
  206. else /* pass 0 to fall back on strlen + 1 */
  207. buf->asize = buf->size + 1;
  208. } else {
  209. cmark_strbuf_grow(buf, asize);
  210. }
  211. }
  212. int cmark_strbuf_cmp(const strbuf *a, const strbuf *b)
  213. {
  214. int result = memcmp(a->ptr, b->ptr, MIN(a->size, b->size));
  215. return (result != 0) ? result :
  216. (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0;
  217. }
  218. int cmark_strbuf_strchr(const strbuf *buf, int c, int pos)
  219. {
  220. const unsigned char *p = memchr(buf->ptr + pos, c, buf->size - pos);
  221. if (!p)
  222. return -1;
  223. return (int)(p - (const unsigned char *)buf->ptr);
  224. }
  225. int cmark_strbuf_strrchr(const strbuf *buf, int c, int pos)
  226. {
  227. int i;
  228. for (i = pos; i >= 0; i--) {
  229. if (buf->ptr[i] == (unsigned char) c)
  230. return i;
  231. }
  232. return -1;
  233. }
  234. void cmark_strbuf_truncate(strbuf *buf, int len)
  235. {
  236. if (len < buf->size) {
  237. buf->size = len;
  238. buf->ptr[buf->size] = '\0';
  239. }
  240. }
  241. void cmark_strbuf_drop(strbuf *buf, int n)
  242. {
  243. if (n > 0) {
  244. buf->size = buf->size - n;
  245. if (buf->size)
  246. memmove(buf->ptr, buf->ptr + n, buf->size);
  247. buf->ptr[buf->size] = '\0';
  248. }
  249. }
  250. void cmark_strbuf_rtrim(strbuf *buf)
  251. {
  252. if (!buf->size)
  253. return;
  254. while (buf->size > 0) {
  255. if (!isspace(buf->ptr[buf->size - 1]))
  256. break;
  257. buf->size--;
  258. }
  259. buf->ptr[buf->size] = '\0';
  260. }
  261. void cmark_strbuf_trim(strbuf *buf)
  262. {
  263. int i = 0;
  264. if (!buf->size)
  265. return;
  266. while (i < buf->size && isspace(buf->ptr[i]))
  267. i++;
  268. cmark_strbuf_drop(buf, i);
  269. cmark_strbuf_rtrim(buf);
  270. }
  271. // Destructively modify string, collapsing consecutive
  272. // space and newline characters into a single space.
  273. void cmark_strbuf_normalize_whitespace(strbuf *s)
  274. {
  275. bool last_char_was_space = false;
  276. int r, w;
  277. for (r = 0, w = 0; r < s->size; ++r) {
  278. switch (s->ptr[r]) {
  279. case ' ':
  280. case '\n':
  281. if (last_char_was_space)
  282. break;
  283. s->ptr[w++] = ' ';
  284. last_char_was_space = true;
  285. break;
  286. default:
  287. s->ptr[w++] = s->ptr[r];
  288. last_char_was_space = false;
  289. }
  290. }
  291. cmark_strbuf_truncate(s, w);
  292. }
  293. // Destructively unescape a string: remove backslashes before punctuation chars.
  294. extern void cmark_strbuf_unescape(strbuf *buf)
  295. {
  296. int r, w;
  297. for (r = 0, w = 0; r < buf->size; ++r) {
  298. if (buf->ptr[r] == '\\' && ispunct(buf->ptr[r + 1]))
  299. continue;
  300. buf->ptr[w++] = buf->ptr[r];
  301. }
  302. cmark_strbuf_truncate(buf, w);
  303. }