+ if (new_len != old_len)
+ {
+ if (new_len > old_len)
+ ds_extend (st, ds_length (st) + (new_len - old_len));
+ memmove (ds_data (st) + (ofs + new_len),
+ ds_data (st) + (ofs + old_len),
+ ds_length (st) - (ofs + old_len));
+ st->ss.length += new_len - old_len;
+ }
+ return ds_data (st) + ofs;
+}
+
+/* Formats FORMAT as a printf string and appends the result to ST. */
+void
+ds_put_format (struct string *st, const char *format, ...)
+{
+ va_list args;
+
+ va_start (args, format);
+ ds_put_vformat (st, format, args);
+ va_end (args);
+}
+
+/* Formats FORMAT as a printf string as if in the C locale and appends the result to ST. */
+void
+ds_put_c_format (struct string *st, const char *format, ...)
+{
+ va_list args;
+
+ va_start (args, format);
+ ds_put_c_vformat (st, format, args);
+ va_end (args);
+}
+
+
+/* Formats FORMAT as a printf string, using fmt_func (a snprintf like function)
+ and appends the result to ST. */
+static void
+ds_put_vformat_int (struct string *st, const char *format, va_list args_,
+ int (*fmt_func) (char *, size_t, const char *, va_list))
+{
+ int avail, needed;
+ va_list args;
+
+ va_copy (args, args_);
+ avail = st->ss.string != NULL ? st->capacity - st->ss.length + 1 : 0;
+ needed = fmt_func (st->ss.string + st->ss.length, avail, format, args);
+ va_end (args);
+
+ if (needed >= avail)
+ {
+ va_copy (args, args_);
+ fmt_func (ds_put_uninit (st, needed), needed + 1, format, args);
+ va_end (args);
+ }
+ else
+ {
+ /* Some old libc's returned -1 when the destination string
+ was too short. */
+ while (needed == -1)
+ {
+ ds_extend (st, (st->capacity + 1) * 2);
+ avail = st->capacity - st->ss.length + 1;
+
+ va_copy (args, args_);
+ needed = fmt_func (ds_end (st), avail, format, args);
+ va_end (args);
+ }
+ st->ss.length += needed;
+ }
+}
+
+
+static int
+vasnwrapper (char *str, size_t size, const char *format, va_list ap)
+{
+ c_vasnprintf (str, &size, format, ap);
+ return size;
+}
+
+/* Formats FORMAT as a printf string and appends the result to ST. */
+void
+ds_put_vformat (struct string *st, const char *format, va_list args_)
+{
+ ds_put_vformat_int (st, format, args_, vsnprintf);
+}
+
+/* Formats FORMAT as a printf string, as if in the C locale,
+ and appends the result to ST. */
+void
+ds_put_c_vformat (struct string *st, const char *format, va_list args_)
+{
+ ds_put_vformat_int (st, format, args_, vasnwrapper);
+}
+
+/* Appends byte CH to ST. */
+void
+ds_put_byte (struct string *st, int ch)
+{
+ ds_put_uninit (st, 1)[0] = ch;
+}
+
+/* Appends CNT copies of byte CH to ST. */
+void
+ds_put_byte_multiple (struct string *st, int ch, size_t cnt)
+{
+ memset (ds_put_uninit (st, cnt), ch, cnt);
+}
+
+/* Appends Unicode code point UC to ST in UTF-8 encoding. */
+void
+ds_put_unichar (struct string *st, ucs4_t uc)
+{
+ ds_extend (st, ds_length (st) + 6);
+ st->ss.length += u8_uctomb (CHAR_CAST (uint8_t *, ds_end (st)), uc, 6);
+}
+
+/* If relocation has been enabled, replace ST,
+ with its relocated version */
+void
+ds_relocate (struct string *st)
+{
+ const char *orig = ds_cstr (st);
+ const char *rel = relocate (orig);
+
+ if ( orig != rel)
+ {
+ ds_clear (st);
+ ds_put_cstr (st, rel);
+ /* The documentation for relocate says that casting away const
+ and then freeing is appropriate ... */
+ free (CONST_CAST (char *, rel));
+ }
+}
+
+
+\f
+
+/* Operations on uint8_t "strings" */
+
+/* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
+ DST is truncated to DST_SIZE bytes or padded on the right with
+ copies of PAD as needed. */
+void
+u8_buf_copy_rpad (uint8_t *dst, size_t dst_size,
+ const uint8_t *src, size_t src_size,
+ char pad)
+{
+ if (src_size >= dst_size)
+ memmove (dst, src, dst_size);
+ else
+ {
+ memmove (dst, src, src_size);
+ memset (&dst[src_size], pad, dst_size - src_size);
+ }