sys-file-reader: get_text_token did not set delimiter - bug #58943
[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 /* Sets the SIZE bytes starting at BLOCK to C,
305    and returns the byte following BLOCK. */
306 void *
307 mempset (void *block, int c, size_t size)
308 {
309   memset (block, c, size);
310   return (char *) block + size;
311 }
312 \f
313 /* Substrings. */
314
315 /* Returns a substring whose contents are the CNT bytes
316    starting at the (0-based) position START in SS. */
317 struct substring
318 ss_substr (struct substring ss, size_t start, size_t cnt)
319 {
320   if (start < ss.length)
321     return ss_buffer (ss.string + start, MIN (cnt, ss.length - start));
322   else
323     return ss_buffer (ss.string + ss.length, 0);
324 }
325
326 /* Returns a substring whose contents are the first CNT
327    bytes in SS. */
328 struct substring
329 ss_head (struct substring ss, size_t cnt)
330 {
331   return ss_buffer (ss.string, MIN (cnt, ss.length));
332 }
333
334 /* Returns a substring whose contents are the last CNT bytes
335    in SS. */
336 struct substring
337 ss_tail (struct substring ss, size_t cnt)
338 {
339   if (cnt < ss.length)
340     return ss_buffer (ss.string + (ss.length - cnt), cnt);
341   else
342     return ss;
343 }
344
345 /* Makes a malloc()'d, null-terminated copy of the contents of OLD
346    and stores it in NEW. */
347 void
348 ss_alloc_substring (struct substring *new, struct substring old)
349 {
350   new->string = xmemdup0 (old.string, old.length);
351   new->length = old.length;
352 }
353
354 /* Allocates room for a CNT-byte string in NEW. */
355 void
356 ss_alloc_uninit (struct substring *new, size_t cnt)
357 {
358   new->string = xmalloc (cnt);
359   new->length = cnt;
360 }
361
362 void
363 ss_realloc (struct substring *ss, size_t size)
364 {
365   ss->string = xrealloc (ss->string, size);
366 }
367
368 /* Makes a pool_alloc_unaligned()'d, null-terminated copy of the contents of
369    OLD in POOL, and stores it in NEW. */
370 void
371 ss_alloc_substring_pool (struct substring *new, struct substring old,
372                          struct pool *pool)
373 {
374   new->string = pool_alloc_unaligned (pool, old.length + 1);
375   new->length = old.length;
376   memcpy (new->string, old.string, old.length);
377   new->string[old.length] = '\0';
378 }
379
380 /* Allocates room for a CNT-byte string in NEW in POOL. */
381 void
382 ss_alloc_uninit_pool (struct substring *new, size_t cnt, struct pool *pool)
383 {
384   new->string = pool_alloc_unaligned (pool, cnt);
385   new->length = cnt;
386 }
387
388 /* Frees the string that SS points to. */
389 void
390 ss_dealloc (struct substring *ss)
391 {
392   free (ss->string);
393 }
394
395 /* Truncates SS to at most CNT bytes in length. */
396 void
397 ss_truncate (struct substring *ss, size_t cnt)
398 {
399   if (ss->length > cnt)
400     ss->length = cnt;
401 }
402
403 /* Removes trailing bytes in TRIM_SET from SS.
404    Returns number of bytes removed. */
405 size_t
406 ss_rtrim (struct substring *ss, struct substring trim_set)
407 {
408   size_t cnt = 0;
409   while (cnt < ss->length
410          && ss_find_byte (trim_set,
411                           ss->string[ss->length - cnt - 1]) != SIZE_MAX)
412     cnt++;
413   ss->length -= cnt;
414   return cnt;
415 }
416
417 /* Removes leading bytes in TRIM_SET from SS.
418    Returns number of bytes removed. */
419 size_t
420 ss_ltrim (struct substring *ss, struct substring trim_set)
421 {
422   size_t cnt = ss_span (*ss, trim_set);
423   ss_advance (ss, cnt);
424   return cnt;
425 }
426
427 /* Trims leading and trailing bytes in TRIM_SET from SS. */
428 void
429 ss_trim (struct substring *ss, struct substring trim_set)
430 {
431   ss_ltrim (ss, trim_set);
432   ss_rtrim (ss, trim_set);
433 }
434
435 /* If the last byte in SS is C, removes it and returns true.
436    Otherwise, returns false without changing the string. */
437 bool
438 ss_chomp_byte (struct substring *ss, char c)
439 {
440   if (ss_last (*ss) == c)
441     {
442       ss->length--;
443       return true;
444     }
445   else
446     return false;
447 }
448
449 /* If SS ends with SUFFIX, removes it and returns true.
450    Otherwise, returns false without changing the string. */
451 bool
452 ss_chomp (struct substring *ss, struct substring suffix)
453 {
454   if (ss_ends_with (*ss, suffix))
455     {
456       ss->length -= suffix.length;
457       return true;
458     }
459   else
460     return false;
461 }
462
463 /* Divides SS into tokens separated by any of the DELIMITERS.
464    Each call replaces TOKEN by the next token in SS, or by an
465    empty string if no tokens remain.  Returns true if a token was
466    obtained, false otherwise.
467
468    Before the first call, initialize *SAVE_IDX to 0.  Do not
469    modify *SAVE_IDX between calls.
470
471    SS divides into exactly one more tokens than it contains
472    delimiters.  That is, a delimiter at the start or end of SS or
473    a pair of adjacent delimiters yields an empty token, and the
474    empty string contains a single token. */
475 bool
476 ss_separate (struct substring ss, struct substring delimiters,
477              size_t *save_idx, struct substring *token)
478 {
479   if (*save_idx <= ss_length (ss))
480     {
481       struct substring tmp = ss_substr (ss, *save_idx, SIZE_MAX);
482       size_t length = ss_cspan (tmp, delimiters);
483       *token = ss_head (tmp, length);
484       *save_idx += length + 1;
485       return true;
486     }
487   else
488     {
489       *token = ss_empty ();
490       return false;
491     }
492 }
493
494 /* Divides SS into tokens separated by any of the DELIMITERS,
495    merging adjacent delimiters so that the empty string is never
496    produced as a token.  Each call replaces TOKEN by the next
497    token in SS, or by an empty string if no tokens remain, and
498    then skips past the first delimiter following the token.
499    Returns true if a token was obtained, false otherwise.
500
501    Before the first call, initialize *SAVE_IDX to 0.  Do not
502    modify *SAVE_IDX between calls. */
503 bool
504 ss_tokenize (struct substring ss, struct substring delimiters,
505              size_t *save_idx, struct substring *token)
506 {
507   bool found_token;
508
509   ss_advance (&ss, *save_idx);
510   *save_idx += ss_ltrim (&ss, delimiters);
511   ss_get_bytes (&ss, ss_cspan (ss, delimiters), token);
512
513   found_token = ss_length (*token) > 0;
514   *save_idx += ss_length (*token) + (found_token?1:0);
515   return found_token;
516 }
517
518 /* Removes the first CNT bytes from SS. */
519 void
520 ss_advance (struct substring *ss, size_t cnt)
521 {
522   if (cnt > ss->length)
523     cnt = ss->length;
524   ss->string += cnt;
525   ss->length -= cnt;
526 }
527
528 /* If the first byte in SS is C, removes it and returns true.
529    Otherwise, returns false without changing the string. */
530 bool
531 ss_match_byte (struct substring *ss, char c)
532 {
533   if (ss_first (*ss) == c)
534     {
535       ss->string++;
536       ss->length--;
537       return true;
538     }
539   else
540     return false;
541 }
542
543 /* If the first byte in SS is in MATCH, removes it and
544    returns the byte that was removed.
545    Otherwise, returns EOF without changing the string. */
546 int
547 ss_match_byte_in (struct substring *ss, struct substring match)
548 {
549   int c = EOF;
550   if (ss->length > 0
551       && memchr (match.string, ss->string[0], match.length) != NULL)
552     {
553       c = ss->string[0];
554       ss->string++;
555       ss->length--;
556     }
557   return c;
558 }
559
560 /* If SS begins with TARGET, removes it and returns true.
561    Otherwise, returns false without changing SS. */
562 bool
563 ss_match_string (struct substring *ss, const struct substring target)
564 {
565   size_t length = ss_length (target);
566   if (ss_equals (ss_head (*ss, length), target))
567     {
568       ss_advance (ss, length);
569       return true;
570     }
571   else
572     return false;
573 }
574
575 /* Removes the first byte from SS and returns it.
576    If SS is empty, returns EOF without modifying SS. */
577 int
578 ss_get_byte (struct substring *ss)
579 {
580   int c = ss_first (*ss);
581   if (c != EOF)
582     {
583       ss->string++;
584       ss->length--;
585     }
586   return c;
587 }
588
589 /* Stores the prefix of SS up to the first DELIMITER in OUT (if
590    any).  Trims those same bytes from SS.  DELIMITER is
591    removed from SS but not made part of OUT.  Returns true if
592    DELIMITER was found (and removed), false otherwise. */
593 bool
594 ss_get_until (struct substring *ss, char delimiter, struct substring *out)
595 {
596   ss_get_bytes (ss, ss_cspan (*ss, ss_buffer (&delimiter, 1)), out);
597   return ss_match_byte (ss, delimiter);
598 }
599
600 /* Stores the first CNT bytes in SS in OUT (or fewer, if SS
601    is shorter than CNT bytes).  Trims the same bytes
602    from the beginning of SS.  Returns CNT. */
603 size_t
604 ss_get_bytes (struct substring *ss, size_t cnt, struct substring *out)
605 {
606   *out = ss_head (*ss, cnt);
607   ss_advance (ss, cnt);
608   return cnt;
609 }
610
611 /* Parses and removes an optionally signed decimal integer from
612    the beginning of SS.  Returns 0 if an error occurred,
613    otherwise the number of bytes removed from SS.  Stores
614    the integer's value into *VALUE. */
615 size_t
616 ss_get_long (struct substring *ss, long *value)
617 {
618   char tmp[64];
619   size_t length;
620
621   length = ss_span (*ss, ss_cstr ("+-"));
622   length += ss_span (ss_substr (*ss, length, SIZE_MAX), ss_cstr (CC_DIGITS));
623   if (length > 0 && length < sizeof tmp)
624     {
625       char *tail;
626
627       memcpy (tmp, ss_data (*ss), length);
628       tmp[length] = '\0';
629
630       *value = strtol (tmp, &tail, 10);
631       if (tail - tmp == length)
632         {
633           ss_advance (ss, length);
634           return length;
635         }
636     }
637   *value = 0;
638   return 0;
639 }
640
641 /* Returns true if SS is empty (has length 0 bytes),
642    false otherwise. */
643 bool
644 ss_is_empty (struct substring ss)
645 {
646   return ss.length == 0;
647 }
648
649 /* Returns the number of bytes in SS. */
650 size_t
651 ss_length (struct substring ss)
652 {
653   return ss.length;
654 }
655
656 /* Returns a pointer to the bytes in SS. */
657 char *
658 ss_data (struct substring ss)
659 {
660   return ss.string;
661 }
662
663 /* Returns a pointer just past the last byte in SS. */
664 char *
665 ss_end (struct substring ss)
666 {
667   return ss.string + ss.length;
668 }
669
670 /* Returns the byte in position IDX in SS, as a value in the
671    range of unsigned char.  Returns EOF if IDX is out of the
672    range of indexes for SS. */
673 int
674 ss_at (struct substring ss, size_t idx)
675 {
676   return idx < ss.length ? (unsigned char) ss.string[idx] : EOF;
677 }
678
679 /* Returns the first byte in SS as a value in the range of
680    unsigned char.  Returns EOF if SS is the empty string. */
681 int
682 ss_first (struct substring ss)
683 {
684   return ss_at (ss, 0);
685 }
686
687 /* Returns the last byte in SS as a value in the range of
688    unsigned char.  Returns EOF if SS is the empty string. */
689 int
690 ss_last (struct substring ss)
691 {
692   return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF;
693 }
694
695 /* Returns true if SS starts with PREFIX, false otherwise. */
696 bool
697 ss_starts_with (struct substring ss, struct substring prefix)
698 {
699   return (ss.length >= prefix.length
700           && !memcmp (ss.string, prefix.string, prefix.length));
701 }
702
703 /* Returns true if SS ends with SUFFIX, false otherwise. */
704 bool
705 ss_ends_with (struct substring ss, struct substring suffix)
706 {
707   return (ss.length >= suffix.length
708           && !memcmp (&ss.string[ss.length - suffix.length], suffix.string,
709                       suffix.length));
710 }
711
712 /* Returns the number of contiguous bytes at the beginning
713    of SS that are in SKIP_SET. */
714 size_t
715 ss_span (struct substring ss, struct substring skip_set)
716 {
717   size_t i;
718   for (i = 0; i < ss.length; i++)
719     if (ss_find_byte (skip_set, ss.string[i]) == SIZE_MAX)
720       break;
721   return i;
722 }
723
724 /* Returns the number of contiguous bytes at the beginning
725    of SS that are not in SKIP_SET. */
726 size_t
727 ss_cspan (struct substring ss, struct substring stop_set)
728 {
729   size_t i;
730   for (i = 0; i < ss.length; i++)
731     if (ss_find_byte (stop_set, ss.string[i]) != SIZE_MAX)
732       break;
733   return i;
734 }
735
736 /* Returns the offset in SS of the first instance of C,
737    or SIZE_MAX if C does not occur in SS. */
738 size_t
739 ss_find_byte (struct substring ss, char c)
740 {
741   const char *p = memchr (ss.string, (int) c, ss.length);
742   return p != NULL ? p - ss.string : SIZE_MAX;
743 }
744
745 /* Returns the offset in HAYSTACK of the first instance of NEEDLE,
746    or SIZE_MAX if NEEDLE does not occur in HAYSTACK. */
747 size_t
748 ss_find_substring (struct substring haystack, struct substring needle)
749 {
750   const char *p = memmem (haystack.string, haystack.length,
751                           needle.string, needle.length);
752   return p != NULL ? p - haystack.string : SIZE_MAX;
753 }
754
755 /* Compares A and B and returns a strcmp()-type comparison
756    result. */
757 int
758 ss_compare (struct substring a, struct substring b)
759 {
760   int retval = memcmp (a.string, b.string, MIN (a.length, b.length));
761   if (retval == 0)
762     retval = a.length < b.length ? -1 : a.length > b.length;
763   return retval;
764 }
765
766 /* Compares A and B case-insensitively and returns a
767    strcmp()-type comparison result. */
768 int
769 ss_compare_case (struct substring a, struct substring b)
770 {
771   int retval = memcasecmp (a.string, b.string, MIN (a.length, b.length));
772   if (retval == 0)
773     retval = a.length < b.length ? -1 : a.length > b.length;
774   return retval;
775 }
776
777 /* Compares A and B and returns true if their contents are
778    identical, false otherwise. */
779 int
780 ss_equals (struct substring a, struct substring b)
781 {
782   return a.length == b.length && !memcmp (a.string, b.string, a.length);
783 }
784
785 /* Compares A and B and returns true if their contents are
786    identical except possibly for case differences, false
787    otherwise. */
788 int
789 ss_equals_case (struct substring a, struct substring b)
790 {
791   return a.length == b.length && !memcasecmp (a.string, b.string, a.length);
792 }
793
794 /* Returns the position in SS that the byte at P occupies.
795    P must point within SS or one past its end. */
796 size_t
797 ss_pointer_to_position (struct substring ss, const char *p)
798 {
799   size_t pos = p - ss.string;
800   assert (pos <= ss.length);
801   return pos;
802 }
803
804 /* Allocates and returns a null-terminated string that contains
805    SS. */
806 char *
807 ss_xstrdup (struct substring ss)
808 {
809   char *s = xmalloc (ss.length + 1);
810   memcpy (s, ss.string, ss.length);
811   s[ss.length] = '\0';
812   return s;
813 }
814 /* UTF-8. */
815
816 /* Returns the character represented by the UTF-8 sequence at the start of S.
817    The return value is either a Unicode code point in the range 0 to 0x10ffff,
818    or UINT32_MAX if S is empty. */
819 ucs4_t
820 ss_first_mb (struct substring s)
821 {
822   return ss_at_mb (s, 0);
823 }
824
825 /* Returns the number of bytes in the UTF-8 character at the beginning of S.
826
827    The return value is 0 if S is empty, otherwise between 1 and 4. */
828 int
829 ss_first_mblen (struct substring s)
830 {
831   return ss_at_mblen (s, 0);
832 }
833
834 /* Advances S past the UTF-8 character at its beginning.  Returns the Unicode
835    code point that was skipped (in the range 0 to 0x10ffff), or UINT32_MAX if S
836    was not modified because it was initially empty. */
837 ucs4_t
838 ss_get_mb (struct substring *s)
839 {
840   if (s->length > 0)
841     {
842       ucs4_t uc;
843       int n;
844
845       n = u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s->string), s->length);
846       s->string += n;
847       s->length -= n;
848       return uc;
849     }
850   else
851     return UINT32_MAX;
852 }
853
854 /* Returns the character represented by the UTF-8 sequence starting OFS bytes
855    into S.  The return value is either a Unicode code point in the range 0 to
856    0x10ffff, or UINT32_MAX if OFS is past the last byte in S.
857
858    (Returns 0xfffd if OFS points into the middle, not the beginning, of a UTF-8
859    sequence.)  */
860 ucs4_t
861 ss_at_mb (struct substring s, size_t ofs)
862 {
863   if (s.length > ofs)
864     {
865       ucs4_t uc;
866       u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s.string + ofs),
867                  s.length - ofs);
868       return uc;
869     }
870   else
871     return UINT32_MAX;
872 }
873
874 /* Returns the number of bytes represented by the UTF-8 sequence starting OFS
875    bytes into S.  The return value is 0 if OFS is past the last byte in S,
876    otherwise between 1 and 4. */
877 int
878 ss_at_mblen (struct substring s, size_t ofs)
879 {
880   if (s.length > ofs)
881     {
882       ucs4_t uc;
883       return u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s.string + ofs),
884                         s.length - ofs);
885     }
886   else
887     return 0;
888 }
889 \f
890 /* Initializes ST as an empty string. */
891 void
892 ds_init_empty (struct string *st)
893 {
894   st->ss = ss_empty ();
895   st->capacity = 0;
896 }
897
898 /* Initializes ST with initial contents S. */
899 void
900 ds_init_string (struct string *st, const struct string *s)
901 {
902   ds_init_substring (st, ds_ss (s));
903 }
904
905 /* Initializes ST with initial contents SS. */
906 void
907 ds_init_substring (struct string *st, struct substring ss)
908 {
909   st->capacity = MAX (8, ss.length * 2);
910   st->ss.string = xmalloc (st->capacity + 1);
911   memcpy (st->ss.string, ss.string, ss.length);
912   st->ss.length = ss.length;
913 }
914
915 /* Initializes ST with initial contents S. */
916 void
917 ds_init_cstr (struct string *st, const char *s)
918 {
919   ds_init_substring (st, ss_cstr (s));
920 }
921
922 /* Frees ST. */
923 void
924 ds_destroy (struct string *st)
925 {
926   if (st != NULL)
927     {
928       ss_dealloc (&st->ss);
929       st->ss.string = NULL;
930       st->ss.length = 0;
931       st->capacity = 0;
932     }
933 }
934
935 /* Swaps the contents of strings A and B. */
936 void
937 ds_swap (struct string *a, struct string *b)
938 {
939   struct string tmp = *a;
940   *a = *b;
941   *b = tmp;
942 }
943
944 /* Helper function for ds_register_pool. */
945 static void
946 free_string (void *st_)
947 {
948   struct string *st = st_;
949   ds_destroy (st);
950 }
951
952 /* Arranges for ST to be destroyed automatically as part of
953    POOL. */
954 void
955 ds_register_pool (struct string *st, struct pool *pool)
956 {
957   pool_register (pool, free_string, st);
958 }
959
960 /* Cancels the arrangement for ST to be destroyed automatically
961    as part of POOL. */
962 void
963 ds_unregister_pool (struct string *st, struct pool *pool)
964 {
965   pool_unregister (pool, st);
966 }
967
968 /* Copies SRC into DST.
969    DST and SRC may be the same string. */
970 void
971 ds_assign_string (struct string *dst, const struct string *src)
972 {
973   ds_assign_substring (dst, ds_ss (src));
974 }
975
976 /* Replaces DST by SS.
977    SS may be a substring of DST. */
978 void
979 ds_assign_substring (struct string *dst, struct substring ss)
980 {
981   dst->ss.length = ss.length;
982   ds_extend (dst, ss.length);
983   memmove (dst->ss.string, ss.string, ss.length);
984 }
985
986 /* Replaces DST by null-terminated string SRC.  SRC may overlap
987    with DST. */
988 void
989 ds_assign_cstr (struct string *dst, const char *src)
990 {
991   ds_assign_substring (dst, ss_cstr (src));
992 }
993
994 /* Truncates ST to zero length. */
995 void
996 ds_clear (struct string *st)
997 {
998   st->ss.length = 0;
999 }
1000
1001 /* Returns a substring that contains ST. */
1002 struct substring
1003 ds_ss (const struct string *st)
1004 {
1005   return st->ss;
1006 }
1007
1008 /* Returns a substring that contains CNT bytes from ST
1009    starting at position START.
1010
1011    If START is greater than or equal to the length of ST, then
1012    the substring will be the empty string.  If START + CNT
1013    exceeds the length of ST, then the substring will only be
1014    ds_length(ST) - START bytes long. */
1015 struct substring
1016 ds_substr (const struct string *st, size_t start, size_t cnt)
1017 {
1018   return ss_substr (ds_ss (st), start, cnt);
1019 }
1020
1021 /* Returns a substring that contains the first CNT bytes in
1022    ST.  If CNT exceeds the length of ST, then the substring will
1023    contain all of ST. */
1024 struct substring
1025 ds_head (const struct string *st, size_t cnt)
1026 {
1027   return ss_head (ds_ss (st), cnt);
1028 }
1029
1030 /* Returns a substring that contains the last CNT bytes in
1031    ST.  If CNT exceeds the length of ST, then the substring will
1032    contain all of ST. */
1033 struct substring
1034 ds_tail (const struct string *st, size_t cnt)
1035 {
1036   return ss_tail (ds_ss (st), cnt);
1037 }
1038
1039 /* Ensures that ST can hold at least MIN_CAPACITY bytes plus a null
1040    terminator. */
1041 void
1042 ds_extend (struct string *st, size_t min_capacity)
1043 {
1044   if (min_capacity > st->capacity)
1045     {
1046       st->capacity *= 2;
1047       if (st->capacity < min_capacity)
1048         st->capacity = 2 * min_capacity;
1049
1050       st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
1051     }
1052 }
1053
1054 /* Shrink ST to the minimum capacity need to contain its content. */
1055 void
1056 ds_shrink (struct string *st)
1057 {
1058   if (st->capacity != st->ss.length)
1059     {
1060       st->capacity = st->ss.length;
1061       st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
1062     }
1063 }
1064
1065 /* Truncates ST to at most LENGTH bytes long. */
1066 void
1067 ds_truncate (struct string *st, size_t length)
1068 {
1069   ss_truncate (&st->ss, length);
1070 }
1071
1072 /* Removes trailing bytes in TRIM_SET from ST.
1073    Returns number of bytes removed. */
1074 size_t
1075 ds_rtrim (struct string *st, struct substring trim_set)
1076 {
1077   return ss_rtrim (&st->ss, trim_set);
1078 }
1079
1080 /* Removes leading bytes in TRIM_SET from ST.
1081    Returns number of bytes removed. */
1082 size_t
1083 ds_ltrim (struct string *st, struct substring trim_set)
1084 {
1085   size_t cnt = ds_span (st, trim_set);
1086   if (cnt > 0)
1087     ds_assign_substring (st, ds_substr (st, cnt, SIZE_MAX));
1088   return cnt;
1089 }
1090
1091 /* Trims leading and trailing bytes in TRIM_SET from ST.
1092    Returns number of bytes removed. */
1093 size_t
1094 ds_trim (struct string *st, struct substring trim_set)
1095 {
1096   size_t cnt = ds_rtrim (st, trim_set);
1097   return cnt + ds_ltrim (st, trim_set);
1098 }
1099
1100 /* If the last byte in ST is C, removes it and returns true.
1101    Otherwise, returns false without modifying ST. */
1102 bool
1103 ds_chomp_byte (struct string *st, char c)
1104 {
1105   return ss_chomp_byte (&st->ss, c);
1106 }
1107
1108 /* If ST ends with SUFFIX, removes it and returns true.
1109    Otherwise, returns false without modifying ST. */
1110 bool
1111 ds_chomp (struct string *st, struct substring suffix)
1112 {
1113   return ss_chomp (&st->ss, suffix);
1114 }
1115
1116 /* Divides ST into tokens separated by any of the DELIMITERS.
1117    Each call replaces TOKEN by the next token in ST, or by an
1118    empty string if no tokens remain.  Returns true if a token was
1119    obtained, false otherwise.
1120
1121    Before the first call, initialize *SAVE_IDX to 0.  Do not
1122    modify *SAVE_IDX between calls.
1123
1124    ST divides into exactly one more tokens than it contains
1125    delimiters.  That is, a delimiter at the start or end of ST or
1126    a pair of adjacent delimiters yields an empty token, and the
1127    empty string contains a single token. */
1128 bool
1129 ds_separate (const struct string *st, struct substring delimiters,
1130              size_t *save_idx, struct substring *token)
1131 {
1132   return ss_separate (ds_ss (st), delimiters, save_idx, token);
1133 }
1134
1135 /* Divides ST into tokens separated by any of the DELIMITERS,
1136    merging adjacent delimiters so that the empty string is never
1137    produced as a token.  Each call replaces TOKEN by the next
1138    token in ST, or by an empty string if no tokens remain.
1139    Returns true if a token was obtained, false otherwise.
1140
1141    Before the first call, initialize *SAVE_IDX to 0.  Do not
1142    modify *SAVE_IDX between calls. */
1143 bool
1144 ds_tokenize (const struct string *st, struct substring delimiters,
1145              size_t *save_idx, struct substring *token)
1146 {
1147   return ss_tokenize (ds_ss (st), delimiters, save_idx, token);
1148 }
1149
1150 /* Pad ST on the right with copies of PAD until ST is at least
1151    LENGTH bytes in size.  If ST is initially LENGTH
1152    bytes or longer, this is a no-op. */
1153 void
1154 ds_rpad (struct string *st, size_t length, char pad)
1155 {
1156   if (length > st->ss.length)
1157     ds_put_byte_multiple (st, pad, length - st->ss.length);
1158 }
1159
1160 /* Sets the length of ST to exactly NEW_LENGTH,
1161    either by truncating bytes from the end,
1162    or by padding on the right with PAD. */
1163 void
1164 ds_set_length (struct string *st, size_t new_length, char pad)
1165 {
1166   if (st->ss.length < new_length)
1167     ds_rpad (st, new_length, pad);
1168   else
1169     st->ss.length = new_length;
1170 }
1171
1172 /* Removes N bytes from ST starting at offset START. */
1173 void
1174 ds_remove (struct string *st, size_t start, size_t n)
1175 {
1176   if (n > 0 && start < st->ss.length)
1177     {
1178       if (st->ss.length - start <= n)
1179         {
1180           /* All bytes at or beyond START are deleted. */
1181           st->ss.length = start;
1182         }
1183       else
1184         {
1185           /* Some bytes remain and must be shifted into
1186              position. */
1187           memmove (st->ss.string + st->ss.length,
1188                    st->ss.string + st->ss.length + n,
1189                    st->ss.length - start - n);
1190           st->ss.length -= n;
1191         }
1192     }
1193   else
1194     {
1195       /* There are no bytes to delete or no bytes at or
1196          beyond START, hence deletion is a no-op. */
1197     }
1198 }
1199
1200 /* Returns true if ST is empty, false otherwise. */
1201 bool
1202 ds_is_empty (const struct string *st)
1203 {
1204   return ss_is_empty (st->ss);
1205 }
1206
1207 /* Returns the length of ST. */
1208 size_t
1209 ds_length (const struct string *st)
1210 {
1211   return ss_length (ds_ss (st));
1212 }
1213
1214 /* Returns the string data inside ST. */
1215 char *
1216 ds_data (const struct string *st)
1217 {
1218   return ss_data (ds_ss (st));
1219 }
1220
1221 /* Returns a pointer to the null terminator ST.
1222    This might not be an actual null byte unless ds_c_str() has
1223    been called since the last modification to ST. */
1224 char *
1225 ds_end (const struct string *st)
1226 {
1227   return ss_end (ds_ss (st));
1228 }
1229
1230 /* Returns the byte in position IDX in ST, as a value in the
1231    range of unsigned char.  Returns EOF if IDX is out of the
1232    range of indexes for ST. */
1233 int
1234 ds_at (const struct string *st, size_t idx)
1235 {
1236   return ss_at (ds_ss (st), idx);
1237 }
1238
1239 /* Returns the first byte in ST as a value in the range of
1240    unsigned char.  Returns EOF if ST is the empty string. */
1241 int
1242 ds_first (const struct string *st)
1243 {
1244   return ss_first (ds_ss (st));
1245 }
1246
1247 /* Returns the last byte in ST as a value in the range of
1248    unsigned char.  Returns EOF if ST is the empty string. */
1249 int
1250 ds_last (const struct string *st)
1251 {
1252   return ss_last (ds_ss (st));
1253 }
1254
1255 /* Returns true if ST ends with SUFFIX, false otherwise. */
1256 bool
1257 ds_ends_with (const struct string *st, struct substring suffix)
1258 {
1259   return ss_ends_with (st->ss, suffix);
1260 }
1261
1262 /* Returns the number of consecutive bytes at the beginning
1263    of ST that are in SKIP_SET. */
1264 size_t
1265 ds_span (const struct string *st, struct substring skip_set)
1266 {
1267   return ss_span (ds_ss (st), skip_set);
1268 }
1269
1270 /* Returns the number of consecutive bytes at the beginning
1271    of ST that are not in STOP_SET.  */
1272 size_t
1273 ds_cspan (const struct string *st, struct substring stop_set)
1274 {
1275   return ss_cspan (ds_ss (st), stop_set);
1276 }
1277
1278 /* Returns the position of the first occurrence of byte C in
1279    ST at or after position OFS, or SIZE_MAX if there is no such
1280    occurrence. */
1281 size_t
1282 ds_find_byte (const struct string *st, char c)
1283 {
1284   return ss_find_byte (ds_ss (st), c);
1285 }
1286
1287 /* Compares A and B and returns a strcmp()-type comparison
1288    result. */
1289 int
1290 ds_compare (const struct string *a, const struct string *b)
1291 {
1292   return ss_compare (ds_ss (a), ds_ss (b));
1293 }
1294
1295 /* Returns the position in ST that the byte at P occupies.
1296    P must point within ST or one past its end. */
1297 size_t
1298 ds_pointer_to_position (const struct string *st, const char *p)
1299 {
1300   return ss_pointer_to_position (ds_ss (st), p);
1301 }
1302
1303 /* Allocates and returns a null-terminated string that contains
1304    ST. */
1305 char *
1306 ds_xstrdup (const struct string *st)
1307 {
1308   return ss_xstrdup (ds_ss (st));
1309 }
1310
1311 /* Returns the allocation size of ST. */
1312 size_t
1313 ds_capacity (const struct string *st)
1314 {
1315   return st->capacity;
1316 }
1317
1318 /* Returns the value of ST as a null-terminated string. */
1319 char *
1320 ds_cstr (const struct string *st_)
1321 {
1322   struct string *st = CONST_CAST (struct string *, st_);
1323   if (st->ss.string == NULL)
1324     ds_extend (st, 1);
1325   st->ss.string[st->ss.length] = '\0';
1326   return st->ss.string;
1327 }
1328
1329 /* Returns the value of ST as a null-terminated string and then
1330    reinitialized ST as an empty string.  The caller must free the
1331    returned string with free(). */
1332 char *
1333 ds_steal_cstr (struct string *st)
1334 {
1335   char *s = ds_cstr (st);
1336   ds_init_empty (st);
1337   return s;
1338 }
1339
1340 /* Reads bytes from STREAM and appends them to ST, stopping
1341    after MAX_LENGTH bytes, after appending a newline, or
1342    after an I/O error or end of file was encountered, whichever
1343    comes first.  Returns true if at least one byte was added
1344    to ST, false if no bytes were read before an I/O error or
1345    end of file (or if MAX_LENGTH was 0).
1346
1347    This function treats LF and CR LF sequences as new-line,
1348    translating each of them to a single '\n' in ST. */
1349 bool
1350 ds_read_line (struct string *st, FILE *stream, size_t max_length)
1351 {
1352   size_t length;
1353
1354   for (length = 0; length < max_length; length++)
1355     {
1356       int c = getc (stream);
1357       switch (c)
1358         {
1359         case EOF:
1360           return length > 0;
1361
1362         case '\n':
1363           ds_put_byte (st, c);
1364           return true;
1365
1366         case '\r':
1367           c = getc (stream);
1368           if (c == '\n')
1369             {
1370               /* CR followed by LF is special: translate to \n. */
1371               ds_put_byte (st, '\n');
1372               return true;
1373             }
1374           else
1375             {
1376               /* CR followed by anything else is just CR. */
1377               ds_put_byte (st, '\r');
1378               if (c == EOF)
1379                 return true;
1380               ungetc (c, stream);
1381             }
1382           break;
1383
1384         default:
1385           ds_put_byte (st, c);
1386         }
1387     }
1388
1389   return length > 0;
1390 }
1391
1392 /* Removes a comment introduced by `#' from ST,
1393    ignoring occurrences inside quoted strings. */
1394 static void
1395 remove_comment (struct string *st)
1396 {
1397   char *cp;
1398   int quote = 0;
1399
1400   for (cp = ds_data (st); cp < ds_end (st); cp++)
1401     if (quote)
1402       {
1403         if (*cp == quote)
1404           quote = 0;
1405         else if (*cp == '\\')
1406           cp++;
1407       }
1408     else if (*cp == '\'' || *cp == '"')
1409       quote = *cp;
1410     else if (*cp == '#')
1411       {
1412         ds_truncate (st, cp - ds_cstr (st));
1413         break;
1414       }
1415 }
1416
1417 /* Reads a line from STREAM into ST, then preprocesses as follows:
1418
1419    - Splices lines terminated with `\'.
1420
1421    - Deletes comments introduced by `#' outside of single or double
1422      quotes.
1423
1424    - Deletes trailing white space.
1425
1426    Returns true if a line was successfully read, false on
1427    failure.  If LINE_NUMBER is non-null, then *LINE_NUMBER is
1428    incremented by the number of lines read. */
1429 bool
1430 ds_read_config_line (struct string *st, int *line_number, FILE *stream)
1431 {
1432   ds_clear (st);
1433   do
1434     {
1435       if (!ds_read_line (st, stream, SIZE_MAX))
1436         return false;
1437       (*line_number)++;
1438       ds_rtrim (st, ss_cstr (CC_SPACES));
1439     }
1440   while (ds_chomp_byte (st, '\\'));
1441
1442   remove_comment (st);
1443   return true;
1444 }
1445
1446 /* Attempts to read SIZE * CNT bytes from STREAM and append them
1447    to ST.
1448    Returns true if all the requested data was read, false otherwise. */
1449 bool
1450 ds_read_stream (struct string *st, size_t size, size_t cnt, FILE *stream)
1451 {
1452   if (size != 0)
1453     {
1454       size_t try_bytes = xtimes (cnt, size);
1455       if (size_in_bounds_p (xsum (ds_length (st), try_bytes)))
1456         {
1457           char *buffer = ds_put_uninit (st, try_bytes);
1458           size_t got_bytes = fread (buffer, 1, try_bytes, stream);
1459           ds_truncate (st, ds_length (st) - (try_bytes - got_bytes));
1460           return got_bytes == try_bytes;
1461         }
1462       else
1463         {
1464           errno = ENOMEM;
1465           return false;
1466         }
1467     }
1468   else
1469     return true;
1470 }
1471
1472 /* Concatenates S onto ST. */
1473 void
1474 ds_put_cstr (struct string *st, const char *s)
1475 {
1476   if (s != NULL)
1477     ds_put_substring (st, ss_cstr (s));
1478 }
1479
1480 /* Concatenates SS to ST. */
1481 void
1482 ds_put_substring (struct string *st, struct substring ss)
1483 {
1484   memcpy (ds_put_uninit (st, ss_length (ss)), ss_data (ss), ss_length (ss));
1485 }
1486
1487 /* Returns ds_end(ST) and THEN increases the length by INCR. */
1488 char *
1489 ds_put_uninit (struct string *st, size_t incr)
1490 {
1491   char *end;
1492   ds_extend (st, ds_length (st) + incr);
1493   end = ds_end (st);
1494   st->ss.length += incr;
1495   return end;
1496 }
1497
1498 /* Moves the bytes in ST following offset OFS + OLD_LEN in ST to offset OFS +
1499    NEW_LEN and returns the byte at offset OFS.  The first min(OLD_LEN, NEW_LEN)
1500    bytes at the returned position are unchanged; if NEW_LEN > OLD_LEN then the
1501    following NEW_LEN - OLD_LEN bytes are initially indeterminate.
1502
1503    The intention is that the caller should write NEW_LEN bytes at the returned
1504    position, to effectively replace the OLD_LEN bytes previously at that
1505    position. */
1506 char *
1507 ds_splice_uninit (struct string *st,
1508                   size_t ofs, size_t old_len, size_t new_len)
1509 {
1510   if (new_len != old_len)
1511     {
1512       if (new_len > old_len)
1513         ds_extend (st, ds_length (st) + (new_len - old_len));
1514
1515       assert (ds_length (st) >= ofs + old_len);
1516
1517       memmove (ds_data (st) + (ofs + new_len),
1518                ds_data (st) + (ofs + old_len),
1519                ds_length (st) - (ofs + old_len));
1520       st->ss.length += new_len - old_len;
1521     }
1522   return ds_data (st) + ofs;
1523 }
1524
1525 /* Formats FORMAT as a printf string and appends the result to ST. */
1526 void
1527 ds_put_format (struct string *st, const char *format, ...)
1528 {
1529   va_list args;
1530
1531   va_start (args, format);
1532   ds_put_vformat (st, format, args);
1533   va_end (args);
1534 }
1535
1536 /* Formats FORMAT as a printf string as if in the C locale and appends the result to ST. */
1537 void
1538 ds_put_c_format (struct string *st, const char *format, ...)
1539 {
1540   va_list args;
1541
1542   va_start (args, format);
1543   ds_put_c_vformat (st, format, args);
1544   va_end (args);
1545 }
1546
1547 /* Formats FORMAT as a printf string and appends the result to ST. */
1548 void
1549 ds_put_vformat (struct string *st, const char *format, va_list args_)
1550 {
1551   int avail, needed;
1552   va_list args;
1553
1554   va_copy (args, args_);
1555   avail = st->ss.string != NULL ? st->capacity - st->ss.length + 1 : 0;
1556   needed = vsnprintf (st->ss.string + st->ss.length, avail, format, args);
1557   va_end (args);
1558
1559   if (needed >= avail)
1560     {
1561       va_copy (args, args_);
1562       vsnprintf (ds_put_uninit (st, needed), needed + 1, format, args);
1563       va_end (args);
1564     }
1565   else
1566     {
1567       /* Some old libc's returned -1 when the destination string
1568          was too short. */
1569       while (needed == -1)
1570         {
1571           ds_extend (st, (st->capacity + 1) * 2);
1572           avail = st->capacity - st->ss.length + 1;
1573
1574           va_copy (args, args_);
1575           needed = vsnprintf (ds_end (st), avail, format, args);
1576           va_end (args);
1577         }
1578       st->ss.length += needed;
1579     }
1580 }
1581
1582 /* Formats FORMAT as a printf string, as if in the C locale,
1583    and appends the result to ST. */
1584 void
1585 ds_put_c_vformat (struct string *st, const char *format, va_list args)
1586 {
1587   char buf[128];
1588   size_t len = sizeof buf;
1589   char *output = c_vasnprintf (buf, &len, format, args);
1590   if (output)
1591     {
1592       ds_put_cstr (st, output);
1593       if (output != buf)
1594         free (output);
1595     }
1596 }
1597
1598 /* Appends byte CH to ST. */
1599 void
1600 ds_put_byte (struct string *st, int ch)
1601 {
1602   ds_put_uninit (st, 1)[0] = ch;
1603 }
1604
1605 /* Appends CNT copies of byte CH to ST. */
1606 void
1607 ds_put_byte_multiple (struct string *st, int ch, size_t cnt)
1608 {
1609   memset (ds_put_uninit (st, cnt), ch, cnt);
1610 }
1611
1612 /* Appends Unicode code point UC to ST in UTF-8 encoding. */
1613 void
1614 ds_put_unichar (struct string *st, ucs4_t uc)
1615 {
1616   ds_extend (st, ds_length (st) + 6);
1617   st->ss.length += u8_uctomb (CHAR_CAST (uint8_t *, ds_end (st)), uc, 6);
1618 }
1619
1620 /* If relocation has been enabled, replace ST,
1621    with its relocated version */
1622 void
1623 ds_relocate (struct string *st)
1624 {
1625   const char *orig = ds_cstr (st);
1626   const char *rel = relocate (orig);
1627
1628   if (orig != rel)
1629     {
1630       ds_clear (st);
1631       ds_put_cstr (st, rel);
1632       /* The documentation for relocate says that casting away const
1633         and then freeing is appropriate ... */
1634       free (CONST_CAST (char *, rel));
1635     }
1636 }
1637
1638
1639 \f
1640
1641 /* Operations on uint8_t "strings" */
1642
1643 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
1644    DST is truncated to DST_SIZE bytes or padded on the right with
1645    copies of PAD as needed. */
1646 void
1647 u8_buf_copy_rpad (uint8_t *dst, size_t dst_size,
1648                   const uint8_t *src, size_t src_size,
1649                   char pad)
1650 {
1651   if (src_size >= dst_size)
1652     memmove (dst, src, dst_size);
1653   else
1654     {
1655       memmove (dst, src, src_size);
1656       memset (&dst[src_size], pad, dst_size - src_size);
1657     }
1658 }