1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2014,
3 2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "libpspp/cast.h"
29 #include "libpspp/message.h"
30 #include "libpspp/pool.h"
32 #include "gl/c-ctype.h"
33 #include "gl/c-vasnprintf.h"
34 #include "gl/relocatable.h"
35 #include "gl/minmax.h"
36 #include "gl/xalloc.h"
37 #include "gl/xmemdup0.h"
40 /* Reverses the order of NBYTES bytes at address P, thus converting
41 between little- and big-endian byte orders. */
43 buf_reverse (char *p, size_t nbytes)
45 char *h = p, *t = &h[nbytes - 1];
57 /* Compares the SIZE bytes in A to those in B, disregarding case,
58 and returns a strcmp()-type result. */
60 buf_compare_case (const char *a_, const char *b_, size_t size)
62 const unsigned char *a = (unsigned char *) a_;
63 const unsigned char *b = (unsigned char *) b_;
67 unsigned char ac = toupper (*a++);
68 unsigned char bc = toupper (*b++);
71 return ac > bc ? 1 : -1;
77 /* Compares A of length A_LEN to B of length B_LEN. The shorter
78 string is considered to be padded with spaces to the length of
81 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
86 min_len = a_len < b_len ? a_len : b_len;
87 result = memcmp (a, b, min_len);
96 for (idx = min_len; idx < b_len; idx++)
98 return ' ' > b[idx] ? 1 : -1;
102 for (idx = min_len; idx < a_len; idx++)
104 return a[idx] > ' ' ? 1 : -1;
110 /* Compares strin A to string B. The shorter string is
111 considered to be padded with spaces to the length of the
114 str_compare_rpad (const char *a, const char *b)
116 return buf_compare_rpad (a, strlen (a), b, strlen (b));
119 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
120 DST is truncated to DST_SIZE bytes or padded on the right with
121 copies of PAD as needed. */
123 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src, char pad)
125 size_t src_len = strlen (src);
126 if (src_len >= dst_size)
127 memcpy (dst, src, dst_size);
130 memcpy (dst, src, src_len);
131 memset (&dst[src_len], pad, dst_size - src_len);
135 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
136 DST is truncated to DST_SIZE bytes or padded on the left with
137 copies of PAD as needed. */
139 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src, char pad)
141 size_t src_len = strlen (src);
142 if (src_len >= dst_size)
143 memcpy (dst, src, dst_size);
146 size_t pad_cnt = dst_size - src_len;
147 memset (&dst[0], pad, pad_cnt);
148 memcpy (dst + pad_cnt, src, src_len);
152 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
153 DST is truncated to DST_SIZE bytes or padded on the left with
154 copies of PAD as needed. */
156 buf_copy_lpad (char *dst, size_t dst_size,
157 const char *src, size_t src_size,
160 if (src_size >= dst_size)
161 memmove (dst, src, dst_size);
164 memset (dst, pad, dst_size - src_size);
165 memmove (&dst[dst_size - src_size], src, src_size);
169 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
170 DST is truncated to DST_SIZE bytes or padded on the right with
171 copies of PAD as needed. */
173 buf_copy_rpad (char *dst, size_t dst_size,
174 const char *src, size_t src_size,
177 if (src_size >= dst_size)
178 memmove (dst, src, dst_size);
181 memmove (dst, src, src_size);
182 memset (&dst[src_size], pad, dst_size - src_size);
186 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
188 Truncates DST to DST_SIZE - 1 bytes or right-pads with
189 spaces to DST_SIZE - 1 bytes if necessary. */
191 str_copy_rpad (char *dst, size_t dst_size, const char *src)
195 size_t src_len = strlen (src);
196 if (src_len < dst_size - 1)
198 memcpy (dst, src, src_len);
199 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
202 memcpy (dst, src, dst_size - 1);
203 dst[dst_size - 1] = 0;
207 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
208 Truncates DST to DST_SIZE - 1 bytes, if necessary. */
210 str_copy_trunc (char *dst, size_t dst_size, const char *src)
212 size_t src_len = strlen (src);
213 assert (dst_size > 0);
214 if (src_len + 1 < dst_size)
215 memcpy (dst, src, src_len + 1);
218 memcpy (dst, src, dst_size - 1);
219 dst[dst_size - 1] = '\0';
223 /* Copies buffer SRC, of SRC_LEN bytes,
224 to DST, which is in a buffer DST_SIZE bytes long.
225 Truncates DST to DST_SIZE - 1 bytes, if necessary. */
227 str_copy_buf_trunc (char *dst, size_t dst_size,
228 const char *src, size_t src_size)
231 assert (dst_size > 0);
233 dst_len = src_size < dst_size ? src_size : dst_size - 1;
234 memcpy (dst, src, dst_len);
238 /* Converts each byte in S to uppercase.
240 This is suitable only for ASCII strings. Use utf8_to_upper() for UTF-8
243 str_uppercase (char *s)
245 for (; *s != '\0'; s++)
246 *s = c_toupper ((unsigned char) *s);
249 /* Converts each byte in S to lowercase.
251 This is suitable only for ASCII strings. Use utf8_to_lower() for UTF-8
254 str_lowercase (char *s)
256 for (; *s != '\0'; s++)
257 *s = c_tolower ((unsigned char) *s);
260 /* Converts NUMBER into a string in 26-adic notation in BUFFER,
261 which has room for SIZE bytes. Uses uppercase if UPPERCASE is
262 true, otherwise lowercase, Returns true if successful, false
263 if NUMBER, plus a trailing null, is too large to fit in the
266 26-adic notation is "spreadsheet column numbering": 1 = A, 2 =
267 B, 3 = C, ... 26 = Z, 27 = AA, 28 = AB, 29 = AC, ...
269 26-adic notation is the special case of a k-adic numeration
270 system (aka bijective base-k numeration) with k=26. In k-adic
271 numeration, the digits are {1, 2, 3, ..., k} (there is no
272 digit 0), and integer 0 is represented by the empty string.
273 For more information, see
274 http://en.wikipedia.org/wiki/Bijective_numeration. */
276 str_format_26adic (unsigned long int number, bool uppercase,
277 char buffer[], size_t size)
280 = uppercase ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "abcdefghijklmnopqrstuvwxyz";
287 buffer[length++] = alphabet[number % 26];
293 buffer[length] = '\0';
295 buf_reverse (buffer, length);
304 /* Copies IN to buffer OUT with size OUT_SIZE, appending a null terminator. If
305 IN is too long for OUT, or if IN contains a new-line, replaces the tail with
308 OUT_SIZE must be at least 16. */
310 str_ellipsize (struct substring in, char *out, size_t out_size)
312 assert (out_size >= 16);
314 size_t out_maxlen = out_size - 1;
315 if (in.length > out_maxlen - 3)
319 while (out_len < in.length
320 && in.string[out_len] != '\n'
321 && in.string[out_len] != '\0'
322 && (in.string[out_len] != '\r'
323 || out_len + 1 >= in.length
324 || in.string[out_len + 1] != '\n'))
326 int mblen = u8_mblen (CHAR_CAST (const uint8_t *, in.string + out_len),
327 in.length - out_len);
328 if (mblen < 0 || out_len + mblen > out_maxlen)
333 memcpy (out, in.string, out_len);
334 strcpy (&out[out_len], out_len < in.length ? "..." : "");
337 /* Sets the SIZE bytes starting at BLOCK to C,
338 and returns the byte following BLOCK. */
340 mempset (void *block, int c, size_t size)
342 memset (block, c, size);
343 return (char *) block + size;
348 /* Returns a substring whose contents are the CNT bytes
349 starting at the (0-based) position START in SS. */
351 ss_substr (struct substring ss, size_t start, size_t cnt)
353 if (start < ss.length)
354 return ss_buffer (ss.string + start, MIN (cnt, ss.length - start));
356 return ss_buffer (ss.string + ss.length, 0);
359 /* Returns a substring whose contents are the first CNT
362 ss_head (struct substring ss, size_t cnt)
364 return ss_buffer (ss.string, MIN (cnt, ss.length));
367 /* Returns a substring whose contents are the last CNT bytes
370 ss_tail (struct substring ss, size_t cnt)
373 return ss_buffer (ss.string + (ss.length - cnt), cnt);
378 /* Makes a malloc()'d, null-terminated copy of the contents of OLD
379 and stores it in NEW. */
381 ss_alloc_substring (struct substring *new, struct substring old)
383 new->string = xmemdup0 (old.string, old.length);
384 new->length = old.length;
387 /* Allocates room for a CNT-byte string in NEW. */
389 ss_alloc_uninit (struct substring *new, size_t cnt)
391 new->string = xmalloc (cnt);
396 ss_realloc (struct substring *ss, size_t size)
398 ss->string = xrealloc (ss->string, size);
401 /* Makes a pool_alloc_unaligned()'d, null-terminated copy of the contents of
402 OLD in POOL, and stores it in NEW. */
404 ss_alloc_substring_pool (struct substring *new, struct substring old,
407 new->string = pool_alloc_unaligned (pool, old.length + 1);
408 new->length = old.length;
409 memcpy (new->string, old.string, old.length);
410 new->string[old.length] = '\0';
413 /* Allocates room for a CNT-byte string in NEW in POOL. */
415 ss_alloc_uninit_pool (struct substring *new, size_t cnt, struct pool *pool)
417 new->string = pool_alloc_unaligned (pool, cnt);
421 /* Frees the string that SS points to. */
423 ss_dealloc (struct substring *ss)
428 /* Truncates SS to at most CNT bytes in length. */
430 ss_truncate (struct substring *ss, size_t cnt)
432 if (ss->length > cnt)
436 /* Removes trailing bytes in TRIM_SET from SS.
437 Returns number of bytes removed. */
439 ss_rtrim (struct substring *ss, struct substring trim_set)
442 while (cnt < ss->length
443 && ss_find_byte (trim_set,
444 ss->string[ss->length - cnt - 1]) != SIZE_MAX)
450 /* Removes leading bytes in TRIM_SET from SS.
451 Returns number of bytes removed. */
453 ss_ltrim (struct substring *ss, struct substring trim_set)
455 size_t cnt = ss_span (*ss, trim_set);
456 ss_advance (ss, cnt);
460 /* Trims leading and trailing bytes in TRIM_SET from SS. */
462 ss_trim (struct substring *ss, struct substring trim_set)
464 ss_ltrim (ss, trim_set);
465 ss_rtrim (ss, trim_set);
468 /* If the last byte in SS is C, removes it and returns true.
469 Otherwise, returns false without changing the string. */
471 ss_chomp_byte (struct substring *ss, char c)
473 if (ss_last (*ss) == c)
482 /* If SS ends with SUFFIX, removes it and returns true.
483 Otherwise, returns false without changing the string. */
485 ss_chomp (struct substring *ss, struct substring suffix)
487 if (ss_ends_with (*ss, suffix))
489 ss->length -= suffix.length;
496 /* Divides SS into tokens separated by any of the DELIMITERS.
497 Each call replaces TOKEN by the next token in SS, or by an
498 empty string if no tokens remain. Returns true if a token was
499 obtained, false otherwise.
501 Before the first call, initialize *SAVE_IDX to 0. Do not
502 modify *SAVE_IDX between calls.
504 SS divides into exactly one more tokens than it contains
505 delimiters. That is, a delimiter at the start or end of SS or
506 a pair of adjacent delimiters yields an empty token, and the
507 empty string contains a single token. */
509 ss_separate (struct substring ss, struct substring delimiters,
510 size_t *save_idx, struct substring *token)
512 if (*save_idx <= ss_length (ss))
514 struct substring tmp = ss_substr (ss, *save_idx, SIZE_MAX);
515 size_t length = ss_cspan (tmp, delimiters);
516 *token = ss_head (tmp, length);
517 *save_idx += length + 1;
522 *token = ss_empty ();
527 /* Divides SS into tokens separated by any of the DELIMITERS,
528 merging adjacent delimiters so that the empty string is never
529 produced as a token. Each call replaces TOKEN by the next
530 token in SS, or by an empty string if no tokens remain, and
531 then skips past the first delimiter following the token.
532 Returns true if a token was obtained, false otherwise.
534 Before the first call, initialize *SAVE_IDX to 0. Do not
535 modify *SAVE_IDX between calls. */
537 ss_tokenize (struct substring ss, struct substring delimiters,
538 size_t *save_idx, struct substring *token)
542 ss_advance (&ss, *save_idx);
543 *save_idx += ss_ltrim (&ss, delimiters);
544 ss_get_bytes (&ss, ss_cspan (ss, delimiters), token);
546 found_token = ss_length (*token) > 0;
547 *save_idx += ss_length (*token) + (found_token?1:0);
551 /* Removes the first CNT bytes from SS. */
553 ss_advance (struct substring *ss, size_t cnt)
555 if (cnt > ss->length)
561 /* If the first byte in SS is C, removes it and returns true.
562 Otherwise, returns false without changing the string. */
564 ss_match_byte (struct substring *ss, char c)
566 if (ss_first (*ss) == c)
576 /* If the first byte in SS is in MATCH, removes it and
577 returns the byte that was removed.
578 Otherwise, returns EOF without changing the string. */
580 ss_match_byte_in (struct substring *ss, struct substring match)
584 && memchr (match.string, ss->string[0], match.length) != NULL)
593 /* If SS begins with TARGET, removes it and returns true.
594 Otherwise, returns false without changing SS. */
596 ss_match_string (struct substring *ss, const struct substring target)
598 size_t length = ss_length (target);
599 if (ss_equals (ss_head (*ss, length), target))
601 ss_advance (ss, length);
608 /* Removes the first byte from SS and returns it.
609 If SS is empty, returns EOF without modifying SS. */
611 ss_get_byte (struct substring *ss)
613 int c = ss_first (*ss);
622 /* Stores the prefix of SS up to the first DELIMITER in OUT (if
623 any). Trims those same bytes from SS. DELIMITER is
624 removed from SS but not made part of OUT. Returns true if
625 DELIMITER was found (and removed), false otherwise. */
627 ss_get_until (struct substring *ss, char delimiter, struct substring *out)
629 ss_get_bytes (ss, ss_cspan (*ss, ss_buffer (&delimiter, 1)), out);
630 return ss_match_byte (ss, delimiter);
633 /* Stores the first CNT bytes in SS in OUT (or fewer, if SS
634 is shorter than CNT bytes). Trims the same bytes
635 from the beginning of SS. Returns CNT. */
637 ss_get_bytes (struct substring *ss, size_t cnt, struct substring *out)
639 *out = ss_head (*ss, cnt);
640 ss_advance (ss, cnt);
644 /* Parses and removes an optionally signed decimal integer from
645 the beginning of SS. Returns 0 if an error occurred,
646 otherwise the number of bytes removed from SS. Stores
647 the integer's value into *VALUE. */
649 ss_get_long (struct substring *ss, long *value)
654 length = ss_span (*ss, ss_cstr ("+-"));
655 length += ss_span (ss_substr (*ss, length, SIZE_MAX), ss_cstr (CC_DIGITS));
656 if (length > 0 && length < sizeof tmp)
660 memcpy (tmp, ss_data (*ss), length);
663 *value = strtol (tmp, &tail, 10);
664 if (tail - tmp == length)
666 ss_advance (ss, length);
674 /* Returns true if SS is empty (has length 0 bytes),
677 ss_is_empty (struct substring ss)
679 return ss.length == 0;
682 /* Returns the number of bytes in SS. */
684 ss_length (struct substring ss)
689 /* Returns a pointer to the bytes in SS. */
691 ss_data (struct substring ss)
696 /* Returns a pointer just past the last byte in SS. */
698 ss_end (struct substring ss)
700 return ss.string + ss.length;
703 /* Returns the byte in position IDX in SS, as a value in the
704 range of unsigned char. Returns EOF if IDX is out of the
705 range of indexes for SS. */
707 ss_at (struct substring ss, size_t idx)
709 return idx < ss.length ? (unsigned char) ss.string[idx] : EOF;
712 /* Returns the first byte in SS as a value in the range of
713 unsigned char. Returns EOF if SS is the empty string. */
715 ss_first (struct substring ss)
717 return ss_at (ss, 0);
720 /* Returns the last byte in SS as a value in the range of
721 unsigned char. Returns EOF if SS is the empty string. */
723 ss_last (struct substring ss)
725 return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF;
728 /* Returns true if SS starts with PREFIX, false otherwise. */
730 ss_starts_with (struct substring ss, struct substring prefix)
732 return (ss.length >= prefix.length
733 && !memcmp (ss.string, prefix.string, prefix.length));
736 /* Returns true if SS ends with SUFFIX, false otherwise. */
738 ss_ends_with (struct substring ss, struct substring suffix)
740 return (ss.length >= suffix.length
741 && !memcmp (&ss.string[ss.length - suffix.length], suffix.string,
745 /* Returns the number of contiguous bytes at the beginning
746 of SS that are in SKIP_SET. */
748 ss_span (struct substring ss, struct substring skip_set)
751 for (i = 0; i < ss.length; i++)
752 if (ss_find_byte (skip_set, ss.string[i]) == SIZE_MAX)
757 /* Returns the number of contiguous bytes at the beginning
758 of SS that are not in SKIP_SET. */
760 ss_cspan (struct substring ss, struct substring stop_set)
763 for (i = 0; i < ss.length; i++)
764 if (ss_find_byte (stop_set, ss.string[i]) != SIZE_MAX)
769 /* Returns the offset in SS of the first instance of C,
770 or SIZE_MAX if C does not occur in SS. */
772 ss_find_byte (struct substring ss, char c)
774 const char *p = memchr (ss.string, (int) c, ss.length);
775 return p != NULL ? p - ss.string : SIZE_MAX;
778 /* Returns the offset in HAYSTACK of the first instance of NEEDLE,
779 or SIZE_MAX if NEEDLE does not occur in HAYSTACK. */
781 ss_find_substring (struct substring haystack, struct substring needle)
783 const char *p = memmem (haystack.string, haystack.length,
784 needle.string, needle.length);
785 return p != NULL ? p - haystack.string : SIZE_MAX;
788 /* Compares A and B and returns a strcmp()-type comparison
791 ss_compare (struct substring a, struct substring b)
793 int retval = memcmp (a.string, b.string, MIN (a.length, b.length));
795 retval = a.length < b.length ? -1 : a.length > b.length;
799 /* Compares A and B case-insensitively and returns a
800 strcmp()-type comparison result. */
802 ss_compare_case (struct substring a, struct substring b)
804 int retval = memcasecmp (a.string, b.string, MIN (a.length, b.length));
806 retval = a.length < b.length ? -1 : a.length > b.length;
810 /* Compares A and B and returns true if their contents are
811 identical, false otherwise. */
813 ss_equals (struct substring a, struct substring b)
815 return a.length == b.length && !memcmp (a.string, b.string, a.length);
818 /* Compares A and B and returns true if their contents are
819 identical except possibly for case differences, false
822 ss_equals_case (struct substring a, struct substring b)
824 return a.length == b.length && !memcasecmp (a.string, b.string, a.length);
827 /* Returns the position in SS that the byte at P occupies.
828 P must point within SS or one past its end. */
830 ss_pointer_to_position (struct substring ss, const char *p)
832 size_t pos = p - ss.string;
833 assert (pos <= ss.length);
837 /* Allocates and returns a null-terminated string that contains
840 ss_xstrdup (struct substring ss)
842 char *s = xmalloc (ss.length + 1);
843 memcpy (s, ss.string, ss.length);
849 /* Returns the character represented by the UTF-8 sequence at the start of S.
850 The return value is either a Unicode code point in the range 0 to 0x10ffff,
851 or UINT32_MAX if S is empty. */
853 ss_first_mb (struct substring s)
855 return ss_at_mb (s, 0);
858 /* Returns the number of bytes in the UTF-8 character at the beginning of S.
860 The return value is 0 if S is empty, otherwise between 1 and 4. */
862 ss_first_mblen (struct substring s)
864 return ss_at_mblen (s, 0);
867 /* Advances S past the UTF-8 character at its beginning. Returns the Unicode
868 code point that was skipped (in the range 0 to 0x10ffff), or UINT32_MAX if S
869 was not modified because it was initially empty. */
871 ss_get_mb (struct substring *s)
878 n = u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s->string), s->length);
887 /* Returns the character represented by the UTF-8 sequence starting OFS bytes
888 into S. The return value is either a Unicode code point in the range 0 to
889 0x10ffff, or UINT32_MAX if OFS is past the last byte in S.
891 (Returns 0xfffd if OFS points into the middle, not the beginning, of a UTF-8
894 ss_at_mb (struct substring s, size_t ofs)
899 u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s.string + ofs),
907 /* Returns the number of bytes represented by the UTF-8 sequence starting OFS
908 bytes into S. The return value is 0 if OFS is past the last byte in S,
909 otherwise between 1 and 4. */
911 ss_at_mblen (struct substring s, size_t ofs)
916 return u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, s.string + ofs),
923 /* Initializes ST as an empty string. */
925 ds_init_empty (struct string *st)
927 st->ss = ss_empty ();
931 /* Initializes ST with initial contents S. */
933 ds_init_string (struct string *st, const struct string *s)
935 ds_init_substring (st, ds_ss (s));
938 /* Initializes ST with initial contents SS. */
940 ds_init_substring (struct string *st, struct substring ss)
942 st->capacity = MAX (8, ss.length * 2);
943 st->ss.string = xmalloc (st->capacity + 1);
944 memcpy (st->ss.string, ss.string, ss.length);
945 st->ss.length = ss.length;
948 /* Initializes ST with initial contents S. */
950 ds_init_cstr (struct string *st, const char *s)
952 ds_init_substring (st, ss_cstr (s));
957 ds_destroy (struct string *st)
961 ss_dealloc (&st->ss);
962 st->ss.string = NULL;
968 /* Swaps the contents of strings A and B. */
970 ds_swap (struct string *a, struct string *b)
972 struct string tmp = *a;
977 /* Helper function for ds_register_pool. */
979 free_string (void *st_)
981 struct string *st = st_;
985 /* Arranges for ST to be destroyed automatically as part of
988 ds_register_pool (struct string *st, struct pool *pool)
990 pool_register (pool, free_string, st);
993 /* Cancels the arrangement for ST to be destroyed automatically
996 ds_unregister_pool (struct string *st, struct pool *pool)
998 pool_unregister (pool, st);
1001 /* Copies SRC into DST.
1002 DST and SRC may be the same string. */
1004 ds_assign_string (struct string *dst, const struct string *src)
1006 ds_assign_substring (dst, ds_ss (src));
1009 /* Replaces DST by SS.
1010 SS may be a substring of DST. */
1012 ds_assign_substring (struct string *dst, struct substring ss)
1014 dst->ss.length = ss.length;
1015 ds_extend (dst, ss.length);
1016 memmove (dst->ss.string, ss.string, ss.length);
1019 /* Replaces DST by null-terminated string SRC. SRC may overlap
1022 ds_assign_cstr (struct string *dst, const char *src)
1024 ds_assign_substring (dst, ss_cstr (src));
1027 /* Truncates ST to zero length. */
1029 ds_clear (struct string *st)
1034 /* Returns a substring that contains ST. */
1036 ds_ss (const struct string *st)
1041 /* Returns a substring that contains CNT bytes from ST
1042 starting at position START.
1044 If START is greater than or equal to the length of ST, then
1045 the substring will be the empty string. If START + CNT
1046 exceeds the length of ST, then the substring will only be
1047 ds_length(ST) - START bytes long. */
1049 ds_substr (const struct string *st, size_t start, size_t cnt)
1051 return ss_substr (ds_ss (st), start, cnt);
1054 /* Returns a substring that contains the first CNT bytes in
1055 ST. If CNT exceeds the length of ST, then the substring will
1056 contain all of ST. */
1058 ds_head (const struct string *st, size_t cnt)
1060 return ss_head (ds_ss (st), cnt);
1063 /* Returns a substring that contains the last CNT bytes in
1064 ST. If CNT exceeds the length of ST, then the substring will
1065 contain all of ST. */
1067 ds_tail (const struct string *st, size_t cnt)
1069 return ss_tail (ds_ss (st), cnt);
1072 /* Ensures that ST can hold at least MIN_CAPACITY bytes plus a null
1075 ds_extend (struct string *st, size_t min_capacity)
1077 if (min_capacity > st->capacity)
1080 if (st->capacity < min_capacity)
1081 st->capacity = 2 * min_capacity;
1083 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
1087 /* Shrink ST to the minimum capacity need to contain its content. */
1089 ds_shrink (struct string *st)
1091 if (st->capacity != st->ss.length)
1093 st->capacity = st->ss.length;
1094 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
1098 /* Truncates ST to at most LENGTH bytes long. */
1100 ds_truncate (struct string *st, size_t length)
1102 ss_truncate (&st->ss, length);
1105 /* Removes trailing bytes in TRIM_SET from ST.
1106 Returns number of bytes removed. */
1108 ds_rtrim (struct string *st, struct substring trim_set)
1110 return ss_rtrim (&st->ss, trim_set);
1113 /* Removes leading bytes in TRIM_SET from ST.
1114 Returns number of bytes removed. */
1116 ds_ltrim (struct string *st, struct substring trim_set)
1118 size_t cnt = ds_span (st, trim_set);
1120 ds_assign_substring (st, ds_substr (st, cnt, SIZE_MAX));
1124 /* Trims leading and trailing bytes in TRIM_SET from ST.
1125 Returns number of bytes removed. */
1127 ds_trim (struct string *st, struct substring trim_set)
1129 size_t cnt = ds_rtrim (st, trim_set);
1130 return cnt + ds_ltrim (st, trim_set);
1133 /* If the last byte in ST is C, removes it and returns true.
1134 Otherwise, returns false without modifying ST. */
1136 ds_chomp_byte (struct string *st, char c)
1138 return ss_chomp_byte (&st->ss, c);
1141 /* If ST ends with SUFFIX, removes it and returns true.
1142 Otherwise, returns false without modifying ST. */
1144 ds_chomp (struct string *st, struct substring suffix)
1146 return ss_chomp (&st->ss, suffix);
1149 /* Divides ST into tokens separated by any of the DELIMITERS.
1150 Each call replaces TOKEN by the next token in ST, or by an
1151 empty string if no tokens remain. Returns true if a token was
1152 obtained, false otherwise.
1154 Before the first call, initialize *SAVE_IDX to 0. Do not
1155 modify *SAVE_IDX between calls.
1157 ST divides into exactly one more tokens than it contains
1158 delimiters. That is, a delimiter at the start or end of ST or
1159 a pair of adjacent delimiters yields an empty token, and the
1160 empty string contains a single token. */
1162 ds_separate (const struct string *st, struct substring delimiters,
1163 size_t *save_idx, struct substring *token)
1165 return ss_separate (ds_ss (st), delimiters, save_idx, token);
1168 /* Divides ST into tokens separated by any of the DELIMITERS,
1169 merging adjacent delimiters so that the empty string is never
1170 produced as a token. Each call replaces TOKEN by the next
1171 token in ST, or by an empty string if no tokens remain.
1172 Returns true if a token was obtained, false otherwise.
1174 Before the first call, initialize *SAVE_IDX to 0. Do not
1175 modify *SAVE_IDX between calls. */
1177 ds_tokenize (const struct string *st, struct substring delimiters,
1178 size_t *save_idx, struct substring *token)
1180 return ss_tokenize (ds_ss (st), delimiters, save_idx, token);
1183 /* Pad ST on the right with copies of PAD until ST is at least
1184 LENGTH bytes in size. If ST is initially LENGTH
1185 bytes or longer, this is a no-op. */
1187 ds_rpad (struct string *st, size_t length, char pad)
1189 if (length > st->ss.length)
1190 ds_put_byte_multiple (st, pad, length - st->ss.length);
1193 /* Sets the length of ST to exactly NEW_LENGTH,
1194 either by truncating bytes from the end,
1195 or by padding on the right with PAD. */
1197 ds_set_length (struct string *st, size_t new_length, char pad)
1199 if (st->ss.length < new_length)
1200 ds_rpad (st, new_length, pad);
1202 st->ss.length = new_length;
1205 /* Removes N bytes from ST starting at offset START. */
1207 ds_remove (struct string *st, size_t start, size_t n)
1209 if (n > 0 && start < st->ss.length)
1211 if (st->ss.length - start <= n)
1213 /* All bytes at or beyond START are deleted. */
1214 st->ss.length = start;
1218 /* Some bytes remain and must be shifted into
1220 memmove (st->ss.string + st->ss.length,
1221 st->ss.string + st->ss.length + n,
1222 st->ss.length - start - n);
1228 /* There are no bytes to delete or no bytes at or
1229 beyond START, hence deletion is a no-op. */
1233 /* Returns true if ST is empty, false otherwise. */
1235 ds_is_empty (const struct string *st)
1237 return ss_is_empty (st->ss);
1240 /* Returns the length of ST. */
1242 ds_length (const struct string *st)
1244 return ss_length (ds_ss (st));
1247 /* Returns the string data inside ST. */
1249 ds_data (const struct string *st)
1251 return ss_data (ds_ss (st));
1254 /* Returns a pointer to the null terminator ST.
1255 This might not be an actual null byte unless ds_c_str() has
1256 been called since the last modification to ST. */
1258 ds_end (const struct string *st)
1260 return ss_end (ds_ss (st));
1263 /* Returns the byte in position IDX in ST, as a value in the
1264 range of unsigned char. Returns EOF if IDX is out of the
1265 range of indexes for ST. */
1267 ds_at (const struct string *st, size_t idx)
1269 return ss_at (ds_ss (st), idx);
1272 /* Returns the first byte in ST as a value in the range of
1273 unsigned char. Returns EOF if ST is the empty string. */
1275 ds_first (const struct string *st)
1277 return ss_first (ds_ss (st));
1280 /* Returns the last byte in ST as a value in the range of
1281 unsigned char. Returns EOF if ST is the empty string. */
1283 ds_last (const struct string *st)
1285 return ss_last (ds_ss (st));
1288 /* Returns true if ST ends with SUFFIX, false otherwise. */
1290 ds_ends_with (const struct string *st, struct substring suffix)
1292 return ss_ends_with (st->ss, suffix);
1295 /* Returns the number of consecutive bytes at the beginning
1296 of ST that are in SKIP_SET. */
1298 ds_span (const struct string *st, struct substring skip_set)
1300 return ss_span (ds_ss (st), skip_set);
1303 /* Returns the number of consecutive bytes at the beginning
1304 of ST that are not in STOP_SET. */
1306 ds_cspan (const struct string *st, struct substring stop_set)
1308 return ss_cspan (ds_ss (st), stop_set);
1311 /* Returns the position of the first occurrence of byte C in
1312 ST at or after position OFS, or SIZE_MAX if there is no such
1315 ds_find_byte (const struct string *st, char c)
1317 return ss_find_byte (ds_ss (st), c);
1320 /* Compares A and B and returns a strcmp()-type comparison
1323 ds_compare (const struct string *a, const struct string *b)
1325 return ss_compare (ds_ss (a), ds_ss (b));
1328 /* Returns the position in ST that the byte at P occupies.
1329 P must point within ST or one past its end. */
1331 ds_pointer_to_position (const struct string *st, const char *p)
1333 return ss_pointer_to_position (ds_ss (st), p);
1336 /* Allocates and returns a null-terminated string that contains
1339 ds_xstrdup (const struct string *st)
1341 return ss_xstrdup (ds_ss (st));
1344 /* Returns the allocation size of ST. */
1346 ds_capacity (const struct string *st)
1348 return st->capacity;
1351 /* Returns the value of ST as a null-terminated string. */
1353 ds_cstr (const struct string *st_)
1355 struct string *st = CONST_CAST (struct string *, st_);
1356 if (st->ss.string == NULL)
1358 st->ss.string[st->ss.length] = '\0';
1359 return st->ss.string;
1362 /* Returns the value of ST as a null-terminated string and then
1363 reinitialized ST as an empty string. The caller must free the
1364 returned string with free(). */
1366 ds_steal_cstr (struct string *st)
1368 char *s = ds_cstr (st);
1373 /* Reads bytes from STREAM and appends them to ST, stopping
1374 after MAX_LENGTH bytes, after appending a newline, or
1375 after an I/O error or end of file was encountered, whichever
1376 comes first. Returns true if at least one byte was added
1377 to ST, false if no bytes were read before an I/O error or
1378 end of file (or if MAX_LENGTH was 0).
1380 This function treats LF and CR LF sequences as new-line,
1381 translating each of them to a single '\n' in ST. */
1383 ds_read_line (struct string *st, FILE *stream, size_t max_length)
1387 for (length = 0; length < max_length; length++)
1389 int c = getc (stream);
1396 ds_put_byte (st, c);
1403 /* CR followed by LF is special: translate to \n. */
1404 ds_put_byte (st, '\n');
1409 /* CR followed by anything else is just CR. */
1410 ds_put_byte (st, '\r');
1418 ds_put_byte (st, c);
1425 /* Removes a comment introduced by `#' from ST,
1426 ignoring occurrences inside quoted strings. */
1428 remove_comment (struct string *st)
1433 for (cp = ds_data (st); cp < ds_end (st); cp++)
1438 else if (*cp == '\\')
1441 else if (*cp == '\'' || *cp == '"')
1443 else if (*cp == '#')
1445 ds_truncate (st, cp - ds_cstr (st));
1450 /* Reads a line from STREAM into ST, then preprocesses as follows:
1452 - Splices lines terminated with `\'.
1454 - Deletes comments introduced by `#' outside of single or double
1457 - Deletes trailing white space.
1459 Returns true if a line was successfully read, false on
1460 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
1461 incremented by the number of lines read. */
1463 ds_read_config_line (struct string *st, int *line_number, FILE *stream)
1468 if (!ds_read_line (st, stream, SIZE_MAX))
1471 ds_rtrim (st, ss_cstr (CC_SPACES));
1473 while (ds_chomp_byte (st, '\\'));
1475 remove_comment (st);
1479 /* Attempts to read SIZE * CNT bytes from STREAM and append them
1481 Returns true if all the requested data was read, false otherwise. */
1483 ds_read_stream (struct string *st, size_t size, size_t cnt, FILE *stream)
1487 size_t try_bytes = xtimes (cnt, size);
1488 if (size_in_bounds_p (xsum (ds_length (st), try_bytes)))
1490 char *buffer = ds_put_uninit (st, try_bytes);
1491 size_t got_bytes = fread (buffer, 1, try_bytes, stream);
1492 ds_truncate (st, ds_length (st) - (try_bytes - got_bytes));
1493 return got_bytes == try_bytes;
1505 /* Concatenates S onto ST. */
1507 ds_put_cstr (struct string *st, const char *s)
1510 ds_put_substring (st, ss_cstr (s));
1513 /* Concatenates SS to ST. */
1515 ds_put_substring (struct string *st, struct substring ss)
1518 memcpy (ds_put_uninit (st, ss_length (ss)), ss_data (ss), ss_length (ss));
1521 /* Returns ds_end(ST) and THEN increases the length by INCR. */
1523 ds_put_uninit (struct string *st, size_t incr)
1526 ds_extend (st, ds_length (st) + incr);
1528 st->ss.length += incr;
1532 /* Moves the bytes in ST following offset OFS + OLD_LEN in ST to offset OFS +
1533 NEW_LEN and returns the byte at offset OFS. The first min(OLD_LEN, NEW_LEN)
1534 bytes at the returned position are unchanged; if NEW_LEN > OLD_LEN then the
1535 following NEW_LEN - OLD_LEN bytes are initially indeterminate.
1537 The intention is that the caller should write NEW_LEN bytes at the returned
1538 position, to effectively replace the OLD_LEN bytes previously at that
1541 ds_splice_uninit (struct string *st,
1542 size_t ofs, size_t old_len, size_t new_len)
1544 if (new_len != old_len)
1546 if (new_len > old_len)
1547 ds_extend (st, ds_length (st) + (new_len - old_len));
1549 assert (ds_length (st) >= ofs + old_len);
1551 memmove (ds_data (st) + (ofs + new_len),
1552 ds_data (st) + (ofs + old_len),
1553 ds_length (st) - (ofs + old_len));
1554 st->ss.length += new_len - old_len;
1556 return ds_data (st) + ofs;
1559 /* Formats FORMAT as a printf string and appends the result to ST. */
1561 ds_put_format (struct string *st, const char *format, ...)
1565 va_start (args, format);
1566 ds_put_vformat (st, format, args);
1570 /* Formats FORMAT as a printf string as if in the C locale and appends the result to ST. */
1572 ds_put_c_format (struct string *st, const char *format, ...)
1576 va_start (args, format);
1577 ds_put_c_vformat (st, format, args);
1581 /* Formats FORMAT as a printf string and appends the result to ST. */
1583 ds_put_vformat (struct string *st, const char *format, va_list args_)
1588 va_copy (args, args_);
1589 avail = st->ss.string != NULL ? st->capacity - st->ss.length + 1 : 0;
1590 needed = vsnprintf (st->ss.string + st->ss.length, avail, format, args);
1593 if (needed >= avail)
1595 va_copy (args, args_);
1596 vsnprintf (ds_put_uninit (st, needed), needed + 1, format, args);
1601 /* Some old libc's returned -1 when the destination string
1603 while (needed == -1)
1605 ds_extend (st, (st->capacity + 1) * 2);
1606 avail = st->capacity - st->ss.length + 1;
1608 va_copy (args, args_);
1609 needed = vsnprintf (ds_end (st), avail, format, args);
1612 st->ss.length += needed;
1616 /* Formats FORMAT as a printf string, as if in the C locale,
1617 and appends the result to ST. */
1619 ds_put_c_vformat (struct string *st, const char *format, va_list args)
1622 size_t len = sizeof buf;
1623 char *output = c_vasnprintf (buf, &len, format, args);
1626 ds_put_cstr (st, output);
1632 /* Appends byte CH to ST. */
1634 ds_put_byte (struct string *st, int ch)
1636 ds_put_uninit (st, 1)[0] = ch;
1639 /* Appends CNT copies of byte CH to ST. */
1641 ds_put_byte_multiple (struct string *st, int ch, size_t cnt)
1643 memset (ds_put_uninit (st, cnt), ch, cnt);
1646 /* Appends Unicode code point UC to ST in UTF-8 encoding. */
1648 ds_put_unichar (struct string *st, ucs4_t uc)
1650 ds_extend (st, ds_length (st) + 6);
1651 st->ss.length += u8_uctomb (CHAR_CAST (uint8_t *, ds_end (st)), uc, 6);
1654 /* If relocation has been enabled, replace ST,
1655 with its relocated version */
1657 ds_relocate (struct string *st)
1659 const char *orig = ds_cstr (st);
1660 const char *rel = relocate (orig);
1665 ds_put_cstr (st, rel);
1666 /* The documentation for relocate says that casting away const
1667 and then freeing is appropriate ... */
1668 free (CONST_CAST (char *, rel));
1675 /* Operations on uint8_t "strings" */
1677 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
1678 DST is truncated to DST_SIZE bytes or padded on the right with
1679 copies of PAD as needed. */
1681 u8_buf_copy_rpad (uint8_t *dst, size_t dst_size,
1682 const uint8_t *src, size_t src_size,
1685 if (src_size >= dst_size)
1686 memmove (dst, src, dst_size);
1689 memmove (dst, src, src_size);
1690 memset (&dst[src_size], pad, dst_size - src_size);