1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010 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/cast.h>
27 #include <libpspp/message.h>
28 #include <libpspp/pool.h>
30 #include <relocatable.h>
36 /* Reverses the order of NBYTES bytes at address P, thus converting
37 between little- and big-endian byte orders. */
39 buf_reverse (char *p, size_t nbytes)
41 char *h = p, *t = &h[nbytes - 1];
53 /* Compares the SIZE bytes in A to those in B, disregarding case,
54 and returns a strcmp()-type result. */
56 buf_compare_case (const char *a_, const char *b_, size_t size)
58 const unsigned char *a = (unsigned char *) a_;
59 const unsigned char *b = (unsigned char *) b_;
63 unsigned char ac = toupper (*a++);
64 unsigned char bc = toupper (*b++);
67 return ac > bc ? 1 : -1;
73 /* Compares A of length A_LEN to B of length B_LEN. The shorter
74 string is considered to be padded with spaces to the length of
77 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
82 min_len = a_len < b_len ? a_len : b_len;
83 result = memcmp (a, b, min_len);
92 for (idx = min_len; idx < b_len; idx++)
94 return ' ' > b[idx] ? 1 : -1;
98 for (idx = min_len; idx < a_len; idx++)
100 return a[idx] > ' ' ? 1 : -1;
106 /* Compares strin A to string B. The shorter string is
107 considered to be padded with spaces to the length of the
110 str_compare_rpad (const char *a, const char *b)
112 return buf_compare_rpad (a, strlen (a), b, strlen (b));
115 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
116 DST is truncated to DST_SIZE bytes or padded on the right with
117 copies of PAD as needed. */
119 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src, char pad)
121 size_t src_len = strlen (src);
122 if (src_len >= dst_size)
123 memcpy (dst, src, dst_size);
126 memcpy (dst, src, src_len);
127 memset (&dst[src_len], pad, dst_size - src_len);
131 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
132 DST is truncated to DST_SIZE bytes or padded on the left with
133 copies of PAD as needed. */
135 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src, char pad)
137 size_t src_len = strlen (src);
138 if (src_len >= dst_size)
139 memcpy (dst, src, dst_size);
142 size_t pad_cnt = dst_size - src_len;
143 memset (&dst[0], pad, pad_cnt);
144 memcpy (dst + pad_cnt, src, src_len);
148 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
149 DST is truncated to DST_SIZE bytes or padded on the left with
150 copies of PAD as needed. */
152 buf_copy_lpad (char *dst, size_t dst_size,
153 const char *src, size_t src_size,
156 if (src_size >= dst_size)
157 memmove (dst, src, dst_size);
160 memset (dst, pad, dst_size - src_size);
161 memmove (&dst[dst_size - src_size], src, src_size);
165 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
166 DST is truncated to DST_SIZE bytes or padded on the right with
167 copies of PAD as needed. */
169 buf_copy_rpad (char *dst, size_t dst_size,
170 const char *src, size_t src_size,
173 if (src_size >= dst_size)
174 memmove (dst, src, dst_size);
177 memmove (dst, src, src_size);
178 memset (&dst[src_size], pad, dst_size - src_size);
182 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
184 Truncates DST to DST_SIZE - 1 characters or right-pads with
185 spaces to DST_SIZE - 1 characters if necessary. */
187 str_copy_rpad (char *dst, size_t dst_size, const char *src)
191 size_t src_len = strlen (src);
192 if (src_len < dst_size - 1)
194 memcpy (dst, src, src_len);
195 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
198 memcpy (dst, src, dst_size - 1);
199 dst[dst_size - 1] = 0;
203 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
204 Truncates DST to DST_SIZE - 1 characters, if necessary. */
206 str_copy_trunc (char *dst, size_t dst_size, const char *src)
208 size_t src_len = strlen (src);
209 assert (dst_size > 0);
210 if (src_len + 1 < dst_size)
211 memcpy (dst, src, src_len + 1);
214 memcpy (dst, src, dst_size - 1);
215 dst[dst_size - 1] = '\0';
219 /* Copies buffer SRC, of SRC_LEN bytes,
220 to DST, which is in a buffer DST_SIZE bytes long.
221 Truncates DST to DST_SIZE - 1 characters, if necessary. */
223 str_copy_buf_trunc (char *dst, size_t dst_size,
224 const char *src, size_t src_size)
227 assert (dst_size > 0);
229 dst_len = src_size < dst_size ? src_size : dst_size - 1;
230 memcpy (dst, src, dst_len);
234 /* Converts each character in S to uppercase. */
236 str_uppercase (char *s)
238 for (; *s != '\0'; s++)
239 *s = toupper ((unsigned char) *s);
242 /* Converts each character in S to lowercase. */
244 str_lowercase (char *s)
246 for (; *s != '\0'; s++)
247 *s = tolower ((unsigned char) *s);
250 /* Converts NUMBER into a string in 26-adic notation in BUFFER,
251 which has room for SIZE bytes. Returns true if successful,
252 false if NUMBER, plus a trailing null, is too large to fit in
255 26-adic notation is "spreadsheet column numbering": 1 = A, 2 =
256 B, 3 = C, ... 26 = Z, 27 = AA, 28 = AB, 29 = AC, ...
258 26-adic notation is the special case of a k-adic numeration
259 system (aka bijective base-k numeration) with k=26. In k-adic
260 numeration, the digits are {1, 2, 3, ..., k} (there is no
261 digit 0), and integer 0 is represented by the empty string.
262 For more information, see
263 http://en.wikipedia.org/wiki/Bijective_numeration. */
265 str_format_26adic (unsigned long int number, char buffer[], size_t size)
273 buffer[length++] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[number % 26];
279 buffer[length] = '\0';
281 buf_reverse (buffer, length);
285 /* Sets the SIZE bytes starting at BLOCK to C,
286 and returns the byte following BLOCK. */
288 mempset (void *block, int c, size_t size)
290 memset (block, c, size);
291 return (char *) block + size;
296 /* Returns a substring whose contents are the CNT characters
297 starting at the (0-based) position START in SS. */
299 ss_substr (struct substring ss, size_t start, size_t cnt)
301 if (start < ss.length)
302 return ss_buffer (ss.string + start, MIN (cnt, ss.length - start));
304 return ss_buffer (ss.string + ss.length, 0);
307 /* Returns a substring whose contents are the first CNT
310 ss_head (struct substring ss, size_t cnt)
312 return ss_buffer (ss.string, MIN (cnt, ss.length));
315 /* Returns a substring whose contents are the last CNT characters
318 ss_tail (struct substring ss, size_t cnt)
321 return ss_buffer (ss.string + (ss.length - cnt), cnt);
326 /* Makes a malloc()'d, null-terminated copy of the contents of OLD
327 and stores it in NEW. */
329 ss_alloc_substring (struct substring *new, struct substring old)
331 new->string = xmemdup0 (old.string, old.length);
332 new->length = old.length;
335 /* Allocates room for a CNT-character string in NEW. */
337 ss_alloc_uninit (struct substring *new, size_t cnt)
339 new->string = xmalloc (cnt);
343 /* Makes a pool_alloc_unaligned()'d copy of the contents of OLD
344 in POOL, and stores it in NEW. */
346 ss_alloc_substring_pool (struct substring *new, struct substring old,
349 new->string = pool_alloc_unaligned (pool, old.length);
350 new->length = old.length;
351 memcpy (new->string, old.string, old.length);
354 /* Allocates room for a CNT-character string in NEW in POOL. */
356 ss_alloc_uninit_pool (struct substring *new, size_t cnt, struct pool *pool)
358 new->string = pool_alloc_unaligned (pool, cnt);
362 /* Frees the string that SS points to. */
364 ss_dealloc (struct substring *ss)
369 /* Truncates SS to at most CNT characters in length. */
371 ss_truncate (struct substring *ss, size_t cnt)
373 if (ss->length > cnt)
377 /* Removes trailing characters in TRIM_SET from SS.
378 Returns number of characters removed. */
380 ss_rtrim (struct substring *ss, struct substring trim_set)
383 while (cnt < ss->length
384 && ss_find_char (trim_set,
385 ss->string[ss->length - cnt - 1]) != SIZE_MAX)
391 /* Removes leading characters in TRIM_SET from SS.
392 Returns number of characters removed. */
394 ss_ltrim (struct substring *ss, struct substring trim_set)
396 size_t cnt = ss_span (*ss, trim_set);
397 ss_advance (ss, cnt);
401 /* Trims leading and trailing characters in TRIM_SET from SS. */
403 ss_trim (struct substring *ss, struct substring trim_set)
405 ss_ltrim (ss, trim_set);
406 ss_rtrim (ss, trim_set);
409 /* If the last character in SS is C, removes it and returns true.
410 Otherwise, returns false without changing the string. */
412 ss_chomp (struct substring *ss, char c)
414 if (ss_last (*ss) == c)
423 /* Divides SS into tokens separated by any of the DELIMITERS.
424 Each call replaces TOKEN by the next token in SS, or by an
425 empty string if no tokens remain. Returns true if a token was
426 obtained, false otherwise.
428 Before the first call, initialize *SAVE_IDX to 0. Do not
429 modify *SAVE_IDX between calls.
431 SS divides into exactly one more tokens than it contains
432 delimiters. That is, a delimiter at the start or end of SS or
433 a pair of adjacent delimiters yields an empty token, and the
434 empty string contains a single token. */
436 ss_separate (struct substring ss, struct substring delimiters,
437 size_t *save_idx, struct substring *token)
439 if (*save_idx <= ss_length (ss))
441 struct substring tmp = ss_substr (ss, *save_idx, SIZE_MAX);
442 size_t length = ss_cspan (tmp, delimiters);
443 *token = ss_head (tmp, length);
444 *save_idx += length + 1;
449 *token = ss_empty ();
454 /* Divides SS into tokens separated by any of the DELIMITERS,
455 merging adjacent delimiters so that the empty string is never
456 produced as a token. Each call replaces TOKEN by the next
457 token in SS, or by an empty string if no tokens remain, and
458 then skips past the first delimiter following the token.
459 Returns true if a token was obtained, false otherwise.
461 Before the first call, initialize *SAVE_IDX to 0. Do not
462 modify *SAVE_IDX between calls. */
464 ss_tokenize (struct substring ss, struct substring delimiters,
465 size_t *save_idx, struct substring *token)
467 ss_advance (&ss, *save_idx);
468 *save_idx += ss_ltrim (&ss, delimiters);
469 ss_get_chars (&ss, ss_cspan (ss, delimiters), token);
470 *save_idx += ss_length (*token) + 1;
471 return ss_length (*token) > 0;
474 /* Removes the first CNT characters from SS. */
476 ss_advance (struct substring *ss, size_t cnt)
478 if (cnt > ss->length)
484 /* If the first character in SS is C, removes it and returns true.
485 Otherwise, returns false without changing the string. */
487 ss_match_char (struct substring *ss, char c)
489 if (ss_first (*ss) == c)
499 /* If the first character in SS is in MATCH, removes it and
500 returns the character that was removed.
501 Otherwise, returns EOF without changing the string. */
503 ss_match_char_in (struct substring *ss, struct substring match)
507 && memchr (match.string, ss->string[0], match.length) != NULL)
516 /* If SS begins with TARGET, removes it and returns true.
517 Otherwise, returns false without changing SS. */
519 ss_match_string (struct substring *ss, const struct substring target)
521 size_t length = ss_length (target);
522 if (ss_equals (ss_head (*ss, length), target))
524 ss_advance (ss, length);
531 /* Removes the first character from SS and returns it.
532 If SS is empty, returns EOF without modifying SS. */
534 ss_get_char (struct substring *ss)
536 int c = ss_first (*ss);
545 /* Stores the prefix of SS up to the first DELIMITER in OUT (if
546 any). Trims those same characters from SS. DELIMITER is
547 removed from SS but not made part of OUT. Returns true if
548 DELIMITER was found (and removed), false otherwise. */
550 ss_get_until (struct substring *ss, char delimiter, struct substring *out)
552 ss_get_chars (ss, ss_cspan (*ss, ss_buffer (&delimiter, 1)), out);
553 return ss_match_char (ss, delimiter);
556 /* Stores the first CNT characters in SS in OUT (or fewer, if SS
557 is shorter than CNT characters). Trims the same characters
558 from the beginning of SS. Returns CNT. */
560 ss_get_chars (struct substring *ss, size_t cnt, struct substring *out)
562 *out = ss_head (*ss, cnt);
563 ss_advance (ss, cnt);
567 /* Parses and removes an optionally signed decimal integer from
568 the beginning of SS. Returns 0 if an error occurred,
569 otherwise the number of characters removed from SS. Stores
570 the integer's value into *VALUE. */
572 ss_get_long (struct substring *ss, long *value)
577 length = ss_span (*ss, ss_cstr ("+-"));
578 length += ss_span (ss_substr (*ss, length, SIZE_MAX), ss_cstr (CC_DIGITS));
579 if (length > 0 && length < sizeof tmp)
583 memcpy (tmp, ss_data (*ss), length);
586 *value = strtol (tmp, &tail, 10);
587 if (tail - tmp == length)
589 ss_advance (ss, length);
597 /* Returns true if SS is empty (contains no characters),
600 ss_is_empty (struct substring ss)
602 return ss.length == 0;
605 /* Returns the number of characters in SS. */
607 ss_length (struct substring ss)
612 /* Returns a pointer to the characters in SS. */
614 ss_data (struct substring ss)
619 /* Returns a pointer just past the last character in SS. */
621 ss_end (struct substring ss)
623 return ss.string + ss.length;
626 /* Returns the character in position IDX in SS, as a value in the
627 range of unsigned char. Returns EOF if IDX is out of the
628 range of indexes for SS. */
630 ss_at (struct substring ss, size_t idx)
632 return idx < ss.length ? (unsigned char) ss.string[idx] : EOF;
635 /* Returns the first character in SS as a value in the range of
636 unsigned char. Returns EOF if SS is the empty string. */
638 ss_first (struct substring ss)
640 return ss_at (ss, 0);
643 /* Returns the last character in SS as a value in the range of
644 unsigned char. Returns EOF if SS is the empty string. */
646 ss_last (struct substring ss)
648 return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF;
651 /* Returns the number of contiguous characters at the beginning
652 of SS that are in SKIP_SET. */
654 ss_span (struct substring ss, struct substring skip_set)
657 for (i = 0; i < ss.length; i++)
658 if (ss_find_char (skip_set, ss.string[i]) == SIZE_MAX)
663 /* Returns the number of contiguous characters at the beginning
664 of SS that are not in SKIP_SET. */
666 ss_cspan (struct substring ss, struct substring stop_set)
669 for (i = 0; i < ss.length; i++)
670 if (ss_find_char (stop_set, ss.string[i]) != SIZE_MAX)
675 /* Returns the offset in SS of the first instance of C,
676 or SIZE_MAX if C does not occur in SS. */
678 ss_find_char (struct substring ss, char c)
680 const char *p = memchr (ss.string, c, ss.length);
681 return p != NULL ? p - ss.string : SIZE_MAX;
684 /* Compares A and B and returns a strcmp()-type comparison
687 ss_compare (struct substring a, struct substring b)
689 int retval = memcmp (a.string, b.string, MIN (a.length, b.length));
691 retval = a.length < b.length ? -1 : a.length > b.length;
695 /* Compares A and B case-insensitively and returns a
696 strcmp()-type comparison result. */
698 ss_compare_case (struct substring a, struct substring b)
700 int retval = memcasecmp (a.string, b.string, MIN (a.length, b.length));
702 retval = a.length < b.length ? -1 : a.length > b.length;
706 /* Compares A and B and returns true if their contents are
707 identical, false otherwise. */
709 ss_equals (struct substring a, struct substring b)
711 return a.length == b.length && !memcmp (a.string, b.string, a.length);
714 /* Compares A and B and returns true if their contents are
715 identical except possibly for case differences, false
718 ss_equals_case (struct substring a, struct substring b)
720 return a.length == b.length && !memcasecmp (a.string, b.string, a.length);
723 /* Returns the position in SS that the character at P occupies.
724 P must point within SS or one past its end. */
726 ss_pointer_to_position (struct substring ss, const char *p)
728 size_t pos = p - ss.string;
729 assert (pos <= ss.length);
733 /* Allocates and returns a null-terminated string that contains
736 ss_xstrdup (struct substring ss)
738 char *s = xmalloc (ss.length + 1);
739 memcpy (s, ss.string, ss.length);
744 /* Initializes ST as an empty string. */
746 ds_init_empty (struct string *st)
748 st->ss = ss_empty ();
752 /* Initializes ST with initial contents S. */
754 ds_init_string (struct string *st, const struct string *s)
756 ds_init_substring (st, ds_ss (s));
759 /* Initializes ST with initial contents SS. */
761 ds_init_substring (struct string *st, struct substring ss)
763 st->capacity = MAX (8, ss.length * 2);
764 st->ss.string = xmalloc (st->capacity + 1);
765 memcpy (st->ss.string, ss.string, ss.length);
766 st->ss.length = ss.length;
769 /* Initializes ST with initial contents S. */
771 ds_init_cstr (struct string *st, const char *s)
773 ds_init_substring (st, ss_cstr (s));
778 ds_destroy (struct string *st)
782 ss_dealloc (&st->ss);
783 st->ss.string = NULL;
789 /* Swaps the contents of strings A and B. */
791 ds_swap (struct string *a, struct string *b)
793 struct string tmp = *a;
798 /* Helper function for ds_register_pool. */
800 free_string (void *st_)
802 struct string *st = st_;
806 /* Arranges for ST to be destroyed automatically as part of
809 ds_register_pool (struct string *st, struct pool *pool)
811 pool_register (pool, free_string, st);
814 /* Cancels the arrangement for ST to be destroyed automatically
817 ds_unregister_pool (struct string *st, struct pool *pool)
819 pool_unregister (pool, st);
822 /* Copies SRC into DST.
823 DST and SRC may be the same string. */
825 ds_assign_string (struct string *dst, const struct string *src)
827 ds_assign_substring (dst, ds_ss (src));
830 /* Replaces DST by SS.
831 SS may be a substring of DST. */
833 ds_assign_substring (struct string *dst, struct substring ss)
835 dst->ss.length = ss.length;
836 ds_extend (dst, ss.length);
837 memmove (dst->ss.string, ss.string, ss.length);
840 /* Replaces DST by null-terminated string SRC. SRC may overlap
843 ds_assign_cstr (struct string *dst, const char *src)
845 ds_assign_substring (dst, ss_cstr (src));
848 /* Truncates ST to zero length. */
850 ds_clear (struct string *st)
855 /* Returns a substring that contains ST. */
857 ds_ss (const struct string *st)
862 /* Returns a substring that contains CNT characters from ST
863 starting at position START.
865 If START is greater than or equal to the length of ST, then
866 the substring will be the empty string. If START + CNT
867 exceeds the length of ST, then the substring will only be
868 ds_length(ST) - START characters long. */
870 ds_substr (const struct string *st, size_t start, size_t cnt)
872 return ss_substr (ds_ss (st), start, cnt);
875 /* Returns a substring that contains the first CNT characters in
876 ST. If CNT exceeds the length of ST, then the substring will
877 contain all of ST. */
879 ds_head (const struct string *st, size_t cnt)
881 return ss_head (ds_ss (st), cnt);
884 /* Returns a substring that contains the last CNT characters in
885 ST. If CNT exceeds the length of ST, then the substring will
886 contain all of ST. */
888 ds_tail (const struct string *st, size_t cnt)
890 return ss_tail (ds_ss (st), cnt);
893 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
896 ds_extend (struct string *st, size_t min_capacity)
898 if (min_capacity > st->capacity)
901 if (st->capacity < min_capacity)
902 st->capacity = 2 * min_capacity;
904 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
908 /* Shrink ST to the minimum capacity need to contain its content. */
910 ds_shrink (struct string *st)
912 if (st->capacity != st->ss.length)
914 st->capacity = st->ss.length;
915 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
919 /* Truncates ST to at most LENGTH characters long. */
921 ds_truncate (struct string *st, size_t length)
923 ss_truncate (&st->ss, length);
926 /* Removes trailing characters in TRIM_SET from ST.
927 Returns number of characters removed. */
929 ds_rtrim (struct string *st, struct substring trim_set)
931 return ss_rtrim (&st->ss, trim_set);
934 /* Removes leading characters in TRIM_SET from ST.
935 Returns number of characters removed. */
937 ds_ltrim (struct string *st, struct substring trim_set)
939 size_t cnt = ds_span (st, trim_set);
941 ds_assign_substring (st, ds_substr (st, cnt, SIZE_MAX));
945 /* Trims leading and trailing characters in TRIM_SET from ST.
946 Returns number of charactesr removed. */
948 ds_trim (struct string *st, struct substring trim_set)
950 size_t cnt = ds_rtrim (st, trim_set);
951 return cnt + ds_ltrim (st, trim_set);
954 /* If the last character in ST is C, removes it and returns true.
955 Otherwise, returns false without modifying ST. */
957 ds_chomp (struct string *st, char c)
959 return ss_chomp (&st->ss, c);
962 /* Divides ST into tokens separated by any of the DELIMITERS.
963 Each call replaces TOKEN by the next token in ST, or by an
964 empty string if no tokens remain. Returns true if a token was
965 obtained, false otherwise.
967 Before the first call, initialize *SAVE_IDX to 0. Do not
968 modify *SAVE_IDX between calls.
970 ST divides into exactly one more tokens than it contains
971 delimiters. That is, a delimiter at the start or end of ST or
972 a pair of adjacent delimiters yields an empty token, and the
973 empty string contains a single token. */
975 ds_separate (const struct string *st, struct substring delimiters,
976 size_t *save_idx, struct substring *token)
978 return ss_separate (ds_ss (st), delimiters, save_idx, token);
981 /* Divides ST into tokens separated by any of the DELIMITERS,
982 merging adjacent delimiters so that the empty string is never
983 produced as a token. Each call replaces TOKEN by the next
984 token in ST, or by an empty string if no tokens remain.
985 Returns true if a token was obtained, false otherwise.
987 Before the first call, initialize *SAVE_IDX to 0. Do not
988 modify *SAVE_IDX between calls. */
990 ds_tokenize (const struct string *st, struct substring delimiters,
991 size_t *save_idx, struct substring *token)
993 return ss_tokenize (ds_ss (st), delimiters, save_idx, token);
996 /* Pad ST on the right with copies of PAD until ST is at least
997 LENGTH characters in size. If ST is initially LENGTH
998 characters or longer, this is a no-op. */
1000 ds_rpad (struct string *st, size_t length, char pad)
1002 if (length > st->ss.length)
1003 ds_put_char_multiple (st, pad, length - st->ss.length);
1006 /* Sets the length of ST to exactly NEW_LENGTH,
1007 either by truncating characters from the end,
1008 or by padding on the right with PAD. */
1010 ds_set_length (struct string *st, size_t new_length, char pad)
1012 if (st->ss.length < new_length)
1013 ds_rpad (st, new_length, pad);
1015 st->ss.length = new_length;
1018 /* Removes N characters from ST starting at offset START. */
1020 ds_remove (struct string *st, size_t start, size_t n)
1022 if (n > 0 && start < st->ss.length)
1024 if (st->ss.length - start <= n)
1026 /* All characters at or beyond START are deleted. */
1027 st->ss.length = start;
1031 /* Some characters remain and must be shifted into
1033 memmove (st->ss.string + st->ss.length,
1034 st->ss.string + st->ss.length + n,
1035 st->ss.length - start - n);
1041 /* There are no characters to delete or no characters at or
1042 beyond START, hence deletion is a no-op. */
1046 /* Returns true if ST is empty, false otherwise. */
1048 ds_is_empty (const struct string *st)
1050 return ss_is_empty (st->ss);
1053 /* Returns the length of ST. */
1055 ds_length (const struct string *st)
1057 return ss_length (ds_ss (st));
1060 /* Returns the string data inside ST. */
1062 ds_data (const struct string *st)
1064 return ss_data (ds_ss (st));
1067 /* Returns a pointer to the null terminator ST.
1068 This might not be an actual null character unless ds_c_str() has
1069 been called since the last modification to ST. */
1071 ds_end (const struct string *st)
1073 return ss_end (ds_ss (st));
1076 /* Returns the character in position IDX in ST, as a value in the
1077 range of unsigned char. Returns EOF if IDX is out of the
1078 range of indexes for ST. */
1080 ds_at (const struct string *st, size_t idx)
1082 return ss_at (ds_ss (st), idx);
1085 /* Returns the first character in ST as a value in the range of
1086 unsigned char. Returns EOF if ST is the empty string. */
1088 ds_first (const struct string *st)
1090 return ss_first (ds_ss (st));
1093 /* Returns the last character in ST as a value in the range of
1094 unsigned char. Returns EOF if ST is the empty string. */
1096 ds_last (const struct string *st)
1098 return ss_last (ds_ss (st));
1101 /* Returns the number of consecutive characters at the beginning
1102 of ST that are in SKIP_SET. */
1104 ds_span (const struct string *st, struct substring skip_set)
1106 return ss_span (ds_ss (st), skip_set);
1109 /* Returns the number of consecutive characters at the beginning
1110 of ST that are not in STOP_SET. */
1112 ds_cspan (const struct string *st, struct substring stop_set)
1114 return ss_cspan (ds_ss (st), stop_set);
1117 /* Returns the position of the first occurrence of character C in
1118 ST at or after position OFS, or SIZE_MAX if there is no such
1121 ds_find_char (const struct string *st, char c)
1123 return ss_find_char (ds_ss (st), c);
1126 /* Compares A and B and returns a strcmp()-type comparison
1129 ds_compare (const struct string *a, const struct string *b)
1131 return ss_compare (ds_ss (a), ds_ss (b));
1134 /* Returns the position in ST that the character at P occupies.
1135 P must point within ST or one past its end. */
1137 ds_pointer_to_position (const struct string *st, const char *p)
1139 return ss_pointer_to_position (ds_ss (st), p);
1142 /* Allocates and returns a null-terminated string that contains
1145 ds_xstrdup (const struct string *st)
1147 return ss_xstrdup (ds_ss (st));
1150 /* Returns the allocation size of ST. */
1152 ds_capacity (const struct string *st)
1154 return st->capacity;
1157 /* Returns the value of ST as a null-terminated string. */
1159 ds_cstr (const struct string *st_)
1161 struct string *st = CONST_CAST (struct string *, st_);
1162 if (st->ss.string == NULL)
1164 st->ss.string[st->ss.length] = '\0';
1165 return st->ss.string;
1168 /* Returns the value of ST as a null-terminated string and then
1169 reinitialized ST as an empty string. The caller must free the
1170 returned string with free(). */
1172 ds_steal_cstr (struct string *st)
1174 char *s = ds_cstr (st);
1179 /* Reads characters from STREAM and appends them to ST, stopping
1180 after MAX_LENGTH characters, after appending a newline, or
1181 after an I/O error or end of file was encountered, whichever
1182 comes first. Returns true if at least one character was added
1183 to ST, false if no characters were read before an I/O error or
1184 end of file (or if MAX_LENGTH was 0).
1186 This function treats LF and CR LF sequences as new-line,
1187 translating each of them to a single '\n' new-line character
1190 ds_read_line (struct string *st, FILE *stream, size_t max_length)
1194 for (length = 0; length < max_length; length++)
1196 int c = getc (stream);
1203 ds_put_char (st, c);
1210 /* CR followed by LF is special: translate to \n. */
1211 ds_put_char (st, '\n');
1216 /* CR followed by anything else is just CR. */
1217 ds_put_char (st, '\r');
1225 ds_put_char (st, c);
1232 /* Removes a comment introduced by `#' from ST,
1233 ignoring occurrences inside quoted strings. */
1235 remove_comment (struct string *st)
1240 for (cp = ds_data (st); cp < ds_end (st); cp++)
1245 else if (*cp == '\\')
1248 else if (*cp == '\'' || *cp == '"')
1250 else if (*cp == '#')
1252 ds_truncate (st, cp - ds_cstr (st));
1257 /* Reads a line from STREAM into ST, then preprocesses as follows:
1259 - Splices lines terminated with `\'.
1261 - Deletes comments introduced by `#' outside of single or double
1264 - Deletes trailing white space.
1266 Returns true if a line was successfully read, false on
1267 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
1268 incremented by the number of lines read. */
1270 ds_read_config_line (struct string *st, int *line_number, FILE *stream)
1275 if (!ds_read_line (st, stream, SIZE_MAX))
1278 ds_rtrim (st, ss_cstr (CC_SPACES));
1280 while (ds_chomp (st, '\\'));
1282 remove_comment (st);
1286 /* Attempts to read SIZE * CNT bytes from STREAM and append them
1288 Returns true if all the requested data was read, false otherwise. */
1290 ds_read_stream (struct string *st, size_t size, size_t cnt, FILE *stream)
1294 size_t try_bytes = xtimes (cnt, size);
1295 if (size_in_bounds_p (xsum (ds_length (st), try_bytes)))
1297 char *buffer = ds_put_uninit (st, try_bytes);
1298 size_t got_bytes = fread (buffer, 1, try_bytes, stream);
1299 ds_truncate (st, ds_length (st) - (try_bytes - got_bytes));
1300 return got_bytes == try_bytes;
1312 /* Concatenates S onto ST. */
1314 ds_put_cstr (struct string *st, const char *s)
1317 ds_put_substring (st, ss_cstr (s));
1320 /* Concatenates SS to ST. */
1322 ds_put_substring (struct string *st, struct substring ss)
1324 memcpy (ds_put_uninit (st, ss_length (ss)), ss_data (ss), ss_length (ss));
1327 /* Returns ds_end(ST) and THEN increases the length by INCR. */
1329 ds_put_uninit (struct string *st, size_t incr)
1332 ds_extend (st, ds_length (st) + incr);
1334 st->ss.length += incr;
1338 /* Formats FORMAT as a printf string and appends the result to ST. */
1340 ds_put_format (struct string *st, const char *format, ...)
1344 va_start (args, format);
1345 ds_put_vformat (st, format, args);
1349 /* Formats FORMAT as a printf string and appends the result to ST. */
1351 ds_put_vformat (struct string *st, const char *format, va_list args_)
1356 va_copy (args, args_);
1357 avail = st->ss.string != NULL ? st->capacity - st->ss.length + 1 : 0;
1358 needed = vsnprintf (st->ss.string + st->ss.length, avail, format, args);
1361 if (needed >= avail)
1363 va_copy (args, args_);
1364 vsprintf (ds_put_uninit (st, needed), format, args);
1369 /* Some old libc's returned -1 when the destination string
1371 while (needed == -1)
1373 ds_extend (st, (st->capacity + 1) * 2);
1374 avail = st->capacity - st->ss.length + 1;
1376 va_copy (args, args_);
1377 needed = vsnprintf (ds_end (st), avail, format, args);
1380 st->ss.length += needed;
1384 /* Appends character CH to ST. */
1386 ds_put_char (struct string *st, int ch)
1388 ds_put_uninit (st, 1)[0] = ch;
1391 /* Appends CNT copies of character CH to ST. */
1393 ds_put_char_multiple (struct string *st, int ch, size_t cnt)
1395 memset (ds_put_uninit (st, cnt), ch, cnt);
1399 /* If relocation has been enabled, replace ST,
1400 with its relocated version */
1402 ds_relocate (struct string *st)
1404 const char *orig = ds_cstr (st);
1405 const char *rel = relocate (orig);
1410 ds_put_cstr (st, rel);
1411 /* The documentation for relocate says that casting away const
1412 and then freeing is appropriate ... */
1413 free (CONST_CAST (char *, rel));
1420 /* Operations on uint8_t "strings" */
1422 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
1423 DST is truncated to DST_SIZE bytes or padded on the right with
1424 copies of PAD as needed. */
1426 u8_buf_copy_rpad (uint8_t *dst, size_t dst_size,
1427 const uint8_t *src, size_t src_size,
1430 if (src_size >= dst_size)
1431 memmove (dst, src, dst_size);
1434 memmove (dst, src, src_size);
1435 memset (&dst[src_size], pad, dst_size - src_size);