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