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