- char *s = st->string;
- st->string = NULL;
- ds_create (st->pool, st, string);
- pool_free (st->pool, s);
+ size_t new_length = strlen (string);
+ if (new_length > st->capacity)
+ {
+ /* The new length is longer than the allocated length, so
+ there can be no overlap. */
+ st->length = 0;
+ ds_concat (st, string, new_length);
+ }
+ else
+ {
+ /* Overlap is possible, but the new string will fit in the
+ allocated space, so we can just copy data. */
+ st->length = new_length;
+ memmove (st->string, string, st->length);
+ }