86e7fd9199a5b9e22fba9fa2ac2eec4e9e7ff1c7
[pspp] / src / libpspp / str.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2014,
3    2020 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include "str.h"
21
22 #include <ctype.h>
23 #include <errno.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <unistr.h>
27
28 #include "libpspp/cast.h"
29 #include "libpspp/message.h"
30 #include "libpspp/pool.h"
31
32 #include "gl/c-ctype.h"
33 #include "gl/c-vasnprintf.h"
34 #include "gl/relocatable.h"
35 #include "gl/minmax.h"
36 #include "gl/xalloc.h"
37 #include "gl/xmemdup0.h"
38 #include "gl/xsize.h"
39 \f
40 /* Reverses the order of NBYTES bytes at address P, thus converting
41    between little- and big-endian byte orders.  */
42 void
43 buf_reverse (char *p, size_t nbytes)
44 {
45   char *h = p, *t = &h[nbytes - 1];
46   char temp;
47
48   nbytes /= 2;
49   while (nbytes--)
50     {
51       temp = *h;
52       *h++ = *t;
53       *t-- = temp;
54     }
55 }
56
57 /* Compares the SIZE bytes in A to those in B, disregarding case,
58    and returns a strcmp()-type result. */
59 int
60 buf_compare_case (const char *a_, const char *b_, size_t size)
61 {
62   const unsigned char *a = (unsigned char *) a_;
63   const unsigned char *b = (unsigned char *) b_;
64
65   while (size-- > 0)
66     {
67       unsigned char ac = toupper (*a++);
68       unsigned char bc = toupper (*b++);
69
70       if (ac != bc)
71         return ac > bc ? 1 : -1;
72     }
73
74   return 0;
75 }
76
77 /* Compares A of length A_LEN to B of length B_LEN.  The shorter
78    string is considered to be padded with spaces to the length of
79    the longer. */
80 int
81 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
82 {
83   size_t min_len;
84   int result;
85
86   min_len = a_len < b_len ? a_len : b_len;
87   result = memcmp (a, b, min_len);
88   if (result != 0)
89     return result;
90   else
91     {
92       size_t idx;
93
94       if (a_len < b_len)
95         {
96           for (idx = min_len; idx < b_len; idx++)
97             if (' ' != b[idx])
98               return ' ' > b[idx] ? 1 : -1;
99         }
100       else
101         {
102           for (idx = min_len; idx < a_len; idx++)
103             if (a[idx] != ' ')
104               return a[idx] > ' ' ? 1 : -1;
105         }
106       return 0;
107     }
108 }
109
110 /* Compares strin A to string B.  The shorter string is
111    considered to be padded with spaces to the length of the
112    longer. */
113 int
114 str_compare_rpad (const char *a, const char *b)
115 {
116   return buf_compare_rpad (a, strlen (a), b, strlen (b));
117 }
118
119 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
120    DST is truncated to DST_SIZE bytes or padded on the right with
121    copies of PAD as needed. */
122 void
123 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src, char pad)
124 {
125   size_t src_len = strlen (src);
126   if (src_len >= dst_size)
127     memcpy (dst, src, dst_size);
128   else
129     {
130       memcpy (dst, src, src_len);
131       memset (&dst[src_len], pad, dst_size - src_len);
132     }
133 }
134
135 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
136    DST is truncated to DST_SIZE bytes or padded on the left with
137    copies of PAD as needed. */
138 void
139 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src, char pad)
140 {
141   size_t src_len = strlen (src);
142   if (src_len >= dst_size)
143     memcpy (dst, src, dst_size);
144   else
145     {
146       size_t pad_cnt = dst_size - src_len;
147       memset (&dst[0], pad, pad_cnt);
148       memcpy (dst + pad_cnt, src, src_len);
149     }
150 }
151
152 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
153    DST is truncated to DST_SIZE bytes or padded on the left with
154    copies of PAD as needed. */
155 void
156 buf_copy_lpad (char *dst, size_t dst_size,
157                const char *src, size_t src_size,
158                char pad)
159 {
160   if (src_size >= dst_size)
161     memmove (dst, src, dst_size);
162   else
163     {
164       memset (dst, pad, dst_size - src_size);
165       memmove (&dst[dst_size - src_size], src, src_size);
166     }
167 }
168
169 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
170    DST is truncated to DST_SIZE bytes or padded on the right with
171    copies of PAD as needed. */
172 void
173 buf_copy_rpad (char *dst, size_t dst_size,
174                const char *src, size_t src_size,
175                char pad)
176 {
177   if (src_size >= dst_size)
178     memmove (dst, src, dst_size);
179   else
180     {
181       memmove (dst, src, src_size);
182       memset (&dst[src_size], pad, dst_size - src_size);
183     }
184 }
185
186 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
187    bytes long.
188    Truncates DST to DST_SIZE - 1 bytes or right-pads with
189    spaces to DST_SIZE - 1 bytes if necessary. */
190 void
191 str_copy_rpad (char *dst, size_t dst_size, const char *src)
192 {
193   if (dst_size > 0)
194     {
195       size_t src_len = strlen (src);
196       if (src_len < dst_size - 1)
197         {
198           memcpy (dst, src, src_len);
199           memset (&dst[src_len], ' ', dst_size - 1 - src_len);
200         }
201       else
202         memcpy (dst, src, dst_size - 1);
203       dst[dst_size - 1] = 0;
204     }
205 }
206
207 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
208    Truncates DST to DST_SIZE - 1 bytes, if necessary. */
209 void
210 str_copy_trunc (char *dst, size_t dst_size, const char *src)
211 {
212   size_t src_len = strlen (src);
213   assert (dst_size > 0);
214   if (src_len + 1 < dst_size)
215     memcpy (dst, src, src_len + 1);
216   else
217     {
218       memcpy (dst, src, dst_size - 1);
219       dst[dst_size - 1] = '\0';
220     }
221 }
222
223 /* Copies buffer SRC, of SRC_LEN bytes,
224    to DST, which is in a buffer DST_SIZE bytes long.
225    Truncates DST to DST_SIZE - 1 bytes, if necessary. */
226 void
227 str_copy_buf_trunc (char *dst, size_t dst_size,
228                     const char *src, size_t src_size)
229 {
230   size_t dst_len;
231   assert (dst_size > 0);
232
233   dst_len = src_size < dst_size ? src_size : dst_size - 1;
234   memcpy (dst, src, dst_len);
235   dst[dst_len] = '\0';
236 }
237
238 /* Converts each byte in S to uppercase.
239
240    This is suitable only for ASCII strings.  Use utf8_to_upper() for UTF-8
241    strings.*/
242 void
243 str_uppercase (char *s)
244 {
245   for (; *s != '\0'; s++)
246     *s = c_toupper ((unsigned char) *s);
247 }
248
249 /* Converts each byte in S to lowercase.
250
251    This is suitable only for ASCII strings.  Use utf8_to_lower() for UTF-8
252    strings.*/
253 void
254 str_lowercase (char *s)
255 {
256   for (; *s != '\0'; s++)
257     *s = c_tolower ((unsigned char) *s);
258 }
259
260 /* Converts NUMBER into a string in 26-adic notation in BUFFER,
261    which has room for SIZE bytes.  Uses uppercase if UPPERCASE is
262    true, otherwise lowercase, Returns true if successful, false
263    if NUMBER, plus a trailing null, is too large to fit in the
264    available space.
265
266    26-adic notation is "spreadsheet column numbering": 1 = A, 2 =
267    B, 3 = C, ... 26 = Z, 27 = AA, 28 = AB, 29 = AC, ...
268
269    26-adic notation is the special case of a k-adic numeration
270    system (aka bijective base-k numeration) with k=26.  In k-adic
271    numeration, the digits are {1, 2, 3, ..., k} (there is no
272    digit 0), and integer 0 is represented by the empty string.
273    For more information, see
274    http://en.wikipedia.org/wiki/Bijective_numeration. */
275 bool
276 str_format_26adic (unsigned long int number, bool uppercase,
277                    char buffer[], size_t size)
278 {
279   const char *alphabet
280     = uppercase ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "abcdefghijklmnopqrstuvwxyz";
281   size_t length = 0;
282
283   while (number-- > 0)
284     {
285       if (length >= size)
286         goto overflow;
287       buffer[length++] = alphabet[number % 26];
288       number /= 26;
289     }
290
291   if (length >= size)
292     goto overflow;
293   buffer[length] = '\0';
294
295   buf_reverse (buffer, length);
296   return true;
297
298 overflow:
299   if (length > 0)
300     buffer[0] = '\0';
301   return false;
302 }
303
304 /* Copies IN to buffer OUT with size OUT_SIZE, appending a null terminator.  If
305    IN is too long for OUT, or if IN contains a new-line, replaces the tail with
306    "...".
307
308    OUT_SIZE must be at least 16. */
309 void
310 str_ellipsize (struct substring in, char *out, size_t out_size)
311 {
312   assert (out_size >= 16);
313
314   size_t out_maxlen = out_size - 1;
315   if (in.length > out_maxlen - 3)
316     out_maxlen -= 3;
317
318   size_t out_len = 0;
319   while (out_len < in.length
320          && in.string[out_len] != '\n'
321          && in.string[out_len] != '\0'
322          && (in.string[out_len] != '\r'
323              || out_len + 1 >= in.length
324              || in.string[out_len + 1] != '\n'))
325     {
326       int mblen = u8_mblen (CHAR_CAST (const uint8_t *, in.string + out_len),
327                             in.length - out_len);
328       if (mblen < 0 || out_len + mblen > out_maxlen)
329         break;
330       out_len += mblen;
331     }
332
333   memcpy (out, in.string, out_len);
334   strcpy (&out[out_len], out_len < in.length ? "..." : "");
335 }
336
337 /* Sets the SIZE bytes starting at BLOCK to C,
338    and returns the byte following BLOCK. */
339 void *
340 mempset (void *block, int c, size_t size)
341 {
342   memset (block, c, size);
343   return (char *) block + size;
344 }
345 \f
346 /* Substrings. */
347
348 /* Returns a substring whose contents are the CNT bytes
349    starting at the (0-based) position START in SS. */
350 struct substring
351 ss_substr (struct substring ss, size_t start, size_t cnt)
352 {
353   if (start < ss.length)
354     return ss_buffer (ss.string + start, MIN (cnt, ss.length - start));
355   else
356     return ss_buffer (ss.string + ss.length, 0);
357 }
358
359 /* Returns a substring whose contents are the first CNT
360    bytes in SS. */
361 struct substring
362 ss_head (struct substring ss, size_t cnt)
363 {
364   return ss_buffer (ss.string, MIN (cnt, ss.length));
365 }
366
367 /* Returns a substring whose contents are the last CNT bytes
368    in SS. */
369 struct substring
370 ss_tail (struct substring ss, size_t cnt)
371 {
372   if (cnt < ss.length)
373     return ss_buffer (ss.string + (ss.length - cnt), cnt);
374   else
375     return ss;
376 }
377
378 /* Makes a malloc()'d, null-terminated copy of the contents of OLD
379    and stores it in NEW. */
380 void
381 ss_alloc_substring (struct substring *new, struct substring old)
382 {
383   new->string = xmemdup0 (old.string, old.length);
384   new->length = old.length;
385 }
386
387 /* Allocates room for a CNT-byte string in NEW. */
388 void
389 ss_alloc_uninit (struct substring *new, size_t cnt)
390 {
391   new->string = xmalloc (cnt);
392   new->length = cnt;
393 }
394
395 void
396 ss_realloc (struct substring *ss, size_t size)
397 {
398   ss->string = xrealloc (ss->string, size);
399 }
400
401 /* Makes a pool_alloc_unaligned()'d, null-terminated copy of the contents of
402    OLD in POOL, and stores it in NEW. */
403 void
404 ss_alloc_substring_pool (struct substring *new, struct substring old,
405                          struct pool *pool)
406 {
407   new->string = pool_alloc_unaligned (pool, old.length + 1);
408   new->length = old.length;
409   memcpy (new->string, old.string, old.length);
410   new->string[old.length] = '\0';
411 }
412
413 /* Allocates room for a CNT-byte string in NEW in POOL. */
414 void
415 ss_alloc_uninit_pool (struct substring *new, size_t cnt, struct pool *pool)
416 {
417   new->string = pool_alloc_unaligned (pool, cnt);
418   new->length = cnt;
419 }
420
421 /* Frees the string that SS points to. */
422 void
423 ss_dealloc (struct substring *ss)
424 {
425   free (ss->string);
426 }
427
428 /* Exchanges the contents of A and B. */
429 void
430 ss_swap (struct substring *a, struct substring *b)
431 {
432   struct substring tmp = *a;
433   *a = *b;
434   *b = tmp;
435 }
436
437 /* Truncates SS to at most CNT bytes in length. */
438 void
439 ss_truncate (struct substring *ss, size_t cnt)
440 {
441   if (ss->length > cnt)
442     ss->length = cnt;
443 }
444
445 /* Removes trailing bytes in TRIM_SET from SS.
446    Returns number of bytes removed. */
447 size_t
448 ss_rtrim (struct substring *ss, struct substring trim_set)
449 {
450   size_t cnt = 0;
451   while (cnt < ss->length
452          && ss_find_byte (trim_set,
453                           ss->string[ss->length - cnt - 1]) != SIZE_MAX)
454     cnt++;
455   ss->length -= cnt;
456   return cnt;
457 }
458
459 /* Removes leading bytes in TRIM_SET from SS.
460    Returns number of bytes removed. */
461 size_t
462 ss_ltrim (struct substring *ss, struct substring trim_set)
463 {
464   size_t cnt = ss_span (*ss, trim_set);
465   ss_advance (ss, cnt);
466   return cnt;
467 }
468
469 /* Trims leading and trailing bytes in TRIM_SET from SS. */
470 void
471 ss_trim (struct substring *ss, struct substring trim_set)
472 {
473   ss_ltrim (ss, trim_set);
474   ss_rtrim (ss, trim_set);
475 }
476
477 /* If the last byte in SS is C, removes it and returns true.
478    Otherwise, returns false without changing the string. */
479 bool
480 ss_chomp_byte (struct substring *ss, char c)
481 {
482   if (ss_last (*ss) == c)
483     {
484       ss->length--;
485       return true;
486     }
487   else
488     return false;
489 }
490
491 /* If SS ends with SUFFIX, removes it and returns true.
492    Otherwise, returns false without changing the string. */
493 bool
494 ss_chomp (struct substring *ss, struct substring suffix)
495 {
496   if (ss_ends_with (*ss, suffix))
497     {
498       ss->length -= suffix.length;
499       return true;
500     }
501   else
502     return false;
503 }
504
505 /* Divides SS into tokens separated by any of the DELIMITERS.
506    Each call replaces TOKEN by the next token in SS, or by an
507    empty string if no tokens remain.  Returns true if a token was
508    obtained, false otherwise.
509
510    Before the first call, initialize *SAVE_IDX to 0.  Do not
511    modify *SAVE_IDX between calls.
512
513    SS divides into exactly one more tokens than it contains
514    delimiters.  That is, a delimiter at the start or end of SS or
515    a pair of adjacent delimiters yields an empty token, and the
516    empty string contains a single token. */
517 bool
518 ss_separate (struct substring ss, struct substring delimiters,
519              size_t *save_idx, struct substring *token)
520 {
521   if (*save_idx <= ss_length (ss))
522     {
523       struct substring tmp = ss_substr (ss, *save_idx, SIZE_MAX);
524       size_t length = ss_cspan (tmp, delimiters);
525       *token = ss_head (tmp, length);
526       *save_idx += length + 1;
527       return true;
528     }
529   else
530     {
531       *token = ss_empty ();
532       return false;
533     }
534 }
535
536 /* Divides SS into tokens separated by any of the DELIMITERS,
537    merging adjacent delimiters so that the empty string is never
538    produced as a token.  Each call replaces TOKEN by the next
539    token in SS, or by an empty string if no tokens remain, and
540    then skips past the first delimiter following the token.
541    Returns true if a token was obtained, false otherwise.
542
543    Before the first call, initialize *SAVE_IDX to 0.  Do not
544    modify *SAVE_IDX between calls. */
545 bool
546 ss_tokenize (struct substring ss, struct substring delimiters,
547              size_t *save_idx, struct substring *token)
548 {
549   bool found_token;
550
551   ss_advance (&ss, *save_idx);
552   *save_idx += ss_ltrim (&ss, delimiters);
553   ss_get_bytes (&ss, ss_cspan (ss, delimiters), token);
554
555   found_token = ss_length (*token) > 0;
556   *save_idx += ss_length (*token) + (found_token?1:0);
557   return found_token;
558 }
559
560 /* Removes the first CNT bytes from SS. */
561 void
562 ss_advance (struct substring *ss, size_t cnt)
563 {
564   if (cnt > ss->length)
565     cnt = ss->length;
566   ss->string += cnt;
567   ss->length -= cnt;
568 }
569
570 /* If the first byte in SS is C, removes it and returns true.
571    Otherwise, returns false without changing the string. */
572 bool
573 ss_match_byte (struct substring *ss, char c)
574 {
575   if (ss_first (*ss) == c)
576     {
577       ss->string++;
578       ss->length--;
579       return true;
580     }
581   else
582     return false;
583 }
584
585 /* If the first byte in SS is in MATCH, removes it and
586    returns the byte that was removed.
587    Otherwise, returns EOF without changing the string. */
588 int
589 ss_match_byte_in (struct substring *ss, struct substring match)
590 {
591   int c = EOF;
592   if (ss->length > 0
593       && memchr (match.string, ss->string[0], match.length) != NULL)
594     {
595       c = ss->string[0];
596       ss->string++;
597       ss->length--;
598     }
599   return c;
600 }
601
602 /* If SS begins with TARGET, removes it and returns true.
603    Otherwise, returns false without changing SS. */
604 bool
605 ss_match_string (struct substring *ss, const struct substring target)
606 {
607   size_t length = ss_length (target);
608   if (ss_equals (ss_head (*ss, length), target))
609     {
610       ss_advance (ss, length);
611       return true;
612     }
613   else
614     return false;
615 }
616
617 /* Removes the first byte from SS and returns it.
618    If SS is empty, returns EOF without modifying SS. */
619 int
620 ss_get_byte (struct substring *ss)
621 {
622   int c = ss_first (*ss);
623   if (c != EOF)
624     {
625       ss->string++;
626       ss->length--;
627     }
628   return c;
629 }
630
631 /* Stores the prefix of SS up to the first DELIMITER in OUT (if
632    any).  Trims those same bytes from SS.  DELIMITER is
633    removed from SS but not made part of OUT.  Returns true if
634    DELIMITER was found (and removed), false otherwise. */
635 bool
636 ss_get_until (struct substring *ss, char delimiter, struct substring *out)
637 {
638   ss_get_bytes (ss, ss_cspan (*ss, ss_buffer (&delimiter, 1)), out);
639   return ss_match_byte (ss, delimiter);
640 }
641
642 /* Stores the first CNT bytes in SS in OUT (or fewer, if SS
643    is shorter than CNT bytes).  Trims the same bytes
644    from the beginning of SS.  Returns CNT. */
645 size_t
646 ss_get_bytes (struct substring *ss, size_t cnt, struct substring *out)
647 {
648   *out = ss_head (*ss, cnt);
649   ss_advance (ss, cnt);
650   return cnt;
651 }
652
653 /* Parses and removes an optionally signed decimal integer from
654    the beginning of SS.  Returns 0 if an error occurred,
655    otherwise the number of bytes removed from SS.  Stores
656    the integer's value into *VALUE. */
657 size_t
658 ss_get_long (struct substring *ss, long *value)
659 {
660   char tmp[64];
661   size_t length;
662
663   length = ss_span (*ss, ss_cstr ("+-"));
664   length += ss_span (ss_substr (*ss, length, SIZE_MAX), ss_cstr (CC_DIGITS));
665   if (length > 0 && length < sizeof tmp)
666     {
667       char *tail;
668
669       memcpy (tmp, ss_data (*ss), length);
670       tmp[length] = '\0';
671
672       *value = strtol (tmp, &tail, 10);
673       if (tail - tmp == length)
674         {
675           ss_advance (ss, length);
676           return length;
677         }
678     }
679   *value = 0;
680   return 0;
681 }
682
683 /* Returns true if SS is empty (has length 0 bytes),
684    false otherwise. */
685 bool
686 ss_is_empty (struct substring ss)
687 {
688   return ss.length == 0;
689 }
690
691 /* Returns the number of bytes in SS. */
692 size_t
693 ss_length (struct substring ss)
694 {
695   return ss.length;
696 }
697
698 /* Returns a pointer to the bytes in SS. */
699 char *
700 ss_data (struct substring ss)
701 {
702   return ss.string;
703 }
704
705 /* Returns a pointer just past the last byte in SS. */
706 char *
707 ss_end (struct substring ss)
708 {
709   return ss.string + ss.length;
710 }
711
712 /* Returns the byte in position IDX in SS, as a value in the
713    range of unsigned char.  Returns EOF if IDX is out of the
714    range of indexes for SS. */
715 int
716 ss_at (struct substring ss, size_t idx)
717 {
718   return idx < ss.length ? (unsigned char) ss.string[idx] : EOF;
719 }
720
721 /* Returns the first byte in SS as a value in the range of
722    unsigned char.  Returns EOF if SS is the empty string. */
723 int
724 ss_first (struct substring ss)
725 {
726   return ss_at (ss, 0);
727 }
728
729 /* Returns the last byte in SS as a value in the range of
730    unsigned char.  Returns EOF if SS is the empty string. */
731 int
732 ss_last (struct substring ss)
733 {
734   return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF;
735 }
736
737 /* Returns true if SS starts with PREFIX, false otherwise. */
738 bool
739 ss_starts_with (struct substring ss, struct substring prefix)
740 {
741   return (ss.length >= prefix.length
742           && !memcmp (ss.string, prefix.string, prefix.length));
743 }
744
745 /* Returns true if SS ends with SUFFIX, false otherwise. */
746 bool
747 ss_ends_with (struct substring ss, struct substring suffix)
748 {
749   return (ss.length >= suffix.length
750           && !memcmp (&ss.string[ss.length - suffix.length], suffix.string,
751                       suffix.length));
752 }
753
754 /* Returns the number of contiguous bytes at the beginning
755    of SS that are in SKIP_SET. */
756 size_t
757 ss_span (struct substring ss, struct substring skip_set)
758 {
759   size_t i;
760   for (i = 0; i < ss.length; i++)
761     if (ss_find_byte (skip_set, ss.string[i]) == SIZE_MAX)
762       break;
763   return i;
764 }
765
766 /* Returns the number of contiguous bytes at the beginning
767    of SS that are not in SKIP_SET. */
768 size_t
769 ss_cspan (struct substring ss, struct substring stop_set)
770 {
771   size_t i;
772   for (i = 0; i < ss.length; i++)
773     if (ss_find_byte (stop_set, ss.string[i]) != SIZE_MAX)
774       break;
775   return i;
776 }
777
778 /* Returns the offset in SS of the first instance of C,
779    or SIZE_MAX if C does not occur in SS. */
780 size_t
781 ss_find_byte (struct substring ss, char c)
782 {
783   const char *p = memchr (ss.string, (int) c, ss.length);
784   return p != NULL ? p - ss.string : SIZE_MAX;
785 }
786
787 /* Returns the offset in HAYSTACK of the first instance of NEEDLE,
788    or SIZE_MAX if NEEDLE does not occur in HAYSTACK. */
789 size_t
790 ss_find_substring (struct substring haystack, struct substring needle)
791 {
792   const char *p = memmem (haystack.string, haystack.length,
793                           needle.string, needle.length);
794   return p != NULL ? p - haystack.string : SIZE_MAX;
795 }
796
797 /* Compares A and B and returns a strcmp()-type comparison
798    result. */
799 int
800 ss_compare (struct substring a, struct substring b)
801 {
802   int retval = memcmp (a.string, b.string, MIN (a.length, b.length));
803   if (retval == 0)
804     retval = a.length < b.length ? -1 : a.length > b.length;
805   return retval;
806 }
807
808 /* Compares A and B case-insensitively and returns a
809    strcmp()-type comparison result. */
810 int
811 ss_compare_case (struct substring a, struct substring b)
812 {
813   int retval = memcasecmp (a.string, b.string, MIN (a.length, b.length));
814   if (retval == 0)
815     retval = a.length < b.length ? -1 : a.length > b.length;
816   return retval;
817 }
818
819 /* Compares A and B and returns true if their contents are
820    identical, false otherwise. */
821 int
822 ss_equals (struct substring a, struct substring b)
823 {
824   return a.length == b.length && !memcmp (a.string, b.string, a.length);
825 }
826
827 /* Compares A and B and returns true if their contents are
828    identical except possibly for case differences, false
829    otherwise. */
830 int
831 ss_equals_case (struct substring a, struct substring b)
832 {
833   return a.length == b.length && !memcasecmp (a.string, b.string, a.length);
834 }
835
836 /* Returns the position in SS that the byte at P occupies.
837    P must point within SS or one past its end. */
838 size_t
839 ss_pointer_to_position (struct substring ss, const char *p)
840 {
841   size_t pos = p - ss.string;
842   assert (pos <= ss.length);
843   return pos;
844 }
845
846 /* Allocates and returns a null-terminated string that contains
847    SS. */
848 char *
849 ss_xstrdup (struct substring ss)
850 {
851   char *s = xmalloc (ss.length + 1);
852   memcpy (s, ss.string, ss.length);
853   s[ss.length] = '\0';
854   return s;
855 }
856 /* UTF-8. */
857
858 /* Returns the character represented by the UTF-8 sequence at the start of S.
859    The return value is either a Unicode code point in the range 0 to 0x10ffff,
860    or UINT32_MAX if S is empty. */
861 ucs4_t
862 ss_first_mb (struct substring s)
863 {
864   return ss_at_mb (s, 0);
865 }
866
867 /* Returns the number of bytes in the UTF-8 character at the beginning of S.
868
869    The return value is 0 if S is empty, otherwise between 1 and 4. */
870 int
871 ss_first_mblen (struct substring s)
872 {
873   return ss_at_mblen (s, 0);
874 }
875
876 /* Advances S past the UTF-8 character at its beginning.  Returns the Unicode
877    code point that was skipped (in the range 0 to 0x10ffff), or UINT32_MAX if S
878    was not modified because it was initially empty. */
879 ucs4_t
880 ss_get_mb (struct substring *s)
881 {
882   if (s->length > 0)
883     {
884       ucs4_t uc;
885       int n;
886
887       n = u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s->string), s->length);
888       s->string += n;
889       s->length -= n;
890       return uc;
891     }
892   else
893     return UINT32_MAX;
894 }
895
896 /* Returns the character represented by the UTF-8 sequence starting OFS bytes
897    into S.  The return value is either a Unicode code point in the range 0 to
898    0x10ffff, or UINT32_MAX if OFS is past the last byte in S.
899
900    (Returns 0xfffd if OFS points into the middle, not the beginning, of a UTF-8
901    sequence.)  */
902 ucs4_t
903 ss_at_mb (struct substring s, size_t ofs)
904 {
905   if (s.length > ofs)
906     {
907       ucs4_t uc;
908       u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s.string + ofs),
909                  s.length - ofs);
910       return uc;
911     }
912   else
913     return UINT32_MAX;
914 }
915
916 /* Returns the number of bytes represented by the UTF-8 sequence starting OFS
917    bytes into S.  The return value is 0 if OFS is past the last byte in S,
918    otherwise between 1 and 4. */
919 int
920 ss_at_mblen (struct substring s, size_t ofs)
921 {
922   if (s.length > ofs)
923     {
924       ucs4_t uc;
925       return u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s.string + ofs),
926                         s.length - ofs);
927     }
928   else
929     return 0;
930 }
931 \f
932 /* Initializes ST as an empty string. */
933 void
934 ds_init_empty (struct string *st)
935 {
936   st->ss = ss_empty ();
937   st->capacity = 0;
938 }
939
940 /* Initializes ST with initial contents S. */
941 void
942 ds_init_string (struct string *st, const struct string *s)
943 {
944   ds_init_substring (st, ds_ss (s));
945 }
946
947 /* Initializes ST with initial contents SS. */
948 void
949 ds_init_substring (struct string *st, struct substring ss)
950 {
951   st->capacity = MAX (8, ss.length * 2);
952   st->ss.string = xmalloc (st->capacity + 1);
953   memcpy (st->ss.string, ss.string, ss.length);
954   st->ss.length = ss.length;
955 }
956
957 /* Initializes ST with initial contents S. */
958 void
959 ds_init_cstr (struct string *st, const char *s)
960 {
961   ds_init_substring (st, ss_cstr (s));
962 }
963
964 /* Frees ST. */
965 void
966 ds_destroy (struct string *st)
967 {
968   if (st != NULL)
969     {
970       ss_dealloc (&st->ss);
971       st->ss.string = NULL;
972       st->ss.length = 0;
973       st->capacity = 0;
974     }
975 }
976
977 /* Swaps the contents of strings A and B. */
978 void
979 ds_swap (struct string *a, struct string *b)
980 {
981   struct string tmp = *a;
982   *a = *b;
983   *b = tmp;
984 }
985
986 /* Helper function for ds_register_pool. */
987 static void
988 free_string (void *st_)
989 {
990   struct string *st = st_;
991   ds_destroy (st);
992 }
993
994 /* Arranges for ST to be destroyed automatically as part of
995    POOL. */
996 void
997 ds_register_pool (struct string *st, struct pool *pool)
998 {
999   pool_register (pool, free_string, st);
1000 }
1001
1002 /* Cancels the arrangement for ST to be destroyed automatically
1003    as part of POOL. */
1004 void
1005 ds_unregister_pool (struct string *st, struct pool *pool)
1006 {
1007   pool_unregister (pool, st);
1008 }
1009
1010 /* Copies SRC into DST.
1011    DST and SRC may be the same string. */
1012 void
1013 ds_assign_string (struct string *dst, const struct string *src)
1014 {
1015   ds_assign_substring (dst, ds_ss (src));
1016 }
1017
1018 /* Replaces DST by SS.
1019    SS may be a substring of DST. */
1020 void
1021 ds_assign_substring (struct string *dst, struct substring ss)
1022 {
1023   dst->ss.length = ss.length;
1024   ds_extend (dst, ss.length);
1025   memmove (dst->ss.string, ss.string, ss.length);
1026 }
1027
1028 /* Replaces DST by null-terminated string SRC.  SRC may overlap
1029    with DST. */
1030 void
1031 ds_assign_cstr (struct string *dst, const char *src)
1032 {
1033   ds_assign_substring (dst, ss_cstr (src));
1034 }
1035
1036 /* Truncates ST to zero length. */
1037 void
1038 ds_clear (struct string *st)
1039 {
1040   st->ss.length = 0;
1041 }
1042
1043 /* Returns a substring that contains ST. */
1044 struct substring
1045 ds_ss (const struct string *st)
1046 {
1047   return st->ss;
1048 }
1049
1050 /* Returns a substring that contains CNT bytes from ST
1051    starting at position START.
1052
1053    If START is greater than or equal to the length of ST, then
1054    the substring will be the empty string.  If START + CNT
1055    exceeds the length of ST, then the substring will only be
1056    ds_length(ST) - START bytes long. */
1057 struct substring
1058 ds_substr (const struct string *st, size_t start, size_t cnt)
1059 {
1060   return ss_substr (ds_ss (st), start, cnt);
1061 }
1062
1063 /* Returns a substring that contains the first CNT bytes in
1064    ST.  If CNT exceeds the length of ST, then the substring will
1065    contain all of ST. */
1066 struct substring
1067 ds_head (const struct string *st, size_t cnt)
1068 {
1069   return ss_head (ds_ss (st), cnt);
1070 }
1071
1072 /* Returns a substring that contains the last CNT bytes in
1073    ST.  If CNT exceeds the length of ST, then the substring will
1074    contain all of ST. */
1075 struct substring
1076 ds_tail (const struct string *st, size_t cnt)
1077 {
1078   return ss_tail (ds_ss (st), cnt);
1079 }
1080
1081 /* Ensures that ST can hold at least MIN_CAPACITY bytes plus a null
1082    terminator. */
1083 void
1084 ds_extend (struct string *st, size_t min_capacity)
1085 {
1086   if (min_capacity > st->capacity)
1087     {
1088       st->capacity *= 2;
1089       if (st->capacity < min_capacity)
1090         st->capacity = 2 * min_capacity;
1091
1092       st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
1093     }
1094 }
1095
1096 /* Shrink ST to the minimum capacity need to contain its content. */
1097 void
1098 ds_shrink (struct string *st)
1099 {
1100   if (st->capacity != st->ss.length)
1101     {
1102       st->capacity = st->ss.length;
1103       st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
1104     }
1105 }
1106
1107 /* Truncates ST to at most LENGTH bytes long. */
1108 void
1109 ds_truncate (struct string *st, size_t length)
1110 {
1111   ss_truncate (&st->ss, length);
1112 }
1113
1114 /* Removes trailing bytes in TRIM_SET from ST.
1115    Returns number of bytes removed. */
1116 size_t
1117 ds_rtrim (struct string *st, struct substring trim_set)
1118 {
1119   return ss_rtrim (&st->ss, trim_set);
1120 }
1121
1122 /* Removes leading bytes in TRIM_SET from ST.
1123    Returns number of bytes removed. */
1124 size_t
1125 ds_ltrim (struct string *st, struct substring trim_set)
1126 {
1127   size_t cnt = ds_span (st, trim_set);
1128   if (cnt > 0)
1129     ds_assign_substring (st, ds_substr (st, cnt, SIZE_MAX));
1130   return cnt;
1131 }
1132
1133 /* Trims leading and trailing bytes in TRIM_SET from ST.
1134    Returns number of bytes removed. */
1135 size_t
1136 ds_trim (struct string *st, struct substring trim_set)
1137 {
1138   size_t cnt = ds_rtrim (st, trim_set);
1139   return cnt + ds_ltrim (st, trim_set);
1140 }
1141
1142 /* If the last byte in ST is C, removes it and returns true.
1143    Otherwise, returns false without modifying ST. */
1144 bool
1145 ds_chomp_byte (struct string *st, char c)
1146 {
1147   return ss_chomp_byte (&st->ss, c);
1148 }
1149
1150 /* If ST ends with SUFFIX, removes it and returns true.
1151    Otherwise, returns false without modifying ST. */
1152 bool
1153 ds_chomp (struct string *st, struct substring suffix)
1154 {
1155   return ss_chomp (&st->ss, suffix);
1156 }
1157
1158 /* Divides ST into tokens separated by any of the DELIMITERS.
1159    Each call replaces TOKEN by the next token in ST, or by an
1160    empty string if no tokens remain.  Returns true if a token was
1161    obtained, false otherwise.
1162
1163    Before the first call, initialize *SAVE_IDX to 0.  Do not
1164    modify *SAVE_IDX between calls.
1165
1166    ST divides into exactly one more tokens than it contains
1167    delimiters.  That is, a delimiter at the start or end of ST or
1168    a pair of adjacent delimiters yields an empty token, and the
1169    empty string contains a single token. */
1170 bool
1171 ds_separate (const struct string *st, struct substring delimiters,
1172              size_t *save_idx, struct substring *token)
1173 {
1174   return ss_separate (ds_ss (st), delimiters, save_idx, token);
1175 }
1176
1177 /* Divides ST into tokens separated by any of the DELIMITERS,
1178    merging adjacent delimiters so that the empty string is never
1179    produced as a token.  Each call replaces TOKEN by the next
1180    token in ST, or by an empty string if no tokens remain.
1181    Returns true if a token was obtained, false otherwise.
1182
1183    Before the first call, initialize *SAVE_IDX to 0.  Do not
1184    modify *SAVE_IDX between calls. */
1185 bool
1186 ds_tokenize (const struct string *st, struct substring delimiters,
1187              size_t *save_idx, struct substring *token)
1188 {
1189   return ss_tokenize (ds_ss (st), delimiters, save_idx, token);
1190 }
1191
1192 /* Pad ST on the right with copies of PAD until ST is at least
1193    LENGTH bytes in size.  If ST is initially LENGTH
1194    bytes or longer, this is a no-op. */
1195 void
1196 ds_rpad (struct string *st, size_t length, char pad)
1197 {
1198   if (length > st->ss.length)
1199     ds_put_byte_multiple (st, pad, length - st->ss.length);
1200 }
1201
1202 /* Sets the length of ST to exactly NEW_LENGTH,
1203    either by truncating bytes from the end,
1204    or by padding on the right with PAD. */
1205 void
1206 ds_set_length (struct string *st, size_t new_length, char pad)
1207 {
1208   if (st->ss.length < new_length)
1209     ds_rpad (st, new_length, pad);
1210   else
1211     st->ss.length = new_length;
1212 }
1213
1214 /* Removes N bytes from ST starting at offset START. */
1215 void
1216 ds_remove (struct string *st, size_t start, size_t n)
1217 {
1218   if (n > 0 && start < st->ss.length)
1219     {
1220       if (st->ss.length - start <= n)
1221         {
1222           /* All bytes at or beyond START are deleted. */
1223           st->ss.length = start;
1224         }
1225       else
1226         {
1227           /* Some bytes remain and must be shifted into
1228              position. */
1229           memmove (st->ss.string + st->ss.length,
1230                    st->ss.string + st->ss.length + n,
1231                    st->ss.length - start - n);
1232           st->ss.length -= n;
1233         }
1234     }
1235   else
1236     {
1237       /* There are no bytes to delete or no bytes at or
1238          beyond START, hence deletion is a no-op. */
1239     }
1240 }
1241
1242 /* Returns true if ST is empty, false otherwise. */
1243 bool
1244 ds_is_empty (const struct string *st)
1245 {
1246   return ss_is_empty (st->ss);
1247 }
1248
1249 /* Returns the length of ST. */
1250 size_t
1251 ds_length (const struct string *st)
1252 {
1253   return ss_length (ds_ss (st));
1254 }
1255
1256 /* Returns the string data inside ST. */
1257 char *
1258 ds_data (const struct string *st)
1259 {
1260   return ss_data (ds_ss (st));
1261 }
1262
1263 /* Returns a pointer to the null terminator ST.
1264    This might not be an actual null byte unless ds_c_str() has
1265    been called since the last modification to ST. */
1266 char *
1267 ds_end (const struct string *st)
1268 {
1269   return ss_end (ds_ss (st));
1270 }
1271
1272 /* Returns the byte in position IDX in ST, as a value in the
1273    range of unsigned char.  Returns EOF if IDX is out of the
1274    range of indexes for ST. */
1275 int
1276 ds_at (const struct string *st, size_t idx)
1277 {
1278   return ss_at (ds_ss (st), idx);
1279 }
1280
1281 /* Returns the first byte in ST as a value in the range of
1282    unsigned char.  Returns EOF if ST is the empty string. */
1283 int
1284 ds_first (const struct string *st)
1285 {
1286   return ss_first (ds_ss (st));
1287 }
1288
1289 /* Returns the last byte in ST as a value in the range of
1290    unsigned char.  Returns EOF if ST is the empty string. */
1291 int
1292 ds_last (const struct string *st)
1293 {
1294   return ss_last (ds_ss (st));
1295 }
1296
1297 /* Returns true if ST ends with SUFFIX, false otherwise. */
1298 bool
1299 ds_ends_with (const struct string *st, struct substring suffix)
1300 {
1301   return ss_ends_with (st->ss, suffix);
1302 }
1303
1304 /* Returns the number of consecutive bytes at the beginning
1305    of ST that are in SKIP_SET. */
1306 size_t
1307 ds_span (const struct string *st, struct substring skip_set)
1308 {
1309   return ss_span (ds_ss (st), skip_set);
1310 }
1311
1312 /* Returns the number of consecutive bytes at the beginning
1313    of ST that are not in STOP_SET.  */
1314 size_t
1315 ds_cspan (const struct string *st, struct substring stop_set)
1316 {
1317   return ss_cspan (ds_ss (st), stop_set);
1318 }
1319
1320 /* Returns the position of the first occurrence of byte C in
1321    ST at or after position OFS, or SIZE_MAX if there is no such
1322    occurrence. */
1323 size_t
1324 ds_find_byte (const struct string *st, char c)
1325 {
1326   return ss_find_byte (ds_ss (st), c);
1327 }
1328
1329 /* Compares A and B and returns a strcmp()-type comparison
1330    result. */
1331 int
1332 ds_compare (const struct string *a, const struct string *b)
1333 {
1334   return ss_compare (ds_ss (a), ds_ss (b));
1335 }
1336
1337 /* Returns the position in ST that the byte at P occupies.
1338    P must point within ST or one past its end. */
1339 size_t
1340 ds_pointer_to_position (const struct string *st, const char *p)
1341 {
1342   return ss_pointer_to_position (ds_ss (st), p);
1343 }
1344
1345 /* Allocates and returns a null-terminated string that contains
1346    ST. */
1347 char *
1348 ds_xstrdup (const struct string *st)
1349 {
1350   return ss_xstrdup (ds_ss (st));
1351 }
1352
1353 /* Returns the allocation size of ST. */
1354 size_t
1355 ds_capacity (const struct string *st)
1356 {
1357   return st->capacity;
1358 }
1359
1360 /* Returns the value of ST as a null-terminated string. */
1361 char *
1362 ds_cstr (const struct string *st_)
1363 {
1364   struct string *st = CONST_CAST (struct string *, st_);
1365   if (st->ss.string == NULL)
1366     ds_extend (st, 1);
1367   st->ss.string[st->ss.length] = '\0';
1368   return st->ss.string;
1369 }
1370
1371 /* Returns the value of ST as a null-terminated string and then
1372    reinitialized ST as an empty string.  The caller must free the
1373    returned string with free(). */
1374 char *
1375 ds_steal_cstr (struct string *st)
1376 {
1377   char *s = ds_cstr (st);
1378   ds_init_empty (st);
1379   return s;
1380 }
1381
1382 /* Reads bytes from STREAM and appends them to ST, stopping
1383    after MAX_LENGTH bytes, after appending a newline, or
1384    after an I/O error or end of file was encountered, whichever
1385    comes first.  Returns true if at least one byte was added
1386    to ST, false if no bytes were read before an I/O error or
1387    end of file (or if MAX_LENGTH was 0).
1388
1389    This function treats LF and CR LF sequences as new-line,
1390    translating each of them to a single '\n' in ST. */
1391 bool
1392 ds_read_line (struct string *st, FILE *stream, size_t max_length)
1393 {
1394   size_t length;
1395
1396   for (length = 0; length < max_length; length++)
1397     {
1398       int c = getc (stream);
1399       switch (c)
1400         {
1401         case EOF:
1402           return length > 0;
1403
1404         case '\n':
1405           ds_put_byte (st, c);
1406           return true;
1407
1408         case '\r':
1409           c = getc (stream);
1410           if (c == '\n')
1411             {
1412               /* CR followed by LF is special: translate to \n. */
1413               ds_put_byte (st, '\n');
1414               return true;
1415             }
1416           else
1417             {
1418               /* CR followed by anything else is just CR. */
1419               ds_put_byte (st, '\r');
1420               if (c == EOF)
1421                 return true;
1422               ungetc (c, stream);
1423             }
1424           break;
1425
1426         default:
1427           ds_put_byte (st, c);
1428         }
1429     }
1430
1431   return length > 0;
1432 }
1433
1434 /* Removes a comment introduced by `#' from ST,
1435    ignoring occurrences inside quoted strings. */
1436 static void
1437 remove_comment (struct string *st)
1438 {
1439   char *cp;
1440   int quote = 0;
1441
1442   for (cp = ds_data (st); cp < ds_end (st); cp++)
1443     if (quote)
1444       {
1445         if (*cp == quote)
1446           quote = 0;
1447         else if (*cp == '\\')
1448           cp++;
1449       }
1450     else if (*cp == '\'' || *cp == '"')
1451       quote = *cp;
1452     else if (*cp == '#')
1453       {
1454         ds_truncate (st, cp - ds_cstr (st));
1455         break;
1456       }
1457 }
1458
1459 /* Reads a line from STREAM into ST, then preprocesses as follows:
1460
1461    - Splices lines terminated with `\'.
1462
1463    - Deletes comments introduced by `#' outside of single or double
1464      quotes.
1465
1466    - Deletes trailing white space.
1467
1468    Returns true if a line was successfully read, false on
1469    failure.  If LINE_NUMBER is non-null, then *LINE_NUMBER is
1470    incremented by the number of lines read. */
1471 bool
1472 ds_read_config_line (struct string *st, int *line_number, FILE *stream)
1473 {
1474   ds_clear (st);
1475   do
1476     {
1477       if (!ds_read_line (st, stream, SIZE_MAX))
1478         return false;
1479       (*line_number)++;
1480       ds_rtrim (st, ss_cstr (CC_SPACES));
1481     }
1482   while (ds_chomp_byte (st, '\\'));
1483
1484   remove_comment (st);
1485   return true;
1486 }
1487
1488 /* Attempts to read SIZE * CNT bytes from STREAM and append them
1489    to ST.
1490    Returns true if all the requested data was read, false otherwise. */
1491 bool
1492 ds_read_stream (struct string *st, size_t size, size_t cnt, FILE *stream)
1493 {
1494   if (size != 0)
1495     {
1496       size_t try_bytes = xtimes (cnt, size);
1497       if (size_in_bounds_p (xsum (ds_length (st), try_bytes)))
1498         {
1499           char *buffer = ds_put_uninit (st, try_bytes);
1500           size_t got_bytes = fread (buffer, 1, try_bytes, stream);
1501           ds_truncate (st, ds_length (st) - (try_bytes - got_bytes));
1502           return got_bytes == try_bytes;
1503         }
1504       else
1505         {
1506           errno = ENOMEM;
1507           return false;
1508         }
1509     }
1510   else
1511     return true;
1512 }
1513
1514 /* Concatenates S onto ST. */
1515 void
1516 ds_put_cstr (struct string *st, const char *s)
1517 {
1518   if (s != NULL)
1519     ds_put_substring (st, ss_cstr (s));
1520 }
1521
1522 /* Concatenates SS to ST. */
1523 void
1524 ds_put_substring (struct string *st, struct substring ss)
1525 {
1526   if (ss.length)
1527     memcpy (ds_put_uninit (st, ss_length (ss)), ss_data (ss), ss_length (ss));
1528 }
1529
1530 /* Returns ds_end(ST) and THEN increases the length by INCR. */
1531 char *
1532 ds_put_uninit (struct string *st, size_t incr)
1533 {
1534   char *end;
1535   ds_extend (st, ds_length (st) + incr);
1536   end = ds_end (st);
1537   st->ss.length += incr;
1538   return end;
1539 }
1540
1541 /* Moves the bytes in ST following offset OFS + OLD_LEN in ST to offset OFS +
1542    NEW_LEN and returns the byte at offset OFS.  The first min(OLD_LEN, NEW_LEN)
1543    bytes at the returned position are unchanged; if NEW_LEN > OLD_LEN then the
1544    following NEW_LEN - OLD_LEN bytes are initially indeterminate.
1545
1546    The intention is that the caller should write NEW_LEN bytes at the returned
1547    position, to effectively replace the OLD_LEN bytes previously at that
1548    position. */
1549 char *
1550 ds_splice_uninit (struct string *st,
1551                   size_t ofs, size_t old_len, size_t new_len)
1552 {
1553   if (new_len != old_len)
1554     {
1555       if (new_len > old_len)
1556         ds_extend (st, ds_length (st) + (new_len - old_len));
1557
1558       assert (ds_length (st) >= ofs + old_len);
1559
1560       memmove (ds_data (st) + (ofs + new_len),
1561                ds_data (st) + (ofs + old_len),
1562                ds_length (st) - (ofs + old_len));
1563       st->ss.length += new_len - old_len;
1564     }
1565   return ds_data (st) + ofs;
1566 }
1567
1568 /* Formats FORMAT as a printf string and appends the result to ST. */
1569 void
1570 ds_put_format (struct string *st, const char *format, ...)
1571 {
1572   va_list args;
1573
1574   va_start (args, format);
1575   ds_put_vformat (st, format, args);
1576   va_end (args);
1577 }
1578
1579 /* Formats FORMAT as a printf string as if in the C locale and appends the result to ST. */
1580 void
1581 ds_put_c_format (struct string *st, const char *format, ...)
1582 {
1583   va_list args;
1584
1585   va_start (args, format);
1586   ds_put_c_vformat (st, format, args);
1587   va_end (args);
1588 }
1589
1590 /* Formats FORMAT as a printf string and appends the result to ST. */
1591 void
1592 ds_put_vformat (struct string *st, const char *format, va_list args_)
1593 {
1594   int avail, needed;
1595   va_list args;
1596
1597   va_copy (args, args_);
1598   avail = st->ss.string != NULL ? st->capacity - st->ss.length + 1 : 0;
1599   needed = vsnprintf (st->ss.string + st->ss.length, avail, format, args);
1600   va_end (args);
1601
1602   if (needed >= avail)
1603     {
1604       va_copy (args, args_);
1605       vsnprintf (ds_put_uninit (st, needed), needed + 1, format, args);
1606       va_end (args);
1607     }
1608   else
1609     {
1610       /* Some old libc's returned -1 when the destination string
1611          was too short. */
1612       while (needed == -1)
1613         {
1614           ds_extend (st, (st->capacity + 1) * 2);
1615           avail = st->capacity - st->ss.length + 1;
1616
1617           va_copy (args, args_);
1618           needed = vsnprintf (ds_end (st), avail, format, args);
1619           va_end (args);
1620         }
1621       st->ss.length += needed;
1622     }
1623 }
1624
1625 /* Formats FORMAT as a printf string, as if in the C locale,
1626    and appends the result to ST. */
1627 void
1628 ds_put_c_vformat (struct string *st, const char *format, va_list args)
1629 {
1630   char buf[128];
1631   size_t len = sizeof buf;
1632   char *output = c_vasnprintf (buf, &len, format, args);
1633   if (output)
1634     {
1635       ds_put_cstr (st, output);
1636       if (output != buf)
1637         free (output);
1638     }
1639 }
1640
1641 /* Appends byte CH to ST. */
1642 void
1643 ds_put_byte (struct string *st, int ch)
1644 {
1645   ds_put_uninit (st, 1)[0] = ch;
1646 }
1647
1648 /* Appends CNT copies of byte CH to ST. */
1649 void
1650 ds_put_byte_multiple (struct string *st, int ch, size_t cnt)
1651 {
1652   memset (ds_put_uninit (st, cnt), ch, cnt);
1653 }
1654
1655 /* Appends Unicode code point UC to ST in UTF-8 encoding. */
1656 void
1657 ds_put_unichar (struct string *st, ucs4_t uc)
1658 {
1659   ds_extend (st, ds_length (st) + 6);
1660   st->ss.length += u8_uctomb (CHAR_CAST (uint8_t *, ds_end (st)), uc, 6);
1661 }
1662
1663 /* If relocation has been enabled, replace ST,
1664    with its relocated version */
1665 void
1666 ds_relocate (struct string *st)
1667 {
1668   const char *orig = ds_cstr (st);
1669   const char *rel = relocate (orig);
1670
1671   if (orig != rel)
1672     {
1673       ds_clear (st);
1674       ds_put_cstr (st, rel);
1675       /* The documentation for relocate says that casting away const
1676         and then freeing is appropriate ... */
1677       free (CONST_CAST (char *, rel));
1678     }
1679 }
1680
1681
1682 \f
1683
1684 /* Operations on uint8_t "strings" */
1685
1686 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
1687    DST is truncated to DST_SIZE bytes or padded on the right with
1688    copies of PAD as needed. */
1689 void
1690 u8_buf_copy_rpad (uint8_t *dst, size_t dst_size,
1691                   const uint8_t *src, size_t src_size,
1692                   char pad)
1693 {
1694   if (src_size >= dst_size)
1695     memmove (dst, src, dst_size);
1696   else
1697     {
1698       memmove (dst, src, src_size);
1699       memset (&dst[src_size], pad, dst_size - src_size);
1700     }
1701 }