1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
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.
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.
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/>. */
26 #include <libpspp/message.h>
27 #include <libpspp/pool.h>
33 /* Reverses the order of NBYTES bytes at address P, thus converting
34 between little- and big-endian byte orders. */
36 buf_reverse (char *p, size_t nbytes)
38 char *h = p, *t = &h[nbytes - 1];
50 /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length
51 HAYSTACK_LEN. Returns a pointer to the needle found. */
53 buf_find_reverse (const char *haystack, size_t haystack_len,
54 const char *needle, size_t needle_len)
57 for (i = haystack_len - needle_len; i >= 0; i--)
58 if (!memcmp (needle, &haystack[i], needle_len))
59 return (char *) &haystack[i];
63 /* Compares the SIZE bytes in A to those in B, disregarding case,
64 and returns a strcmp()-type result. */
66 buf_compare_case (const char *a_, const char *b_, size_t size)
68 const unsigned char *a = (unsigned char *) a_;
69 const unsigned char *b = (unsigned char *) b_;
73 unsigned char ac = toupper (*a++);
74 unsigned char bc = toupper (*b++);
77 return ac > bc ? 1 : -1;
83 /* Compares A of length A_LEN to B of length B_LEN. The shorter
84 string is considered to be padded with spaces to the length of
87 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
92 min_len = a_len < b_len ? a_len : b_len;
93 result = memcmp (a, b, min_len);
102 for (idx = min_len; idx < b_len; idx++)
104 return ' ' > b[idx] ? 1 : -1;
108 for (idx = min_len; idx < a_len; idx++)
110 return a[idx] > ' ' ? 1 : -1;
116 /* Compares strin A to string B. The shorter string is
117 considered to be padded with spaces to the length of the
120 str_compare_rpad (const char *a, const char *b)
122 return buf_compare_rpad (a, strlen (a), b, strlen (b));
125 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
126 DST is truncated to DST_SIZE bytes or padded on the right with
129 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src)
131 size_t src_len = strlen (src);
132 if (src_len >= dst_size)
133 memcpy (dst, src, dst_size);
136 memcpy (dst, src, src_len);
137 memset (&dst[src_len], ' ', dst_size - src_len);
141 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
142 DST is truncated to DST_SIZE bytes or padded on the left with
145 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src)
147 size_t src_len = strlen (src);
148 if (src_len >= dst_size)
149 memcpy (dst, src, dst_size);
152 size_t pad_cnt = dst_size - src_len;
153 memset (&dst[0], ' ', pad_cnt);
154 memcpy (dst + pad_cnt, src, src_len);
158 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
159 DST is truncated to DST_SIZE bytes or padded on the left with
162 buf_copy_lpad (char *dst, size_t dst_size,
163 const char *src, size_t src_size)
165 if (src_size >= dst_size)
166 memmove (dst, src, dst_size);
169 memset (dst, ' ', dst_size - src_size);
170 memmove (&dst[dst_size - src_size], src, src_size);
174 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
175 DST is truncated to DST_SIZE bytes or padded on the right with
178 buf_copy_rpad (char *dst, size_t dst_size,
179 const char *src, size_t src_size)
181 if (src_size >= dst_size)
182 memmove (dst, src, dst_size);
185 memmove (dst, src, src_size);
186 memset (&dst[src_size], ' ', dst_size - src_size);
190 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
192 Truncates DST to DST_SIZE - 1 characters or right-pads with
193 spaces to DST_SIZE - 1 characters if necessary. */
195 str_copy_rpad (char *dst, size_t dst_size, const char *src)
197 size_t src_len = strlen (src);
198 if (src_len < dst_size - 1)
200 memcpy (dst, src, src_len);
201 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
204 memcpy (dst, src, dst_size - 1);
205 dst[dst_size - 1] = 0;
208 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
209 Truncates DST to DST_SIZE - 1 characters, if necessary. */
211 str_copy_trunc (char *dst, size_t dst_size, const char *src)
213 size_t src_len = strlen (src);
214 assert (dst_size > 0);
215 if (src_len + 1 < dst_size)
216 memcpy (dst, src, src_len + 1);
219 memcpy (dst, src, dst_size - 1);
220 dst[dst_size - 1] = '\0';
224 /* Copies buffer SRC, of SRC_LEN bytes,
225 to DST, which is in a buffer DST_SIZE bytes long.
226 Truncates DST to DST_SIZE - 1 characters, if necessary. */
228 str_copy_buf_trunc (char *dst, size_t dst_size,
229 const char *src, size_t src_size)
232 assert (dst_size > 0);
234 dst_len = src_size < dst_size ? src_size : dst_size - 1;
235 memcpy (dst, src, dst_len);
239 /* Converts each character in S to uppercase. */
241 str_uppercase (char *s)
243 for (; *s != '\0'; s++)
244 *s = toupper ((unsigned char) *s);
247 /* Converts each character in S to lowercase. */
249 str_lowercase (char *s)
251 for (; *s != '\0'; s++)
252 *s = tolower ((unsigned char) *s);
255 /* Converts NUMBER into a string in 26-adic notation in BUFFER,
256 which has room for SIZE bytes. Returns true if successful,
257 false if NUMBER, plus a trailing null, is too large to fit in
260 26-adic notation is "spreadsheet column numbering": 1 = A, 2 =
261 B, 3 = C, ... 26 = Z, 27 = AA, 28 = AB, 29 = AC, ...
263 26-adic notation is the special case of a k-adic numeration
264 system (aka bijective base-k numeration) with k=26. In k-adic
265 numeration, the digits are {1, 2, 3, ..., k} (there is no
266 digit 0), and integer 0 is represented by the empty string.
267 For more information, see
268 http://en.wikipedia.org/wiki/Bijective_numeration. */
270 str_format_26adic (unsigned long int number, char buffer[], size_t size)
278 buffer[length++] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[number % 26];
284 buffer[length] = '\0';
286 buf_reverse (buffer, length);
290 /* Formats FORMAT into DST, as with sprintf(), and returns the
291 address of the terminating null written to DST. */
293 spprintf (char *dst, const char *format, ...)
298 va_start (args, format);
299 count = vsprintf (dst, format, args);
305 /* Sets the SIZE bytes starting at BLOCK to C,
306 and returns the byte following BLOCK. */
308 mempset (void *block, int c, size_t size)
310 memset (block, c, size);
311 return (char *) block + size;
316 /* Returns an empty substring. */
326 /* Returns a substring whose contents are the given C-style
329 ss_cstr (const char *cstr)
331 return ss_buffer (cstr, strlen (cstr));
334 /* Returns a substring whose contents are the CNT characters in
337 ss_buffer (const char *buffer, size_t cnt)
340 ss.string = (char *) buffer;
345 /* Returns a substring whose contents are the CNT characters
346 starting at the (0-based) position START in SS. */
348 ss_substr (struct substring ss, size_t start, size_t cnt)
350 if (start < ss.length)
351 return ss_buffer (ss.string + start, MIN (cnt, ss.length - start));
353 return ss_buffer (ss.string + ss.length, 0);
356 /* Returns a substring whose contents are the first CNT
359 ss_head (struct substring ss, size_t cnt)
361 return ss_buffer (ss.string, MIN (cnt, ss.length));
364 /* Returns a substring whose contents are the last CNT characters
367 ss_tail (struct substring ss, size_t cnt)
370 return ss_buffer (ss.string + (ss.length - cnt), cnt);
375 /* Makes a malloc()'d copy of the contents of OLD
376 and stores it in NEW. */
378 ss_alloc_substring (struct substring *new, struct substring old)
380 new->string = xmalloc (old.length);
381 new->length = old.length;
382 memcpy (new->string, old.string, old.length);
385 /* Allocates room for a CNT-character string in NEW. */
387 ss_alloc_uninit (struct substring *new, size_t cnt)
389 new->string = xmalloc (cnt);
393 /* Makes a pool_alloc_unaligned()'d copy of the contents of OLD
394 in POOL, and stores it in NEW. */
396 ss_alloc_substring_pool (struct substring *new, struct substring old,
399 new->string = pool_alloc_unaligned (pool, old.length);
400 new->length = old.length;
401 memcpy (new->string, old.string, old.length);
404 /* Allocates room for a CNT-character string in NEW in POOL. */
406 ss_alloc_uninit_pool (struct substring *new, size_t cnt, struct pool *pool)
408 new->string = pool_alloc_unaligned (pool, cnt);
412 /* Frees the string that SS points to. */
414 ss_dealloc (struct substring *ss)
419 /* Truncates SS to at most CNT characters in length. */
421 ss_truncate (struct substring *ss, size_t cnt)
423 if (ss->length > cnt)
427 /* Removes trailing characters in TRIM_SET from SS.
428 Returns number of characters removed. */
430 ss_rtrim (struct substring *ss, struct substring trim_set)
433 while (cnt < ss->length
434 && ss_find_char (trim_set,
435 ss->string[ss->length - cnt - 1]) != SIZE_MAX)
441 /* Removes leading characters in TRIM_SET from SS.
442 Returns number of characters removed. */
444 ss_ltrim (struct substring *ss, struct substring trim_set)
446 size_t cnt = ss_span (*ss, trim_set);
447 ss_advance (ss, cnt);
451 /* Trims leading and trailing characters in TRIM_SET from SS. */
453 ss_trim (struct substring *ss, struct substring trim_set)
455 ss_ltrim (ss, trim_set);
456 ss_rtrim (ss, trim_set);
459 /* If the last character in SS is C, removes it and returns true.
460 Otherwise, returns false without changing the string. */
462 ss_chomp (struct substring *ss, char c)
464 if (ss_last (*ss) == c)
473 /* Divides SS into tokens separated by any of the DELIMITERS.
474 Each call replaces TOKEN by the next token in SS, or by an
475 empty string if no tokens remain. Returns true if a token was
476 obtained, false otherwise.
478 Before the first call, initialize *SAVE_IDX to 0. Do not
479 modify *SAVE_IDX between calls.
481 SS divides into exactly one more tokens than it contains
482 delimiters. That is, a delimiter at the start or end of SS or
483 a pair of adjacent delimiters yields an empty token, and the
484 empty string contains a single token. */
486 ss_separate (struct substring ss, struct substring delimiters,
487 size_t *save_idx, struct substring *token)
489 if (*save_idx <= ss_length (ss))
491 struct substring tmp = ss_substr (ss, *save_idx, SIZE_MAX);
492 size_t length = ss_cspan (tmp, delimiters);
493 *token = ss_head (tmp, length);
494 *save_idx += length + 1;
499 *token = ss_empty ();
504 /* Divides SS into tokens separated by any of the DELIMITERS,
505 merging adjacent delimiters so that the empty string is never
506 produced as a token. Each call replaces TOKEN by the next
507 token in SS, or by an empty string if no tokens remain, and
508 then skips past the first delimiter following the token.
509 Returns true if a token was obtained, false otherwise.
511 Before the first call, initialize *SAVE_IDX to 0. Do not
512 modify *SAVE_IDX between calls. */
514 ss_tokenize (struct substring ss, struct substring delimiters,
515 size_t *save_idx, struct substring *token)
517 ss_advance (&ss, *save_idx);
518 *save_idx += ss_ltrim (&ss, delimiters);
519 ss_get_chars (&ss, ss_cspan (ss, delimiters), token);
520 *save_idx += ss_length (*token) + 1;
521 return ss_length (*token) > 0;
524 /* Removes the first CNT characters from SS. */
526 ss_advance (struct substring *ss, size_t cnt)
528 if (cnt > ss->length)
534 /* If the first character in SS is C, removes it and returns true.
535 Otherwise, returns false without changing the string. */
537 ss_match_char (struct substring *ss, char c)
539 if (ss_first (*ss) == c)
549 /* If SS begins with TARGET, removes it and returns true.
550 Otherwise, returns false without changing SS. */
552 ss_match_string (struct substring *ss, const struct substring target)
554 size_t length = ss_length (target);
555 if (ss_equals (ss_head (*ss, length), target))
557 ss_advance (ss, length);
564 /* Removes the first character from SS and returns it.
565 If SS is empty, returns EOF without modifying SS. */
567 ss_get_char (struct substring *ss)
569 int c = ss_first (*ss);
578 /* Stores the prefix of SS up to the first DELIMITER in OUT (if
579 any). Trims those same characters from SS. DELIMITER is
580 removed from SS but not made part of OUT. Returns true if
581 DELIMITER was found (and removed), false otherwise. */
583 ss_get_until (struct substring *ss, char delimiter, struct substring *out)
585 ss_get_chars (ss, ss_cspan (*ss, ss_buffer (&delimiter, 1)), out);
586 return ss_match_char (ss, delimiter);
589 /* Stores the first CNT characters in SS in OUT (or fewer, if SS
590 is shorter than CNT characters). Trims the same characters
591 from the beginning of SS. Returns CNT. */
593 ss_get_chars (struct substring *ss, size_t cnt, struct substring *out)
595 *out = ss_head (*ss, cnt);
596 ss_advance (ss, cnt);
600 /* Parses and removes an optionally signed decimal integer from
601 the beginning of SS. Returns 0 if an error occurred,
602 otherwise the number of characters removed from SS. Stores
603 the integer's value into *VALUE. */
605 ss_get_long (struct substring *ss, long *value)
610 length = ss_span (*ss, ss_cstr ("+-"));
611 length += ss_span (ss_substr (*ss, length, SIZE_MAX), ss_cstr (CC_DIGITS));
612 if (length > 0 && length < sizeof tmp)
616 memcpy (tmp, ss_data (*ss), length);
619 *value = strtol (tmp, &tail, 10);
620 if (tail - tmp == length)
622 ss_advance (ss, length);
630 /* Returns true if SS is empty (contains no characters),
633 ss_is_empty (struct substring ss)
635 return ss.length == 0;
638 /* Returns the number of characters in SS. */
640 ss_length (struct substring ss)
645 /* Returns a pointer to the characters in SS. */
647 ss_data (struct substring ss)
652 /* Returns a pointer just past the last character in SS. */
654 ss_end (struct substring ss)
656 return ss.string + ss.length;
659 /* Returns the character in position IDX in SS, as a value in the
660 range of unsigned char. Returns EOF if IDX is out of the
661 range of indexes for SS. */
663 ss_at (struct substring ss, size_t idx)
665 return idx < ss.length ? (unsigned char) ss.string[idx] : EOF;
668 /* Returns the first character in SS as a value in the range of
669 unsigned char. Returns EOF if SS is the empty string. */
671 ss_first (struct substring ss)
673 return ss_at (ss, 0);
676 /* Returns the last character in SS as a value in the range of
677 unsigned char. Returns EOF if SS is the empty string. */
679 ss_last (struct substring ss)
681 return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF;
684 /* Returns the number of contiguous characters at the beginning
685 of SS that are in SKIP_SET. */
687 ss_span (struct substring ss, struct substring skip_set)
690 for (i = 0; i < ss.length; i++)
691 if (ss_find_char (skip_set, ss.string[i]) == SIZE_MAX)
696 /* Returns the number of contiguous characters at the beginning
697 of SS that are not in SKIP_SET. */
699 ss_cspan (struct substring ss, struct substring stop_set)
702 for (i = 0; i < ss.length; i++)
703 if (ss_find_char (stop_set, ss.string[i]) != SIZE_MAX)
708 /* Returns the offset in SS of the first instance of C,
709 or SIZE_MAX if C does not occur in SS. */
711 ss_find_char (struct substring ss, char c)
713 const char *p = memchr (ss.string, c, ss.length);
714 return p != NULL ? p - ss.string : SIZE_MAX;
717 /* Compares A and B and returns a strcmp()-type comparison
720 ss_compare (struct substring a, struct substring b)
722 int retval = memcmp (a.string, b.string, MIN (a.length, b.length));
724 retval = a.length < b.length ? -1 : a.length > b.length;
728 /* Compares A and B case-insensitively and returns a
729 strcmp()-type comparison result. */
731 ss_compare_case (struct substring a, struct substring b)
733 int retval = memcasecmp (a.string, b.string, MIN (a.length, b.length));
735 retval = a.length < b.length ? -1 : a.length > b.length;
739 /* Compares A and B and returns true if their contents are
740 identical, false otherwise. */
742 ss_equals (struct substring a, struct substring b)
744 return a.length == b.length && !memcmp (a.string, b.string, a.length);
747 /* Compares A and B and returns true if their contents are
748 identical except possibly for case differences, false
751 ss_equals_case (struct substring a, struct substring b)
753 return a.length == b.length && !memcasecmp (a.string, b.string, a.length);
756 /* Returns the position in SS that the character at P occupies.
757 P must point within SS or one past its end. */
759 ss_pointer_to_position (struct substring ss, const char *p)
761 size_t pos = p - ss.string;
762 assert (pos <= ss.length);
766 /* Allocates and returns a null-terminated string that contains
769 ss_xstrdup (struct substring ss)
771 char *s = xmalloc (ss.length + 1);
772 memcpy (s, ss.string, ss.length);
777 /* Initializes ST as an empty string. */
779 ds_init_empty (struct string *st)
781 st->ss = ss_empty ();
785 /* Initializes ST with initial contents S. */
787 ds_init_string (struct string *st, const struct string *s)
789 ds_init_substring (st, ds_ss (s));
792 /* Initializes ST with initial contents SS. */
794 ds_init_substring (struct string *st, struct substring ss)
796 st->capacity = MAX (8, ss.length * 2);
797 st->ss.string = xmalloc (st->capacity + 1);
798 memcpy (st->ss.string, ss.string, ss.length);
799 st->ss.length = ss.length;
802 /* Initializes ST with initial contents S. */
804 ds_init_cstr (struct string *st, const char *s)
806 ds_init_substring (st, ss_cstr (s));
811 ds_destroy (struct string *st)
815 ss_dealloc (&st->ss);
816 st->ss.string = NULL;
822 /* Swaps the contents of strings A and B. */
824 ds_swap (struct string *a, struct string *b)
826 struct string tmp = *a;
831 /* Helper function for ds_register_pool. */
833 free_string (void *st_)
835 struct string *st = st_;
839 /* Arranges for ST to be destroyed automatically as part of
842 ds_register_pool (struct string *st, struct pool *pool)
844 pool_register (pool, free_string, st);
847 /* Cancels the arrangement for ST to be destroyed automatically
850 ds_unregister_pool (struct string *st, struct pool *pool)
852 pool_unregister (pool, st);
855 /* Copies SRC into DST.
856 DST and SRC may be the same string. */
858 ds_assign_string (struct string *dst, const struct string *src)
860 ds_assign_substring (dst, ds_ss (src));
863 /* Replaces DST by SS.
864 SS may be a substring of DST. */
866 ds_assign_substring (struct string *dst, struct substring ss)
868 dst->ss.length = ss.length;
869 ds_extend (dst, ss.length);
870 memmove (dst->ss.string, ss.string, ss.length);
873 /* Replaces DST by null-terminated string SRC. SRC may overlap
876 ds_assign_cstr (struct string *dst, const char *src)
878 ds_assign_substring (dst, ss_cstr (src));
881 /* Truncates ST to zero length. */
883 ds_clear (struct string *st)
888 /* Returns a substring that contains ST. */
890 ds_ss (const struct string *st)
895 /* Returns a substring that contains CNT characters from ST
896 starting at position START.
898 If START is greater than or equal to the length of ST, then
899 the substring will be the empty string. If START + CNT
900 exceeds the length of ST, then the substring will only be
901 ds_length(ST) - START characters long. */
903 ds_substr (const struct string *st, size_t start, size_t cnt)
905 return ss_substr (ds_ss (st), start, cnt);
908 /* Returns a substring that contains the first CNT characters in
909 ST. If CNT exceeds the length of ST, then the substring will
910 contain all of ST. */
912 ds_head (const struct string *st, size_t cnt)
914 return ss_head (ds_ss (st), cnt);
917 /* Returns a substring that contains the last CNT characters in
918 ST. If CNT exceeds the length of ST, then the substring will
919 contain all of ST. */
921 ds_tail (const struct string *st, size_t cnt)
923 return ss_tail (ds_ss (st), cnt);
926 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
929 ds_extend (struct string *st, size_t min_capacity)
931 if (min_capacity > st->capacity)
934 if (st->capacity < min_capacity)
935 st->capacity = 2 * min_capacity;
937 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
941 /* Shrink ST to the minimum capacity need to contain its content. */
943 ds_shrink (struct string *st)
945 if (st->capacity != st->ss.length)
947 st->capacity = st->ss.length;
948 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
952 /* Truncates ST to at most LENGTH characters long. */
954 ds_truncate (struct string *st, size_t length)
956 ss_truncate (&st->ss, length);
959 /* Removes trailing characters in TRIM_SET from ST.
960 Returns number of characters removed. */
962 ds_rtrim (struct string *st, struct substring trim_set)
964 return ss_rtrim (&st->ss, trim_set);
967 /* Removes leading characters in TRIM_SET from ST.
968 Returns number of characters removed. */
970 ds_ltrim (struct string *st, struct substring trim_set)
972 size_t cnt = ds_span (st, trim_set);
974 ds_assign_substring (st, ds_substr (st, cnt, SIZE_MAX));
978 /* Trims leading and trailing characters in TRIM_SET from ST.
979 Returns number of charactesr removed. */
981 ds_trim (struct string *st, struct substring trim_set)
983 size_t cnt = ds_rtrim (st, trim_set);
984 return cnt + ds_ltrim (st, trim_set);
987 /* If the last character in ST is C, removes it and returns true.
988 Otherwise, returns false without modifying ST. */
990 ds_chomp (struct string *st, char c)
992 return ss_chomp (&st->ss, c);
995 /* Divides ST into tokens separated by any of the DELIMITERS.
996 Each call replaces TOKEN by the next token in ST, or by an
997 empty string if no tokens remain. Returns true if a token was
998 obtained, false otherwise.
1000 Before the first call, initialize *SAVE_IDX to 0. Do not
1001 modify *SAVE_IDX between calls.
1003 ST divides into exactly one more tokens than it contains
1004 delimiters. That is, a delimiter at the start or end of ST or
1005 a pair of adjacent delimiters yields an empty token, and the
1006 empty string contains a single token. */
1008 ds_separate (const struct string *st, struct substring delimiters,
1009 size_t *save_idx, struct substring *token)
1011 return ss_separate (ds_ss (st), delimiters, save_idx, token);
1014 /* Divides ST into tokens separated by any of the DELIMITERS,
1015 merging adjacent delimiters so that the empty string is never
1016 produced as a token. Each call replaces TOKEN by the next
1017 token in ST, or by an empty string if no tokens remain.
1018 Returns true if a token was obtained, false otherwise.
1020 Before the first call, initialize *SAVE_IDX to 0. Do not
1021 modify *SAVE_IDX between calls. */
1023 ds_tokenize (const struct string *st, struct substring delimiters,
1024 size_t *save_idx, struct substring *token)
1026 return ss_tokenize (ds_ss (st), delimiters, save_idx, token);
1029 /* Pad ST on the right with copies of PAD until ST is at least
1030 LENGTH characters in size. If ST is initially LENGTH
1031 characters or longer, this is a no-op. */
1033 ds_rpad (struct string *st, size_t length, char pad)
1035 if (length > st->ss.length)
1036 ds_put_char_multiple (st, pad, length - st->ss.length);
1039 /* Sets the length of ST to exactly NEW_LENGTH,
1040 either by truncating characters from the end,
1041 or by padding on the right with PAD. */
1043 ds_set_length (struct string *st, size_t new_length, char pad)
1045 if (st->ss.length < new_length)
1046 ds_rpad (st, new_length, pad);
1048 st->ss.length = new_length;
1051 /* Returns true if ST is empty, false otherwise. */
1053 ds_is_empty (const struct string *st)
1055 return ss_is_empty (st->ss);
1058 /* Returns the length of ST. */
1060 ds_length (const struct string *st)
1062 return ss_length (ds_ss (st));
1065 /* Returns the string data inside ST. */
1067 ds_data (const struct string *st)
1069 return ss_data (ds_ss (st));
1072 /* Returns a pointer to the null terminator ST.
1073 This might not be an actual null character unless ds_c_str() has
1074 been called since the last modification to ST. */
1076 ds_end (const struct string *st)
1078 return ss_end (ds_ss (st));
1081 /* Returns the character in position IDX in ST, as a value in the
1082 range of unsigned char. Returns EOF if IDX is out of the
1083 range of indexes for ST. */
1085 ds_at (const struct string *st, size_t idx)
1087 return ss_at (ds_ss (st), idx);
1090 /* Returns the first character in ST as a value in the range of
1091 unsigned char. Returns EOF if ST is the empty string. */
1093 ds_first (const struct string *st)
1095 return ss_first (ds_ss (st));
1098 /* Returns the last character in ST as a value in the range of
1099 unsigned char. Returns EOF if ST is the empty string. */
1101 ds_last (const struct string *st)
1103 return ss_last (ds_ss (st));
1106 /* Returns the number of consecutive characters at the beginning
1107 of ST that are in SKIP_SET. */
1109 ds_span (const struct string *st, struct substring skip_set)
1111 return ss_span (ds_ss (st), skip_set);
1114 /* Returns the number of consecutive characters at the beginning
1115 of ST that are not in STOP_SET. */
1117 ds_cspan (const struct string *st, struct substring stop_set)
1119 return ss_cspan (ds_ss (st), stop_set);
1122 /* Returns the position of the first occurrence of character C in
1123 ST at or after position OFS, or SIZE_MAX if there is no such
1126 ds_find_char (const struct string *st, char c)
1128 return ss_find_char (ds_ss (st), c);
1131 /* Compares A and B and returns a strcmp()-type comparison
1134 ds_compare (const struct string *a, const struct string *b)
1136 return ss_compare (ds_ss (a), ds_ss (b));
1139 /* Returns the position in ST that the character at P occupies.
1140 P must point within ST or one past its end. */
1142 ds_pointer_to_position (const struct string *st, const char *p)
1144 return ss_pointer_to_position (ds_ss (st), p);
1147 /* Allocates and returns a null-terminated string that contains
1150 ds_xstrdup (const struct string *st)
1152 return ss_xstrdup (ds_ss (st));
1155 /* Returns the allocation size of ST. */
1157 ds_capacity (const struct string *st)
1159 return st->capacity;
1162 /* Returns the value of ST as a null-terminated string. */
1164 ds_cstr (const struct string *st_)
1166 struct string *st = (struct string *) st_;
1167 if (st->ss.string == NULL)
1169 st->ss.string[st->ss.length] = '\0';
1170 return st->ss.string;
1173 /* Appends to ST a newline-terminated line read from STREAM, but
1174 no more than MAX_LENGTH characters.
1175 Newline is the last character of ST on return, if encountering
1176 a newline was the reason for terminating.
1177 Returns true if at least one character was read from STREAM
1178 and appended to ST, false if no characters at all were read
1179 before an I/O error or end of file was encountered (or
1180 MAX_LENGTH was 0). */
1182 ds_read_line (struct string *st, FILE *stream, size_t max_length)
1184 if (!st->ss.length && max_length == SIZE_MAX)
1186 size_t capacity = st->capacity ? st->capacity + 1 : 0;
1187 ssize_t n = getline (&st->ss.string, &capacity, stream);
1189 st->capacity = capacity - 1;
1202 for (length = 0; length < max_length; length++)
1204 int c = getc (stream);
1208 ds_put_char (st, c);
1217 /* Removes a comment introduced by `#' from ST,
1218 ignoring occurrences inside quoted strings. */
1220 remove_comment (struct string *st)
1225 for (cp = ds_data (st); cp < ds_end (st); cp++)
1230 else if (*cp == '\\')
1233 else if (*cp == '\'' || *cp == '"')
1235 else if (*cp == '#')
1237 ds_truncate (st, cp - ds_cstr (st));
1242 /* Reads a line from STREAM into ST, then preprocesses as follows:
1244 - Splices lines terminated with `\'.
1246 - Deletes comments introduced by `#' outside of single or double
1249 - Deletes trailing white space.
1251 Returns true if a line was successfully read, false on
1252 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
1253 incremented by the number of lines read. */
1255 ds_read_config_line (struct string *st, int *line_number, FILE *stream)
1260 if (!ds_read_line (st, stream, SIZE_MAX))
1263 ds_rtrim (st, ss_cstr (CC_SPACES));
1265 while (ds_chomp (st, '\\'));
1267 remove_comment (st);
1271 /* Attempts to read SIZE * CNT bytes from STREAM and append them
1273 Returns true if all the requested data was read, false otherwise. */
1275 ds_read_stream (struct string *st, size_t size, size_t cnt, FILE *stream)
1279 size_t try_bytes = xtimes (cnt, size);
1280 if (size_in_bounds_p (xsum (ds_length (st), try_bytes)))
1282 char *buffer = ds_put_uninit (st, try_bytes);
1283 size_t got_bytes = fread (buffer, 1, try_bytes, stream);
1284 ds_truncate (st, ds_length (st) - (try_bytes - got_bytes));
1285 return got_bytes == try_bytes;
1297 /* Concatenates S onto ST. */
1299 ds_put_cstr (struct string *st, const char *s)
1302 ds_put_substring (st, ss_cstr (s));
1305 /* Concatenates SS to ST. */
1307 ds_put_substring (struct string *st, struct substring ss)
1309 memcpy (ds_put_uninit (st, ss_length (ss)), ss_data (ss), ss_length (ss));
1312 /* Returns ds_end(ST) and THEN increases the length by INCR. */
1314 ds_put_uninit (struct string *st, size_t incr)
1317 ds_extend (st, ds_length (st) + incr);
1319 st->ss.length += incr;
1323 /* Formats FORMAT as a printf string and appends the result to ST. */
1325 ds_put_format (struct string *st, const char *format, ...)
1329 va_start (args, format);
1330 ds_put_vformat (st, format, args);
1334 /* Formats FORMAT as a printf string and appends the result to ST. */
1336 ds_put_vformat (struct string *st, const char *format, va_list args_)
1341 va_copy (args, args_);
1342 avail = st->ss.string != NULL ? st->capacity - st->ss.length + 1 : 0;
1343 needed = vsnprintf (st->ss.string + st->ss.length, avail, format, args);
1346 if (needed >= avail)
1348 va_copy (args, args_);
1349 vsprintf (ds_put_uninit (st, needed), format, args);
1354 /* Some old libc's returned -1 when the destination string
1356 while (needed == -1)
1358 ds_extend (st, (st->capacity + 1) * 2);
1359 avail = st->capacity - st->ss.length + 1;
1361 va_copy (args, args_);
1362 needed = vsnprintf (ds_end (st), avail, format, args);
1365 st->ss.length += needed;
1369 /* Appends character CH to ST. */
1371 ds_put_char (struct string *st, int ch)
1373 ds_put_uninit (st, 1)[0] = ch;
1376 /* Appends CNT copies of character CH to ST. */
1378 ds_put_char_multiple (struct string *st, int ch, size_t cnt)
1380 memset (ds_put_uninit (st, cnt), ch, cnt);