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 an empty substring. */
306 /* Returns a substring whose contents are the given C-style
309 ss_cstr (const char *cstr)
311 return ss_buffer (cstr, strlen (cstr));
314 /* Returns a substring whose contents are the CNT characters in
317 ss_buffer (const char *buffer, size_t cnt)
320 ss.string = (char *) buffer;
325 /* Returns a substring whose contents are the CNT characters
326 starting at the (0-based) position START in SS. */
328 ss_substr (struct substring ss, size_t start, size_t cnt)
330 if (start < ss.length)
331 return ss_buffer (ss.string + start, MIN (cnt, ss.length - start));
333 return ss_buffer (ss.string + ss.length, 0);
336 /* Returns a substring whose contents are the first CNT
339 ss_head (struct substring ss, size_t cnt)
341 return ss_buffer (ss.string, MIN (cnt, ss.length));
344 /* Returns a substring whose contents are the last CNT characters
347 ss_tail (struct substring ss, size_t cnt)
350 return ss_buffer (ss.string + (ss.length - cnt), cnt);
355 /* Makes a malloc()'d, null-terminated copy of the contents of OLD
356 and stores it in NEW. */
358 ss_alloc_substring (struct substring *new, struct substring old)
360 new->string = xmemdup0 (old.string, old.length);
361 new->length = old.length;
364 /* Allocates room for a CNT-character string in NEW. */
366 ss_alloc_uninit (struct substring *new, size_t cnt)
368 new->string = xmalloc (cnt);
372 /* Makes a pool_alloc_unaligned()'d copy of the contents of OLD
373 in POOL, and stores it in NEW. */
375 ss_alloc_substring_pool (struct substring *new, struct substring old,
378 new->string = pool_alloc_unaligned (pool, old.length);
379 new->length = old.length;
380 memcpy (new->string, old.string, old.length);
383 /* Allocates room for a CNT-character string in NEW in POOL. */
385 ss_alloc_uninit_pool (struct substring *new, size_t cnt, struct pool *pool)
387 new->string = pool_alloc_unaligned (pool, cnt);
391 /* Frees the string that SS points to. */
393 ss_dealloc (struct substring *ss)
398 /* Truncates SS to at most CNT characters in length. */
400 ss_truncate (struct substring *ss, size_t cnt)
402 if (ss->length > cnt)
406 /* Removes trailing characters in TRIM_SET from SS.
407 Returns number of characters removed. */
409 ss_rtrim (struct substring *ss, struct substring trim_set)
412 while (cnt < ss->length
413 && ss_find_char (trim_set,
414 ss->string[ss->length - cnt - 1]) != SIZE_MAX)
420 /* Removes leading characters in TRIM_SET from SS.
421 Returns number of characters removed. */
423 ss_ltrim (struct substring *ss, struct substring trim_set)
425 size_t cnt = ss_span (*ss, trim_set);
426 ss_advance (ss, cnt);
430 /* Trims leading and trailing characters in TRIM_SET from SS. */
432 ss_trim (struct substring *ss, struct substring trim_set)
434 ss_ltrim (ss, trim_set);
435 ss_rtrim (ss, trim_set);
438 /* If the last character in SS is C, removes it and returns true.
439 Otherwise, returns false without changing the string. */
441 ss_chomp (struct substring *ss, char c)
443 if (ss_last (*ss) == c)
452 /* Divides SS into tokens separated by any of the DELIMITERS.
453 Each call replaces TOKEN by the next token in SS, or by an
454 empty string if no tokens remain. Returns true if a token was
455 obtained, false otherwise.
457 Before the first call, initialize *SAVE_IDX to 0. Do not
458 modify *SAVE_IDX between calls.
460 SS divides into exactly one more tokens than it contains
461 delimiters. That is, a delimiter at the start or end of SS or
462 a pair of adjacent delimiters yields an empty token, and the
463 empty string contains a single token. */
465 ss_separate (struct substring ss, struct substring delimiters,
466 size_t *save_idx, struct substring *token)
468 if (*save_idx <= ss_length (ss))
470 struct substring tmp = ss_substr (ss, *save_idx, SIZE_MAX);
471 size_t length = ss_cspan (tmp, delimiters);
472 *token = ss_head (tmp, length);
473 *save_idx += length + 1;
478 *token = ss_empty ();
483 /* Divides SS into tokens separated by any of the DELIMITERS,
484 merging adjacent delimiters so that the empty string is never
485 produced as a token. Each call replaces TOKEN by the next
486 token in SS, or by an empty string if no tokens remain, and
487 then skips past the first delimiter following the token.
488 Returns true if a token was obtained, false otherwise.
490 Before the first call, initialize *SAVE_IDX to 0. Do not
491 modify *SAVE_IDX between calls. */
493 ss_tokenize (struct substring ss, struct substring delimiters,
494 size_t *save_idx, struct substring *token)
496 ss_advance (&ss, *save_idx);
497 *save_idx += ss_ltrim (&ss, delimiters);
498 ss_get_chars (&ss, ss_cspan (ss, delimiters), token);
499 *save_idx += ss_length (*token) + 1;
500 return ss_length (*token) > 0;
503 /* Removes the first CNT characters from SS. */
505 ss_advance (struct substring *ss, size_t cnt)
507 if (cnt > ss->length)
513 /* If the first character in SS is C, removes it and returns true.
514 Otherwise, returns false without changing the string. */
516 ss_match_char (struct substring *ss, char c)
518 if (ss_first (*ss) == c)
528 /* If the first character in SS is in MATCH, removes it and
529 returns the character that was removed.
530 Otherwise, returns EOF without changing the string. */
532 ss_match_char_in (struct substring *ss, struct substring match)
536 && memchr (match.string, ss->string[0], match.length) != NULL)
545 /* If SS begins with TARGET, removes it and returns true.
546 Otherwise, returns false without changing SS. */
548 ss_match_string (struct substring *ss, const struct substring target)
550 size_t length = ss_length (target);
551 if (ss_equals (ss_head (*ss, length), target))
553 ss_advance (ss, length);
560 /* Removes the first character from SS and returns it.
561 If SS is empty, returns EOF without modifying SS. */
563 ss_get_char (struct substring *ss)
565 int c = ss_first (*ss);
574 /* Stores the prefix of SS up to the first DELIMITER in OUT (if
575 any). Trims those same characters from SS. DELIMITER is
576 removed from SS but not made part of OUT. Returns true if
577 DELIMITER was found (and removed), false otherwise. */
579 ss_get_until (struct substring *ss, char delimiter, struct substring *out)
581 ss_get_chars (ss, ss_cspan (*ss, ss_buffer (&delimiter, 1)), out);
582 return ss_match_char (ss, delimiter);
585 /* Stores the first CNT characters in SS in OUT (or fewer, if SS
586 is shorter than CNT characters). Trims the same characters
587 from the beginning of SS. Returns CNT. */
589 ss_get_chars (struct substring *ss, size_t cnt, struct substring *out)
591 *out = ss_head (*ss, cnt);
592 ss_advance (ss, cnt);
596 /* Parses and removes an optionally signed decimal integer from
597 the beginning of SS. Returns 0 if an error occurred,
598 otherwise the number of characters removed from SS. Stores
599 the integer's value into *VALUE. */
601 ss_get_long (struct substring *ss, long *value)
606 length = ss_span (*ss, ss_cstr ("+-"));
607 length += ss_span (ss_substr (*ss, length, SIZE_MAX), ss_cstr (CC_DIGITS));
608 if (length > 0 && length < sizeof tmp)
612 memcpy (tmp, ss_data (*ss), length);
615 *value = strtol (tmp, &tail, 10);
616 if (tail - tmp == length)
618 ss_advance (ss, length);
626 /* Returns true if SS is empty (contains no characters),
629 ss_is_empty (struct substring ss)
631 return ss.length == 0;
634 /* Returns the number of characters in SS. */
636 ss_length (struct substring ss)
641 /* Returns a pointer to the characters in SS. */
643 ss_data (struct substring ss)
648 /* Returns a pointer just past the last character in SS. */
650 ss_end (struct substring ss)
652 return ss.string + ss.length;
655 /* Returns the character in position IDX in SS, as a value in the
656 range of unsigned char. Returns EOF if IDX is out of the
657 range of indexes for SS. */
659 ss_at (struct substring ss, size_t idx)
661 return idx < ss.length ? (unsigned char) ss.string[idx] : EOF;
664 /* Returns the first character in SS as a value in the range of
665 unsigned char. Returns EOF if SS is the empty string. */
667 ss_first (struct substring ss)
669 return ss_at (ss, 0);
672 /* Returns the last character in SS as a value in the range of
673 unsigned char. Returns EOF if SS is the empty string. */
675 ss_last (struct substring ss)
677 return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF;
680 /* Returns the number of contiguous characters at the beginning
681 of SS that are in SKIP_SET. */
683 ss_span (struct substring ss, struct substring skip_set)
686 for (i = 0; i < ss.length; i++)
687 if (ss_find_char (skip_set, ss.string[i]) == SIZE_MAX)
692 /* Returns the number of contiguous characters at the beginning
693 of SS that are not in SKIP_SET. */
695 ss_cspan (struct substring ss, struct substring stop_set)
698 for (i = 0; i < ss.length; i++)
699 if (ss_find_char (stop_set, ss.string[i]) != SIZE_MAX)
704 /* Returns the offset in SS of the first instance of C,
705 or SIZE_MAX if C does not occur in SS. */
707 ss_find_char (struct substring ss, char c)
709 const char *p = memchr (ss.string, c, ss.length);
710 return p != NULL ? p - ss.string : SIZE_MAX;
713 /* Compares A and B and returns a strcmp()-type comparison
716 ss_compare (struct substring a, struct substring b)
718 int retval = memcmp (a.string, b.string, MIN (a.length, b.length));
720 retval = a.length < b.length ? -1 : a.length > b.length;
724 /* Compares A and B case-insensitively and returns a
725 strcmp()-type comparison result. */
727 ss_compare_case (struct substring a, struct substring b)
729 int retval = memcasecmp (a.string, b.string, MIN (a.length, b.length));
731 retval = a.length < b.length ? -1 : a.length > b.length;
735 /* Compares A and B and returns true if their contents are
736 identical, false otherwise. */
738 ss_equals (struct substring a, struct substring b)
740 return a.length == b.length && !memcmp (a.string, b.string, a.length);
743 /* Compares A and B and returns true if their contents are
744 identical except possibly for case differences, false
747 ss_equals_case (struct substring a, struct substring b)
749 return a.length == b.length && !memcasecmp (a.string, b.string, a.length);
752 /* Returns the position in SS that the character at P occupies.
753 P must point within SS or one past its end. */
755 ss_pointer_to_position (struct substring ss, const char *p)
757 size_t pos = p - ss.string;
758 assert (pos <= ss.length);
762 /* Allocates and returns a null-terminated string that contains
765 ss_xstrdup (struct substring ss)
767 char *s = xmalloc (ss.length + 1);
768 memcpy (s, ss.string, ss.length);
773 /* Initializes ST as an empty string. */
775 ds_init_empty (struct string *st)
777 st->ss = ss_empty ();
781 /* Initializes ST with initial contents S. */
783 ds_init_string (struct string *st, const struct string *s)
785 ds_init_substring (st, ds_ss (s));
788 /* Initializes ST with initial contents SS. */
790 ds_init_substring (struct string *st, struct substring ss)
792 st->capacity = MAX (8, ss.length * 2);
793 st->ss.string = xmalloc (st->capacity + 1);
794 memcpy (st->ss.string, ss.string, ss.length);
795 st->ss.length = ss.length;
798 /* Initializes ST with initial contents S. */
800 ds_init_cstr (struct string *st, const char *s)
802 ds_init_substring (st, ss_cstr (s));
807 ds_destroy (struct string *st)
811 ss_dealloc (&st->ss);
812 st->ss.string = NULL;
818 /* Swaps the contents of strings A and B. */
820 ds_swap (struct string *a, struct string *b)
822 struct string tmp = *a;
827 /* Helper function for ds_register_pool. */
829 free_string (void *st_)
831 struct string *st = st_;
835 /* Arranges for ST to be destroyed automatically as part of
838 ds_register_pool (struct string *st, struct pool *pool)
840 pool_register (pool, free_string, st);
843 /* Cancels the arrangement for ST to be destroyed automatically
846 ds_unregister_pool (struct string *st, struct pool *pool)
848 pool_unregister (pool, st);
851 /* Copies SRC into DST.
852 DST and SRC may be the same string. */
854 ds_assign_string (struct string *dst, const struct string *src)
856 ds_assign_substring (dst, ds_ss (src));
859 /* Replaces DST by SS.
860 SS may be a substring of DST. */
862 ds_assign_substring (struct string *dst, struct substring ss)
864 dst->ss.length = ss.length;
865 ds_extend (dst, ss.length);
866 memmove (dst->ss.string, ss.string, ss.length);
869 /* Replaces DST by null-terminated string SRC. SRC may overlap
872 ds_assign_cstr (struct string *dst, const char *src)
874 ds_assign_substring (dst, ss_cstr (src));
877 /* Truncates ST to zero length. */
879 ds_clear (struct string *st)
884 /* Returns a substring that contains ST. */
886 ds_ss (const struct string *st)
891 /* Returns a substring that contains CNT characters from ST
892 starting at position START.
894 If START is greater than or equal to the length of ST, then
895 the substring will be the empty string. If START + CNT
896 exceeds the length of ST, then the substring will only be
897 ds_length(ST) - START characters long. */
899 ds_substr (const struct string *st, size_t start, size_t cnt)
901 return ss_substr (ds_ss (st), start, cnt);
904 /* Returns a substring that contains the first CNT characters in
905 ST. If CNT exceeds the length of ST, then the substring will
906 contain all of ST. */
908 ds_head (const struct string *st, size_t cnt)
910 return ss_head (ds_ss (st), cnt);
913 /* Returns a substring that contains the last CNT characters in
914 ST. If CNT exceeds the length of ST, then the substring will
915 contain all of ST. */
917 ds_tail (const struct string *st, size_t cnt)
919 return ss_tail (ds_ss (st), cnt);
922 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
925 ds_extend (struct string *st, size_t min_capacity)
927 if (min_capacity > st->capacity)
930 if (st->capacity < min_capacity)
931 st->capacity = 2 * min_capacity;
933 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
937 /* Shrink ST to the minimum capacity need to contain its content. */
939 ds_shrink (struct string *st)
941 if (st->capacity != st->ss.length)
943 st->capacity = st->ss.length;
944 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
948 /* Truncates ST to at most LENGTH characters long. */
950 ds_truncate (struct string *st, size_t length)
952 ss_truncate (&st->ss, length);
955 /* Removes trailing characters in TRIM_SET from ST.
956 Returns number of characters removed. */
958 ds_rtrim (struct string *st, struct substring trim_set)
960 return ss_rtrim (&st->ss, trim_set);
963 /* Removes leading characters in TRIM_SET from ST.
964 Returns number of characters removed. */
966 ds_ltrim (struct string *st, struct substring trim_set)
968 size_t cnt = ds_span (st, trim_set);
970 ds_assign_substring (st, ds_substr (st, cnt, SIZE_MAX));
974 /* Trims leading and trailing characters in TRIM_SET from ST.
975 Returns number of charactesr removed. */
977 ds_trim (struct string *st, struct substring trim_set)
979 size_t cnt = ds_rtrim (st, trim_set);
980 return cnt + ds_ltrim (st, trim_set);
983 /* If the last character in ST is C, removes it and returns true.
984 Otherwise, returns false without modifying ST. */
986 ds_chomp (struct string *st, char c)
988 return ss_chomp (&st->ss, c);
991 /* Divides ST into tokens separated by any of the DELIMITERS.
992 Each call replaces TOKEN by the next token in ST, or by an
993 empty string if no tokens remain. Returns true if a token was
994 obtained, false otherwise.
996 Before the first call, initialize *SAVE_IDX to 0. Do not
997 modify *SAVE_IDX between calls.
999 ST divides into exactly one more tokens than it contains
1000 delimiters. That is, a delimiter at the start or end of ST or
1001 a pair of adjacent delimiters yields an empty token, and the
1002 empty string contains a single token. */
1004 ds_separate (const struct string *st, struct substring delimiters,
1005 size_t *save_idx, struct substring *token)
1007 return ss_separate (ds_ss (st), delimiters, save_idx, token);
1010 /* Divides ST into tokens separated by any of the DELIMITERS,
1011 merging adjacent delimiters so that the empty string is never
1012 produced as a token. Each call replaces TOKEN by the next
1013 token in ST, or by an empty string if no tokens remain.
1014 Returns true if a token was obtained, false otherwise.
1016 Before the first call, initialize *SAVE_IDX to 0. Do not
1017 modify *SAVE_IDX between calls. */
1019 ds_tokenize (const struct string *st, struct substring delimiters,
1020 size_t *save_idx, struct substring *token)
1022 return ss_tokenize (ds_ss (st), delimiters, save_idx, token);
1025 /* Pad ST on the right with copies of PAD until ST is at least
1026 LENGTH characters in size. If ST is initially LENGTH
1027 characters or longer, this is a no-op. */
1029 ds_rpad (struct string *st, size_t length, char pad)
1031 if (length > st->ss.length)
1032 ds_put_char_multiple (st, pad, length - st->ss.length);
1035 /* Sets the length of ST to exactly NEW_LENGTH,
1036 either by truncating characters from the end,
1037 or by padding on the right with PAD. */
1039 ds_set_length (struct string *st, size_t new_length, char pad)
1041 if (st->ss.length < new_length)
1042 ds_rpad (st, new_length, pad);
1044 st->ss.length = new_length;
1047 /* Removes N characters from ST starting at offset START. */
1049 ds_remove (struct string *st, size_t start, size_t n)
1051 if (n > 0 && start < st->ss.length)
1053 if (st->ss.length - start <= n)
1055 /* All characters at or beyond START are deleted. */
1056 st->ss.length = start;
1060 /* Some characters remain and must be shifted into
1062 memmove (st->ss.string + st->ss.length,
1063 st->ss.string + st->ss.length + n,
1064 st->ss.length - start - n);
1070 /* There are no characters to delete or no characters at or
1071 beyond START, hence deletion is a no-op. */
1075 /* Returns true if ST is empty, false otherwise. */
1077 ds_is_empty (const struct string *st)
1079 return ss_is_empty (st->ss);
1082 /* Returns the length of ST. */
1084 ds_length (const struct string *st)
1086 return ss_length (ds_ss (st));
1089 /* Returns the string data inside ST. */
1091 ds_data (const struct string *st)
1093 return ss_data (ds_ss (st));
1096 /* Returns a pointer to the null terminator ST.
1097 This might not be an actual null character unless ds_c_str() has
1098 been called since the last modification to ST. */
1100 ds_end (const struct string *st)
1102 return ss_end (ds_ss (st));
1105 /* Returns the character in position IDX in ST, as a value in the
1106 range of unsigned char. Returns EOF if IDX is out of the
1107 range of indexes for ST. */
1109 ds_at (const struct string *st, size_t idx)
1111 return ss_at (ds_ss (st), idx);
1114 /* Returns the first character in ST as a value in the range of
1115 unsigned char. Returns EOF if ST is the empty string. */
1117 ds_first (const struct string *st)
1119 return ss_first (ds_ss (st));
1122 /* Returns the last character in ST as a value in the range of
1123 unsigned char. Returns EOF if ST is the empty string. */
1125 ds_last (const struct string *st)
1127 return ss_last (ds_ss (st));
1130 /* Returns the number of consecutive characters at the beginning
1131 of ST that are in SKIP_SET. */
1133 ds_span (const struct string *st, struct substring skip_set)
1135 return ss_span (ds_ss (st), skip_set);
1138 /* Returns the number of consecutive characters at the beginning
1139 of ST that are not in STOP_SET. */
1141 ds_cspan (const struct string *st, struct substring stop_set)
1143 return ss_cspan (ds_ss (st), stop_set);
1146 /* Returns the position of the first occurrence of character C in
1147 ST at or after position OFS, or SIZE_MAX if there is no such
1150 ds_find_char (const struct string *st, char c)
1152 return ss_find_char (ds_ss (st), c);
1155 /* Compares A and B and returns a strcmp()-type comparison
1158 ds_compare (const struct string *a, const struct string *b)
1160 return ss_compare (ds_ss (a), ds_ss (b));
1163 /* Returns the position in ST that the character at P occupies.
1164 P must point within ST or one past its end. */
1166 ds_pointer_to_position (const struct string *st, const char *p)
1168 return ss_pointer_to_position (ds_ss (st), p);
1171 /* Allocates and returns a null-terminated string that contains
1174 ds_xstrdup (const struct string *st)
1176 return ss_xstrdup (ds_ss (st));
1179 /* Returns the allocation size of ST. */
1181 ds_capacity (const struct string *st)
1183 return st->capacity;
1186 /* Returns the value of ST as a null-terminated string. */
1188 ds_cstr (const struct string *st_)
1190 struct string *st = CONST_CAST (struct string *, st_);
1191 if (st->ss.string == NULL)
1193 st->ss.string[st->ss.length] = '\0';
1194 return st->ss.string;
1197 /* Returns the value of ST as a null-terminated string and then
1198 reinitialized ST as an empty string. The caller must free the
1199 returned string with free(). */
1201 ds_steal_cstr (struct string *st)
1203 char *s = ds_cstr (st);
1208 /* Reads characters from STREAM and appends them to ST, stopping
1209 after MAX_LENGTH characters, after appending a newline, or
1210 after an I/O error or end of file was encountered, whichever
1211 comes first. Returns true if at least one character was added
1212 to ST, false if no characters were read before an I/O error or
1213 end of file (or if MAX_LENGTH was 0).
1215 This function treats LF and CR LF sequences as new-line,
1216 translating each of them to a single '\n' new-line character
1219 ds_read_line (struct string *st, FILE *stream, size_t max_length)
1223 for (length = 0; length < max_length; length++)
1225 int c = getc (stream);
1232 ds_put_char (st, c);
1239 /* CR followed by LF is special: translate to \n. */
1240 ds_put_char (st, '\n');
1245 /* CR followed by anything else is just CR. */
1246 ds_put_char (st, '\r');
1254 ds_put_char (st, c);
1261 /* Removes a comment introduced by `#' from ST,
1262 ignoring occurrences inside quoted strings. */
1264 remove_comment (struct string *st)
1269 for (cp = ds_data (st); cp < ds_end (st); cp++)
1274 else if (*cp == '\\')
1277 else if (*cp == '\'' || *cp == '"')
1279 else if (*cp == '#')
1281 ds_truncate (st, cp - ds_cstr (st));
1286 /* Reads a line from STREAM into ST, then preprocesses as follows:
1288 - Splices lines terminated with `\'.
1290 - Deletes comments introduced by `#' outside of single or double
1293 - Deletes trailing white space.
1295 Returns true if a line was successfully read, false on
1296 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
1297 incremented by the number of lines read. */
1299 ds_read_config_line (struct string *st, int *line_number, FILE *stream)
1304 if (!ds_read_line (st, stream, SIZE_MAX))
1307 ds_rtrim (st, ss_cstr (CC_SPACES));
1309 while (ds_chomp (st, '\\'));
1311 remove_comment (st);
1315 /* Attempts to read SIZE * CNT bytes from STREAM and append them
1317 Returns true if all the requested data was read, false otherwise. */
1319 ds_read_stream (struct string *st, size_t size, size_t cnt, FILE *stream)
1323 size_t try_bytes = xtimes (cnt, size);
1324 if (size_in_bounds_p (xsum (ds_length (st), try_bytes)))
1326 char *buffer = ds_put_uninit (st, try_bytes);
1327 size_t got_bytes = fread (buffer, 1, try_bytes, stream);
1328 ds_truncate (st, ds_length (st) - (try_bytes - got_bytes));
1329 return got_bytes == try_bytes;
1341 /* Concatenates S onto ST. */
1343 ds_put_cstr (struct string *st, const char *s)
1346 ds_put_substring (st, ss_cstr (s));
1349 /* Concatenates SS to ST. */
1351 ds_put_substring (struct string *st, struct substring ss)
1353 memcpy (ds_put_uninit (st, ss_length (ss)), ss_data (ss), ss_length (ss));
1356 /* Returns ds_end(ST) and THEN increases the length by INCR. */
1358 ds_put_uninit (struct string *st, size_t incr)
1361 ds_extend (st, ds_length (st) + incr);
1363 st->ss.length += incr;
1367 /* Formats FORMAT as a printf string and appends the result to ST. */
1369 ds_put_format (struct string *st, const char *format, ...)
1373 va_start (args, format);
1374 ds_put_vformat (st, format, args);
1378 /* Formats FORMAT as a printf string and appends the result to ST. */
1380 ds_put_vformat (struct string *st, const char *format, va_list args_)
1385 va_copy (args, args_);
1386 avail = st->ss.string != NULL ? st->capacity - st->ss.length + 1 : 0;
1387 needed = vsnprintf (st->ss.string + st->ss.length, avail, format, args);
1390 if (needed >= avail)
1392 va_copy (args, args_);
1393 vsprintf (ds_put_uninit (st, needed), format, args);
1398 /* Some old libc's returned -1 when the destination string
1400 while (needed == -1)
1402 ds_extend (st, (st->capacity + 1) * 2);
1403 avail = st->capacity - st->ss.length + 1;
1405 va_copy (args, args_);
1406 needed = vsnprintf (ds_end (st), avail, format, args);
1409 st->ss.length += needed;
1413 /* Appends character CH to ST. */
1415 ds_put_char (struct string *st, int ch)
1417 ds_put_uninit (st, 1)[0] = ch;
1420 /* Appends CNT copies of character CH to ST. */
1422 ds_put_char_multiple (struct string *st, int ch, size_t cnt)
1424 memset (ds_put_uninit (st, cnt), ch, cnt);
1428 /* If relocation has been enabled, replace ST,
1429 with its relocated version */
1431 ds_relocate (struct string *st)
1433 const char *orig = ds_cstr (st);
1434 const char *rel = relocate (orig);
1439 ds_put_cstr (st, rel);
1440 /* The documentation for relocate says that casting away const
1441 and then freeing is appropriate ... */
1442 free (CONST_CAST (char *, rel));
1449 /* Operations on uint8_t "strings" */
1451 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
1452 DST is truncated to DST_SIZE bytes or padded on the right with
1453 copies of PAD as needed. */
1455 u8_buf_copy_rpad (uint8_t *dst, size_t dst_size,
1456 const uint8_t *src, size_t src_size,
1459 if (src_size >= dst_size)
1460 memmove (dst, src, dst_size);
1463 memmove (dst, src, src_size);
1464 memset (&dst[src_size], pad, dst_size - src_size);