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