+/* Copies SRC into DST.
+ DST and SRC may be the same string. */
+void
+ds_assign_string (struct string *dst, const struct string *src)
+{
+ ds_assign_substring (dst, ds_ss (src));
+}
+
+/* Replaces DST by SS.
+ SS may be a substring of DST. */
+void
+ds_assign_substring (struct string *dst, struct substring ss)
+{
+ dst->ss.length = ss.length;
+ ds_extend (dst, ss.length);
+ memmove (dst->ss.string, ss.string, ss.length);
+}
+
+/* Replaces DST by null-terminated string SRC. SRC may overlap
+ with DST. */
+void
+ds_assign_cstr (struct string *dst, const char *src)
+{
+ ds_assign_substring (dst, ss_cstr (src));
+}
+
+/* Truncates ST to zero length. */
+void
+ds_clear (struct string *st)
+{
+ st->ss.length = 0;
+}
+
+/* Returns a substring that contains ST. */
+struct substring
+ds_ss (const struct string *st)
+{
+ return st->ss;
+}
+
+/* Returns a substring that contains CNT bytes from ST
+ starting at position START.
+
+ If START is greater than or equal to the length of ST, then
+ the substring will be the empty string. If START + CNT
+ exceeds the length of ST, then the substring will only be
+ ds_length(ST) - START bytes long. */
+struct substring
+ds_substr (const struct string *st, size_t start, size_t cnt)
+{
+ return ss_substr (ds_ss (st), start, cnt);
+}
+
+/* Returns a substring that contains the first CNT bytes in
+ ST. If CNT exceeds the length of ST, then the substring will
+ contain all of ST. */
+struct substring
+ds_head (const struct string *st, size_t cnt)
+{
+ return ss_head (ds_ss (st), cnt);
+}
+
+/* Returns a substring that contains the last CNT bytes in
+ ST. If CNT exceeds the length of ST, then the substring will
+ contain all of ST. */
+struct substring
+ds_tail (const struct string *st, size_t cnt)
+{
+ return ss_tail (ds_ss (st), cnt);
+}
+
+/* Ensures that ST can hold at least MIN_CAPACITY bytes plus a null
+ terminator. */
+void
+ds_extend (struct string *st, size_t min_capacity)
+{
+ if (min_capacity > st->capacity)
+ {
+ st->capacity *= 2;
+ if (st->capacity < min_capacity)
+ st->capacity = 2 * min_capacity;
+
+ st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
+ }
+}
+
+/* Shrink ST to the minimum capacity need to contain its content. */
+void
+ds_shrink (struct string *st)
+{
+ if (st->capacity != st->ss.length)
+ {
+ st->capacity = st->ss.length;
+ st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
+ }
+}
+
+/* Truncates ST to at most LENGTH bytes long. */
+void
+ds_truncate (struct string *st, size_t length)
+{
+ ss_truncate (&st->ss, length);
+}
+
+/* Removes trailing bytes in TRIM_SET from ST.
+ Returns number of bytes removed. */
+size_t
+ds_rtrim (struct string *st, struct substring trim_set)
+{
+ return ss_rtrim (&st->ss, trim_set);
+}
+
+/* Removes leading bytes in TRIM_SET from ST.
+ Returns number of bytes removed. */
+size_t
+ds_ltrim (struct string *st, struct substring trim_set)
+{
+ size_t cnt = ds_span (st, trim_set);
+ if (cnt > 0)
+ ds_assign_substring (st, ds_substr (st, cnt, SIZE_MAX));
+ return cnt;
+}
+
+/* Trims leading and trailing bytes in TRIM_SET from ST.
+ Returns number of bytes removed. */
+size_t
+ds_trim (struct string *st, struct substring trim_set)
+{
+ size_t cnt = ds_rtrim (st, trim_set);
+ return cnt + ds_ltrim (st, trim_set);
+}
+
+/* If the last byte in ST is C, removes it and returns true.
+ Otherwise, returns false without modifying ST. */
+bool
+ds_chomp (struct string *st, char c)
+{
+ return ss_chomp (&st->ss, c);
+}
+
+/* Divides ST into tokens separated by any of the DELIMITERS.
+ Each call replaces TOKEN by the next token in ST, or by an
+ empty string if no tokens remain. Returns true if a token was
+ obtained, false otherwise.
+
+ Before the first call, initialize *SAVE_IDX to 0. Do not
+ modify *SAVE_IDX between calls.
+
+ ST divides into exactly one more tokens than it contains
+ delimiters. That is, a delimiter at the start or end of ST or
+ a pair of adjacent delimiters yields an empty token, and the
+ empty string contains a single token. */
+bool
+ds_separate (const struct string *st, struct substring delimiters,
+ size_t *save_idx, struct substring *token)
+{
+ return ss_separate (ds_ss (st), delimiters, save_idx, token);
+}
+
+/* Divides ST into tokens separated by any of the DELIMITERS,
+ merging adjacent delimiters so that the empty string is never
+ produced as a token. Each call replaces TOKEN by the next
+ token in ST, or by an empty string if no tokens remain.
+ Returns true if a token was obtained, false otherwise.
+
+ Before the first call, initialize *SAVE_IDX to 0. Do not
+ modify *SAVE_IDX between calls. */
+bool
+ds_tokenize (const struct string *st, struct substring delimiters,
+ size_t *save_idx, struct substring *token)
+{
+ return ss_tokenize (ds_ss (st), delimiters, save_idx, token);
+}
+
+/* Pad ST on the right with copies of PAD until ST is at least
+ LENGTH bytes in size. If ST is initially LENGTH
+ bytes or longer, this is a no-op. */
+void
+ds_rpad (struct string *st, size_t length, char pad)
+{
+ if (length > st->ss.length)
+ ds_put_byte_multiple (st, pad, length - st->ss.length);
+}
+
+/* Sets the length of ST to exactly NEW_LENGTH,
+ either by truncating bytes from the end,
+ or by padding on the right with PAD. */
+void
+ds_set_length (struct string *st, size_t new_length, char pad)
+{
+ if (st->ss.length < new_length)
+ ds_rpad (st, new_length, pad);
+ else
+ st->ss.length = new_length;
+}
+
+/* Removes N bytes from ST starting at offset START. */
+void
+ds_remove (struct string *st, size_t start, size_t n)
+{
+ if (n > 0 && start < st->ss.length)
+ {
+ if (st->ss.length - start <= n)
+ {
+ /* All bytes at or beyond START are deleted. */
+ st->ss.length = start;
+ }
+ else
+ {
+ /* Some bytes remain and must be shifted into
+ position. */
+ memmove (st->ss.string + st->ss.length,
+ st->ss.string + st->ss.length + n,
+ st->ss.length - start - n);
+ st->ss.length -= n;
+ }
+ }
+ else
+ {
+ /* There are no bytes to delete or no bytes at or
+ beyond START, hence deletion is a no-op. */
+ }
+}
+
+/* Returns true if ST is empty, false otherwise. */
+bool
+ds_is_empty (const struct string *st)
+{
+ return ss_is_empty (st->ss);
+}
+
+/* Returns the length of ST. */
+size_t
+ds_length (const struct string *st)
+{
+ return ss_length (ds_ss (st));
+}
+
+/* Returns the string data inside ST. */
+char *
+ds_data (const struct string *st)
+{
+ return ss_data (ds_ss (st));
+}
+
+/* Returns a pointer to the null terminator ST.
+ This might not be an actual null byte unless ds_c_str() has
+ been called since the last modification to ST. */
+char *
+ds_end (const struct string *st)
+{
+ return ss_end (ds_ss (st));
+}
+
+/* Returns the byte in position IDX in ST, as a value in the
+ range of unsigned char. Returns EOF if IDX is out of the
+ range of indexes for ST. */
+int
+ds_at (const struct string *st, size_t idx)
+{
+ return ss_at (ds_ss (st), idx);
+}
+
+/* Returns the first byte in ST as a value in the range of
+ unsigned char. Returns EOF if ST is the empty string. */
+int
+ds_first (const struct string *st)
+{
+ return ss_first (ds_ss (st));
+}
+
+/* Returns the last byte in ST as a value in the range of
+ unsigned char. Returns EOF if ST is the empty string. */
+int
+ds_last (const struct string *st)
+{
+ return ss_last (ds_ss (st));
+}
+
+/* Returns the number of consecutive bytes at the beginning
+ of ST that are in SKIP_SET. */
+size_t
+ds_span (const struct string *st, struct substring skip_set)
+{
+ return ss_span (ds_ss (st), skip_set);
+}
+
+/* Returns the number of consecutive bytes at the beginning
+ of ST that are not in STOP_SET. */
+size_t
+ds_cspan (const struct string *st, struct substring stop_set)
+{
+ return ss_cspan (ds_ss (st), stop_set);
+}
+
+/* Returns the position of the first occurrence of byte C in
+ ST at or after position OFS, or SIZE_MAX if there is no such
+ occurrence. */
+size_t
+ds_find_byte (const struct string *st, char c)
+{
+ return ss_find_byte (ds_ss (st), c);
+}
+
+/* Compares A and B and returns a strcmp()-type comparison
+ result. */
+int
+ds_compare (const struct string *a, const struct string *b)
+{
+ return ss_compare (ds_ss (a), ds_ss (b));
+}
+
+/* Returns the position in ST that the byte at P occupies.
+ P must point within ST or one past its end. */
+size_t
+ds_pointer_to_position (const struct string *st, const char *p)
+{
+ return ss_pointer_to_position (ds_ss (st), p);
+}
+
+/* Allocates and returns a null-terminated string that contains
+ ST. */
+char *
+ds_xstrdup (const struct string *st)
+{
+ return ss_xstrdup (ds_ss (st));
+}
+
+/* Returns the allocation size of ST. */
+size_t
+ds_capacity (const struct string *st)
+{
+ return st->capacity;
+}
+
+/* Returns the value of ST as a null-terminated string. */