02110-1301, USA. */
#include <config.h>
+
#include "str.h"
-#include "message.h"
+
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
-#include "alloc.h"
-#include "message.h"
+
+#include <libpspp/alloc.h>
+#include <libpspp/message.h>
+
#include "minmax.h"
#include "size_max.h"
\f
strcpy (st->string, s);
}
-/* Initializes ST, making room for at least CAPACITY characters. */
+/* Initializes ST as an empty string. */
void
-ds_init (struct string *st, size_t capacity)
+ds_init (struct string *st)
{
st->length = 0;
- st->capacity = MAX (8, capacity);
- st->string = xmalloc (st->capacity + 1);
+ st->capacity = 0;
+ st->string = NULL;
}
/* Frees ST. */
const struct string *src, size_t idx, size_t cnt)
{
assert (dst != src);
- ds_init (dst, cnt);
+ ds_init (dst);
ds_assign_substring (dst, src, idx, cnt);
}
st->length += len;
}
+/* Returns ds_end(ST) and THEN increases the length by INCR. */
+char *
+ds_append_uninit(struct string *st, size_t incr)
+{
+ char *end;
+
+ ds_extend(st, ds_length(st) + incr);
+
+ end = ds_end(st);
+
+ st->length += incr;
+
+ return end;
+}
+
/* Formats FORMAT as a printf string and appends the result to ST. */
void
ds_printf (struct string *st, const char *format, ...)
int avail, needed;
va_list args;
-#ifndef va_copy
-#define va_copy(DST, SRC) (DST) = (SRC)
-#endif
-
va_copy (args, args_);
- avail = st->capacity - st->length + 1;
+ avail = st->string != NULL ? st->capacity - st->length + 1 : 0;
needed = vsnprintf (st->string + st->length, avail, format, args);
va_end (args);