chiark / gitweb /
@@@ more mess
[mLib] / struct / dstr.c
index 3072c5756fc334de6732fcca7c74fa4c9ab069a7..69c90f9c7328affdb6e7579f61dbda60ac2d7527 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "alloc.h"
 #include "dstr.h"
 
 #include "alloc.h"
 #include "dstr.h"
+#include "growbuf.h"
 
 /*----- Tunable constants -------------------------------------------------*/
 
 
 /*----- Tunable constants -------------------------------------------------*/
 
@@ -92,40 +93,21 @@ void dstr_reset(dstr *d) { DRESET(d); }
 
 void dstr_ensure(dstr *d, size_t sz)
 {
 
 void dstr_ensure(dstr *d, size_t sz)
 {
-  size_t rq = d->len + sz;
-  size_t nsz;
-
-  /* --- If we have enough space, just leave it --- */
-
-  if (rq <= d->sz)
-    return;
-
-  /* --- Grow the buffer --- */
-
-  nsz = d->sz;
-
-  if (nsz == 0)
-    nsz = (DSTR_INITSZ >> 1);
-  do nsz <<= 1; while (nsz < rq);
-
-  if (d->buf)
-    d->buf = x_realloc(d->a, d->buf, nsz, d->sz);
-  else
-    d->buf = x_alloc(d->a, nsz);
-  d->sz = nsz;
+  GROWBUF_EXTEND(size_t, d->a, d->buf, d->sz, d->len + sz,
+                DSTR_INITSZ, 1);
 }
 
 /* --- @dstr_putc@ --- *
  *
  * Arguments:  @dstr *d@ = pointer to a dynamic string block
 }
 
 /* --- @dstr_putc@ --- *
  *
  * Arguments:  @dstr *d@ = pointer to a dynamic string block
- *             @char ch@ = character to append
+ *             @int ch@ = character to append
  *
  * Returns:    ---
  *
  * Use:                Appends a character to a string.
  */
 
  *
  * Returns:    ---
  *
  * Use:                Appends a character to a string.
  */
 
-void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
+void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); }
 
 /* --- @dstr_putz@ --- *
  *
 
 /* --- @dstr_putz@ --- *
  *