1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2014,
3 2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "libpspp/assertion.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "libpspp/pool.h"
34 #include "gl/c-ctype.h"
35 #include "gl/c-vasnprintf.h"
36 #include "gl/relocatable.h"
37 #include "gl/minmax.h"
38 #include "gl/xalloc.h"
39 #include "gl/xmemdup0.h"
42 /* Reverses the order of NBYTES bytes at address P, thus converting
43 between little- and big-endian byte orders. */
45 buf_reverse (char *p
, size_t nbytes
)
47 char *h
= p
, *t
= &h
[nbytes
- 1];
59 /* Compares the SIZE bytes in A to those in B, disregarding case,
60 and returns a strcmp()-type result. */
62 buf_compare_case (const char *a_
, const char *b_
, size_t size
)
64 const unsigned char *a
= (unsigned char *) a_
;
65 const unsigned char *b
= (unsigned char *) b_
;
69 unsigned char ac
= toupper (*a
++);
70 unsigned char bc
= toupper (*b
++);
73 return ac
> bc
? 1 : -1;
79 /* Compares A of length A_LEN to B of length B_LEN. The shorter
80 string is considered to be padded with spaces to the length of
83 buf_compare_rpad (const char *a
, size_t a_len
, const char *b
, size_t b_len
)
88 min_len
= a_len
< b_len
? a_len
: b_len
;
89 result
= memcmp (a
, b
, min_len
);
98 for (idx
= min_len
; idx
< b_len
; idx
++)
100 return ' ' > b
[idx
] ? 1 : -1;
104 for (idx
= min_len
; idx
< a_len
; idx
++)
106 return a
[idx
] > ' ' ? 1 : -1;
112 /* Compares strin A to string B. The shorter string is
113 considered to be padded with spaces to the length of the
116 str_compare_rpad (const char *a
, const char *b
)
118 return buf_compare_rpad (a
, strlen (a
), b
, strlen (b
));
121 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
122 DST is truncated to DST_SIZE bytes or padded on the right with
123 copies of PAD as needed. */
125 buf_copy_str_rpad (char *dst
, size_t dst_size
, const char *src
, char pad
)
127 size_t src_len
= strlen (src
);
128 if (src_len
>= dst_size
)
129 memcpy (dst
, src
, dst_size
);
132 memcpy (dst
, src
, src_len
);
133 memset (&dst
[src_len
], pad
, dst_size
- src_len
);
137 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
138 DST is truncated to DST_SIZE bytes or padded on the left with
139 copies of PAD as needed. */
141 buf_copy_str_lpad (char *dst
, size_t dst_size
, const char *src
, char pad
)
143 size_t src_len
= strlen (src
);
144 if (src_len
>= dst_size
)
145 memcpy (dst
, src
, dst_size
);
148 size_t n_pad
= dst_size
- src_len
;
149 memset (&dst
[0], pad
, n_pad
);
150 memcpy (dst
+ n_pad
, src
, src_len
);
154 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
155 DST is truncated to DST_SIZE bytes or padded on the left with
156 copies of PAD as needed. */
158 buf_copy_lpad (char *dst
, size_t dst_size
,
159 const char *src
, size_t src_size
,
162 if (src_size
>= dst_size
)
163 memmove (dst
, src
, dst_size
);
166 memset (dst
, pad
, dst_size
- src_size
);
167 memmove (&dst
[dst_size
- src_size
], src
, src_size
);
171 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
172 DST is truncated to DST_SIZE bytes or padded on the right with
173 copies of PAD as needed. */
175 buf_copy_rpad (char *dst
, size_t dst_size
,
176 const char *src
, size_t src_size
,
179 if (src_size
>= dst_size
)
180 memmove (dst
, src
, dst_size
);
183 memmove (dst
, src
, src_size
);
184 memset (&dst
[src_size
], pad
, dst_size
- src_size
);
188 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
190 Truncates DST to DST_SIZE - 1 bytes or right-pads with
191 spaces to DST_SIZE - 1 bytes if necessary. */
193 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;
209 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
210 Truncates DST to DST_SIZE - 1 bytes, if necessary. */
212 str_copy_trunc (char *dst
, size_t dst_size
, const char *src
)
214 size_t src_len
= strlen (src
);
215 assert (dst_size
> 0);
216 if (src_len
+ 1 < dst_size
)
217 memcpy (dst
, src
, src_len
+ 1);
220 memcpy (dst
, src
, dst_size
- 1);
221 dst
[dst_size
- 1] = '\0';
225 /* Copies buffer SRC, of SRC_LEN bytes,
226 to DST, which is in a buffer DST_SIZE bytes long.
227 Truncates DST to DST_SIZE - 1 bytes, if necessary. */
229 str_copy_buf_trunc (char *dst
, size_t dst_size
,
230 const char *src
, size_t src_size
)
233 assert (dst_size
> 0);
235 dst_len
= src_size
< dst_size
? src_size
: dst_size
- 1;
236 memcpy (dst
, src
, dst_len
);
240 /* Converts each byte in S to uppercase.
242 This is suitable only for ASCII strings. Use utf8_to_upper() for UTF-8
245 str_uppercase (char *s
)
247 for (; *s
!= '\0'; s
++)
248 *s
= c_toupper ((unsigned char) *s
);
251 /* Converts each byte in S to lowercase.
253 This is suitable only for ASCII strings. Use utf8_to_lower() for UTF-8
256 str_lowercase (char *s
)
258 for (; *s
!= '\0'; s
++)
259 *s
= c_tolower ((unsigned char) *s
);
262 /* Converts NUMBER into a string in 26-adic notation in BUFFER,
263 which has room for SIZE bytes. Uses uppercase if UPPERCASE is
264 true, otherwise lowercase, Returns true if successful, false
265 if NUMBER, plus a trailing null, is too large to fit in the
266 available space. If SIZE is at least F26ADIC_STRLEN_MAX + 1,
267 the number is guaranteed to fit. Even if the number doesn't
268 fit, if SIZE > 0, the characters that do fit, if any, will be
271 26-adic notation is "spreadsheet column numbering": 1 = A, 2 =
272 B, 3 = C, ... 26 = Z, 27 = AA, 28 = AB, 29 = AC, ... Zero is
275 26-adic notation is the special case of a k-adic numeration
276 system (aka bijective base-k numeration) with k=26. In k-adic
277 numeration, the digits are {1, 2, 3, ..., k} (there is no
278 digit 0), and integer 0 is represented by the empty string.
279 For more information, see
280 http://en.wikipedia.org/wiki/Bijective_numeration. */
282 str_format_26adic__ (unsigned long int number
, bool uppercase
,
283 char buffer
[], size_t size
)
286 = uppercase
? "ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "abcdefghijklmnopqrstuvwxyz";
293 buffer
[length
++] = alphabet
[number
% 26];
299 buffer
[length
] = '\0';
301 buf_reverse (buffer
, length
);
310 /* Like str_format_26adic__(), but SIZE must be big enough that it cannot
313 str_format_26adic (unsigned long int number
, bool uppercase
,
314 char buffer
[], size_t size
)
316 assert (size
>= F26ADIC_STRLEN_MAX
+ 1);
317 if (!str_format_26adic__ (number
, uppercase
, buffer
, size
))
321 /* Returns the value of 26-adic string STR. See str_format_26adic() for a
322 definition of 26-adic.
324 On error, this function returns -1. */
326 str_parse_26adic (const char *str
)
333 size_t len
= strlen (str
);
334 for (size_t i
= 0; i
< len
; i
++)
336 if (result
>= INT_MAX
/ RADIX
)
339 char c
= str
[len
- i
- 1];
340 int digit
= (c
>= 'A' && c
<= 'Z' ? c
- 'A'
341 : c
>= 'a' && c
<= 'z' ? c
- 'a'
345 assert (digit
>= 0 && digit
< RADIX
);
347 result
+= (digit
+ (i
> 0)) * multiplier
;
354 /* Copies IN to buffer OUT with size OUT_SIZE, appending a null terminator. If
355 IN is too long for OUT, or if IN contains a new-line, replaces the tail with
358 OUT_SIZE must be at least 16. */
360 str_ellipsize (struct substring in
, char *out
, size_t out_size
)
362 assert (out_size
>= 16);
364 size_t out_maxlen
= out_size
- 1;
365 if (in
.length
> out_maxlen
- 3)
369 while (out_len
< in
.length
370 && in
.string
[out_len
] != '\n'
371 && in
.string
[out_len
] != '\0'
372 && (in
.string
[out_len
] != '\r'
373 || out_len
+ 1 >= in
.length
374 || in
.string
[out_len
+ 1] != '\n'))
376 int mblen
= u8_mblen (CHAR_CAST (const uint8_t *, in
.string
+ out_len
),
377 in
.length
- out_len
);
378 if (mblen
< 0 || out_len
+ mblen
> out_maxlen
)
383 memcpy (out
, in
.string
, out_len
);
384 strcpy (&out
[out_len
], out_len
< in
.length
? "..." : "");
387 /* Sets the SIZE bytes starting at BLOCK to C,
388 and returns the byte following BLOCK. */
390 mempset (void *block
, int c
, size_t size
)
392 memset (block
, c
, size
);
393 return (char *) block
+ size
;
398 /* Returns a substring whose contents are the N bytes
399 starting at the (0-based) position START in SS. */
401 ss_substr (struct substring ss
, size_t start
, size_t n
)
403 if (start
< ss
.length
)
404 return ss_buffer (ss
.string
+ start
, MIN (n
, ss
.length
- start
));
406 return ss_buffer (ss
.string
+ ss
.length
, 0);
409 /* Returns a substring whose contents are the first N
412 ss_head (struct substring ss
, size_t n
)
414 return ss_buffer (ss
.string
, MIN (n
, ss
.length
));
417 /* Returns a substring whose contents are the last N bytes
420 ss_tail (struct substring ss
, size_t n
)
423 return ss_buffer (ss
.string
+ (ss
.length
- n
), n
);
428 /* Returns a malloc()'d, null-terminated copy of the contents of OLD. The
429 caller owns the returned string and must eventually free it. */
431 ss_clone (struct substring old
)
433 return (struct substring
) {
434 .string
= xmemdup0 (old
.string
, old
.length
),
435 .length
= old
.length
,
439 /* Allocates room for a N-byte string in NEW. */
441 ss_alloc_uninit (struct substring
*new, size_t n
)
443 new->string
= xmalloc (n
);
448 ss_realloc (struct substring
*ss
, size_t size
)
450 ss
->string
= xrealloc (ss
->string
, size
);
453 /* Returns a pool_alloc_unaligned()'d, null-terminated copy of the contents of
454 OLD in POOL. The pool owns the returned string. */
456 ss_clone_pool (struct substring old
, struct pool
*pool
)
458 return (struct substring
) {
459 .string
= pool_memdup0 (pool
, old
.string
, old
.length
),
464 /* Allocates room for a N-byte string in NEW in POOL. */
466 ss_alloc_uninit_pool (struct substring
*new, size_t n
, struct pool
*pool
)
468 new->string
= pool_alloc_unaligned (pool
, n
);
472 /* Frees the string that SS points to. */
474 ss_dealloc (struct substring
*ss
)
479 /* Exchanges the contents of A and B. */
481 ss_swap (struct substring
*a
, struct substring
*b
)
483 struct substring tmp
= *a
;
488 /* Truncates SS to at most N bytes in length. */
490 ss_truncate (struct substring
*ss
, size_t n
)
496 /* Removes trailing bytes in TRIM_SET from SS.
497 Returns number of bytes removed. */
499 ss_rtrim (struct substring
*ss
, struct substring trim_set
)
502 while (n
< ss
->length
503 && ss_find_byte (trim_set
,
504 ss
->string
[ss
->length
- n
- 1]) != SIZE_MAX
)
510 /* Removes leading bytes in TRIM_SET from SS.
511 Returns number of bytes removed. */
513 ss_ltrim (struct substring
*ss
, struct substring trim_set
)
515 size_t n
= ss_span (*ss
, trim_set
);
520 /* Trims leading and trailing bytes in TRIM_SET from SS. */
522 ss_trim (struct substring
*ss
, struct substring trim_set
)
524 ss_ltrim (ss
, trim_set
);
525 ss_rtrim (ss
, trim_set
);
528 /* If the last byte in SS is C, removes it and returns true.
529 Otherwise, returns false without changing the string. */
531 ss_chomp_byte (struct substring
*ss
, char c
)
533 if (ss_last (*ss
) == c
)
542 /* If SS ends with SUFFIX, removes it and returns true.
543 Otherwise, returns false without changing the string. */
545 ss_chomp (struct substring
*ss
, struct substring suffix
)
547 if (ss_ends_with (*ss
, suffix
))
549 ss
->length
-= suffix
.length
;
556 /* Divides SS into tokens separated by any of the DELIMITERS.
557 Each call replaces TOKEN by the next token in SS, or by an
558 empty string if no tokens remain. Returns true if a token was
559 obtained, false otherwise.
561 Before the first call, initialize *SAVE_IDX to 0. Do not
562 modify *SAVE_IDX between calls.
564 SS divides into exactly one more tokens than it contains
565 delimiters. That is, a delimiter at the start or end of SS or
566 a pair of adjacent delimiters yields an empty token, and the
567 empty string contains a single token. */
569 ss_separate (struct substring ss
, struct substring delimiters
,
570 size_t *save_idx
, struct substring
*token
)
572 if (*save_idx
<= ss_length (ss
))
574 struct substring tmp
= ss_substr (ss
, *save_idx
, SIZE_MAX
);
575 size_t length
= ss_cspan (tmp
, delimiters
);
576 *token
= ss_head (tmp
, length
);
577 *save_idx
+= length
+ 1;
582 *token
= ss_empty ();
587 /* Divides SS into tokens separated by any of the DELIMITERS,
588 merging adjacent delimiters so that the empty string is never
589 produced as a token. Each call replaces TOKEN by the next
590 token in SS, or by an empty string if no tokens remain, and
591 then skips past the first delimiter following the token.
592 Returns true if a token was obtained, false otherwise.
594 Before the first call, initialize *SAVE_IDX to 0. Do not
595 modify *SAVE_IDX between calls. */
597 ss_tokenize (struct substring ss
, struct substring delimiters
,
598 size_t *save_idx
, struct substring
*token
)
602 ss_advance (&ss
, *save_idx
);
603 *save_idx
+= ss_ltrim (&ss
, delimiters
);
604 ss_get_bytes (&ss
, ss_cspan (ss
, delimiters
), token
);
606 found_token
= ss_length (*token
) > 0;
607 *save_idx
+= ss_length (*token
) + (found_token
?1:0);
611 /* Removes the first N bytes from SS. */
613 ss_advance (struct substring
*ss
, size_t n
)
621 /* If the first byte in SS is C, removes it and returns true.
622 Otherwise, returns false without changing the string. */
624 ss_match_byte (struct substring
*ss
, char c
)
626 if (ss_first (*ss
) == c
)
636 /* If the first byte in SS is in MATCH, removes it and
637 returns the byte that was removed.
638 Otherwise, returns EOF without changing the string. */
640 ss_match_byte_in (struct substring
*ss
, struct substring match
)
644 && memchr (match
.string
, ss
->string
[0], match
.length
) != NULL
)
653 /* If SS begins with TARGET, removes it and returns true.
654 Otherwise, returns false without changing SS. */
656 ss_match_string (struct substring
*ss
, const struct substring target
)
658 size_t length
= ss_length (target
);
659 if (ss_equals (ss_head (*ss
, length
), target
))
661 ss_advance (ss
, length
);
668 /* If SS begins with TARGET, except possibly for case differences, removes it
669 and returns true. Otherwise, returns false without changing SS. */
671 ss_match_string_case (struct substring
*ss
, const struct substring target
)
673 size_t length
= ss_length (target
);
674 if (ss_equals_case (ss_head (*ss
, length
), target
))
676 ss_advance (ss
, length
);
683 /* Removes the first byte from SS and returns it.
684 If SS is empty, returns EOF without modifying SS. */
686 ss_get_byte (struct substring
*ss
)
688 int c
= ss_first (*ss
);
697 /* Stores the prefix of SS up to the first DELIMITER in OUT (if
698 any). Trims those same bytes from SS. DELIMITER is
699 removed from SS but not made part of OUT. Returns true if
700 DELIMITER was found (and removed), false otherwise. */
702 ss_get_until (struct substring
*ss
, char delimiter
, struct substring
*out
)
704 ss_get_bytes (ss
, ss_cspan (*ss
, ss_buffer (&delimiter
, 1)), out
);
705 return ss_match_byte (ss
, delimiter
);
708 /* Stores the first N bytes in SS in OUT (or fewer, if SS
709 is shorter than N bytes). Trims the same bytes
710 from the beginning of SS. Returns N. */
712 ss_get_bytes (struct substring
*ss
, size_t n
, struct substring
*out
)
714 *out
= ss_head (*ss
, n
);
719 /* Parses and removes an optionally signed decimal integer from
720 the beginning of SS. Returns 0 if an error occurred,
721 otherwise the number of bytes removed from SS. Stores
722 the integer's value into *VALUE. */
724 ss_get_long (struct substring
*ss
, long *value
)
729 length
= ss_span (*ss
, ss_cstr ("+-"));
730 length
+= ss_span (ss_substr (*ss
, length
, SIZE_MAX
), ss_cstr (CC_DIGITS
));
731 if (length
> 0 && length
< sizeof tmp
)
735 memcpy (tmp
, ss_data (*ss
), length
);
738 *value
= strtol (tmp
, &tail
, 10);
739 if (tail
- tmp
== length
)
741 ss_advance (ss
, length
);
749 /* Returns true if SS is empty (has length 0 bytes),
752 ss_is_empty (struct substring ss
)
754 return ss
.length
== 0;
757 /* Returns the number of bytes in SS. */
759 ss_length (struct substring ss
)
764 /* Returns a pointer to the bytes in SS. */
766 ss_data (struct substring ss
)
771 /* Returns a pointer just past the last byte in SS. */
773 ss_end (struct substring ss
)
775 return ss
.string
+ ss
.length
;
778 /* Returns the byte in position IDX in SS, as a value in the
779 range of unsigned char. Returns EOF if IDX is out of the
780 range of indexes for SS. */
782 ss_at (struct substring ss
, size_t idx
)
784 return idx
< ss
.length
? (unsigned char) ss
.string
[idx
] : EOF
;
787 /* Returns the first byte in SS as a value in the range of
788 unsigned char. Returns EOF if SS is the empty string. */
790 ss_first (struct substring ss
)
792 return ss_at (ss
, 0);
795 /* Returns the last byte in SS as a value in the range of
796 unsigned char. Returns EOF if SS is the empty string. */
798 ss_last (struct substring ss
)
800 return ss
.length
> 0 ? (unsigned char) ss
.string
[ss
.length
- 1] : EOF
;
803 /* Returns true if SS starts with PREFIX, false otherwise. */
805 ss_starts_with (struct substring ss
, struct substring prefix
)
807 return (ss
.length
>= prefix
.length
808 && !memcmp (ss
.string
, prefix
.string
, prefix
.length
));
811 /* Returns true if SS starts with PREFIX in any case, false otherwise. */
813 ss_starts_with_case (struct substring ss
, struct substring prefix
)
815 return (ss
.length
>= prefix
.length
816 && !memcasecmp (ss
.string
, prefix
.string
, prefix
.length
));
819 /* Returns true if SS ends with SUFFIX, false otherwise. */
821 ss_ends_with (struct substring ss
, struct substring suffix
)
823 return (ss
.length
>= suffix
.length
824 && !memcmp (&ss
.string
[ss
.length
- suffix
.length
], suffix
.string
,
828 /* Returns true if SS ends with SUFFIX in any case, false otherwise. */
830 ss_ends_with_case (struct substring ss
, struct substring suffix
)
832 return (ss
.length
>= suffix
.length
833 && !memcasecmp (&ss
.string
[ss
.length
- suffix
.length
], suffix
.string
,
837 /* Returns the number of contiguous bytes at the beginning
838 of SS that are in SKIP_SET. */
840 ss_span (struct substring ss
, struct substring skip_set
)
843 for (i
= 0; i
< ss
.length
; i
++)
844 if (ss_find_byte (skip_set
, ss
.string
[i
]) == SIZE_MAX
)
849 /* Returns the number of contiguous bytes at the beginning
850 of SS that are not in SKIP_SET. */
852 ss_cspan (struct substring ss
, struct substring stop_set
)
855 for (i
= 0; i
< ss
.length
; i
++)
856 if (ss_find_byte (stop_set
, ss
.string
[i
]) != SIZE_MAX
)
861 /* Returns the offset in SS of the first instance of C,
862 or SIZE_MAX if C does not occur in SS. */
864 ss_find_byte (struct substring ss
, char c
)
866 const char *p
= memchr (ss
.string
, (int) c
, ss
.length
);
867 return p
!= NULL
? p
- ss
.string
: SIZE_MAX
;
870 /* Returns the offset in HAYSTACK of the first instance of NEEDLE,
871 or SIZE_MAX if NEEDLE does not occur in HAYSTACK. */
873 ss_find_substring (struct substring haystack
, struct substring needle
)
875 const char *p
= memmem (haystack
.string
, haystack
.length
,
876 needle
.string
, needle
.length
);
877 return p
!= NULL
? p
- haystack
.string
: SIZE_MAX
;
880 /* Compares A and B and returns a strcmp()-type comparison
883 ss_compare (struct substring a
, struct substring b
)
885 int retval
= memcmp (a
.string
, b
.string
, MIN (a
.length
, b
.length
));
887 retval
= a
.length
< b
.length
? -1 : a
.length
> b
.length
;
891 /* Compares A to B and returns a strcmp()-type comparison result. The shorter
892 string is considered to be padded with spaces to the length of the
895 ss_compare_rpad (struct substring a
, struct substring b
)
897 return buf_compare_rpad (a
.string
, a
.length
, b
.string
, b
.length
);
900 /* Compares A and B case-insensitively and returns a
901 strcmp()-type comparison result. */
903 ss_compare_case (struct substring a
, struct substring b
)
905 int retval
= memcasecmp (a
.string
, b
.string
, MIN (a
.length
, b
.length
));
907 retval
= a
.length
< b
.length
? -1 : a
.length
> b
.length
;
911 /* Compares A and B and returns true if their contents are
912 identical, false otherwise. */
914 ss_equals (struct substring a
, struct substring b
)
916 return a
.length
== b
.length
&& !memcmp (a
.string
, b
.string
, a
.length
);
919 /* Compares A and B and returns true if their contents are
920 identical except possibly for case differences, false
923 ss_equals_case (struct substring a
, struct substring b
)
925 return a
.length
== b
.length
&& !memcasecmp (a
.string
, b
.string
, a
.length
);
928 /* Returns the position in SS that the byte at P occupies.
929 P must point within SS or one past its end. */
931 ss_pointer_to_position (struct substring ss
, const char *p
)
933 size_t pos
= p
- ss
.string
;
934 assert (pos
<= ss
.length
);
938 /* Allocates and returns a null-terminated string that contains
941 ss_xstrdup (struct substring ss
)
943 char *s
= xmalloc (ss
.length
+ 1);
944 memcpy (s
, ss
.string
, ss
.length
);
950 /* Returns the character represented by the UTF-8 sequence at the start of S.
951 The return value is either a Unicode code point in the range 0 to 0x10ffff,
952 or UINT32_MAX if S is empty. */
954 ss_first_mb (struct substring s
)
956 return ss_at_mb (s
, 0);
959 /* Returns the number of bytes in the UTF-8 character at the beginning of S.
961 The return value is 0 if S is empty, otherwise between 1 and 4. */
963 ss_first_mblen (struct substring s
)
965 return ss_at_mblen (s
, 0);
968 /* Advances S past the UTF-8 character at its beginning. Returns the Unicode
969 code point that was skipped (in the range 0 to 0x10ffff), or UINT32_MAX if S
970 was not modified because it was initially empty. */
972 ss_get_mb (struct substring
*s
)
979 n
= u8_mbtouc (&uc
, CHAR_CAST (const uint8_t *, s
->string
), s
->length
);
988 /* Returns the character represented by the UTF-8 sequence starting OFS bytes
989 into S. The return value is either a Unicode code point in the range 0 to
990 0x10ffff, or UINT32_MAX if OFS is past the last byte in S.
992 (Returns 0xfffd if OFS points into the middle, not the beginning, of a UTF-8
995 ss_at_mb (struct substring s
, size_t ofs
)
1000 u8_mbtouc (&uc
, CHAR_CAST (const uint8_t *, s
.string
+ ofs
),
1008 /* Returns the number of bytes represented by the UTF-8 sequence starting OFS
1009 bytes into S. The return value is 0 if OFS is past the last byte in S,
1010 otherwise between 1 and 4. */
1012 ss_at_mblen (struct substring s
, size_t ofs
)
1017 return u8_mbtouc (&uc
, CHAR_CAST (const uint8_t *, s
.string
+ ofs
),
1025 ss_utf8_count_columns (struct substring s
)
1027 return utf8_count_columns (s
.string
, s
.length
);
1030 /* Returns a substring of S starting at 0-based display column START and
1031 running for N display columns. */
1033 ss_utf8_columns (struct substring s
, size_t start
, size_t n
)
1035 ss_advance (&s
, utf8_columns_to_bytes (s
.string
, s
.length
, start
));
1036 s
.length
= utf8_columns_to_bytes (s
.string
, s
.length
, n
);
1040 /* Initializes ST as an empty string. */
1042 ds_init_empty (struct string
*st
)
1044 st
->ss
= ss_empty ();
1048 /* Initializes ST with initial contents S. */
1050 ds_init_string (struct string
*st
, const struct string
*s
)
1052 ds_init_substring (st
, ds_ss (s
));
1055 /* Initializes ST with initial contents SS. */
1057 ds_init_substring (struct string
*st
, struct substring ss
)
1059 st
->capacity
= MAX (8, ss
.length
* 2);
1060 st
->ss
.string
= xmalloc (st
->capacity
+ 1);
1061 memcpy (st
->ss
.string
, ss
.string
, ss
.length
);
1062 st
->ss
.length
= ss
.length
;
1065 /* Initializes ST with initial contents S. */
1067 ds_init_cstr (struct string
*st
, const char *s
)
1069 ds_init_substring (st
, ss_cstr (s
));
1074 ds_destroy (struct string
*st
)
1078 ss_dealloc (&st
->ss
);
1079 st
->ss
.string
= NULL
;
1085 /* Swaps the contents of strings A and B. */
1087 ds_swap (struct string
*a
, struct string
*b
)
1089 struct string tmp
= *a
;
1094 /* Helper function for ds_register_pool. */
1096 free_string (void *st_
)
1098 struct string
*st
= st_
;
1102 /* Arranges for ST to be destroyed automatically as part of
1105 ds_register_pool (struct string
*st
, struct pool
*pool
)
1107 pool_register (pool
, free_string
, st
);
1110 /* Cancels the arrangement for ST to be destroyed automatically
1113 ds_unregister_pool (struct string
*st
, struct pool
*pool
)
1115 pool_unregister (pool
, st
);
1118 /* Copies SRC into DST.
1119 DST and SRC may be the same string. */
1121 ds_assign_string (struct string
*dst
, const struct string
*src
)
1123 ds_assign_substring (dst
, ds_ss (src
));
1126 /* Replaces DST by SS.
1127 SS may be a substring of DST. */
1129 ds_assign_substring (struct string
*dst
, struct substring ss
)
1131 dst
->ss
.length
= ss
.length
;
1132 ds_extend (dst
, ss
.length
);
1133 memmove (dst
->ss
.string
, ss
.string
, ss
.length
);
1136 /* Replaces DST by null-terminated string SRC. SRC may overlap
1139 ds_assign_cstr (struct string
*dst
, const char *src
)
1141 ds_assign_substring (dst
, ss_cstr (src
));
1144 /* Truncates ST to zero length. */
1146 ds_clear (struct string
*st
)
1151 /* Returns a substring that contains ST. */
1153 ds_ss (const struct string
*st
)
1158 /* Returns a substring that contains N bytes from ST
1159 starting at position START.
1161 If START is greater than or equal to the length of ST, then
1162 the substring will be the empty string. If START + N
1163 exceeds the length of ST, then the substring will only be
1164 ds_length(ST) - START bytes long. */
1166 ds_substr (const struct string
*st
, size_t start
, size_t n
)
1168 return ss_substr (ds_ss (st
), start
, n
);
1171 /* Returns a substring that contains the first N bytes in
1172 ST. If N exceeds the length of ST, then the substring will
1173 contain all of ST. */
1175 ds_head (const struct string
*st
, size_t n
)
1177 return ss_head (ds_ss (st
), n
);
1180 /* Returns a substring that contains the last N bytes in
1181 ST. If N exceeds the length of ST, then the substring will
1182 contain all of ST. */
1184 ds_tail (const struct string
*st
, size_t n
)
1186 return ss_tail (ds_ss (st
), n
);
1189 /* Ensures that ST can hold at least MIN_CAPACITY bytes plus a null
1192 ds_extend (struct string
*st
, size_t min_capacity
)
1194 if (min_capacity
> st
->capacity
)
1197 if (st
->capacity
< min_capacity
)
1198 st
->capacity
= 2 * min_capacity
;
1200 st
->ss
.string
= xrealloc (st
->ss
.string
, st
->capacity
+ 1);
1204 /* Shrink ST to the minimum capacity need to contain its content. */
1206 ds_shrink (struct string
*st
)
1208 if (st
->capacity
!= st
->ss
.length
)
1210 st
->capacity
= st
->ss
.length
;
1211 st
->ss
.string
= xrealloc (st
->ss
.string
, st
->capacity
+ 1);
1215 /* Truncates ST to at most LENGTH bytes long. */
1217 ds_truncate (struct string
*st
, size_t length
)
1219 ss_truncate (&st
->ss
, length
);
1222 /* Removes trailing bytes in TRIM_SET from ST.
1223 Returns number of bytes removed. */
1225 ds_rtrim (struct string
*st
, struct substring trim_set
)
1227 return ss_rtrim (&st
->ss
, trim_set
);
1230 /* Removes leading bytes in TRIM_SET from ST.
1231 Returns number of bytes removed. */
1233 ds_ltrim (struct string
*st
, struct substring trim_set
)
1235 size_t n
= ds_span (st
, trim_set
);
1237 ds_assign_substring (st
, ds_substr (st
, n
, SIZE_MAX
));
1241 /* Trims leading and trailing bytes in TRIM_SET from ST.
1242 Returns number of bytes removed. */
1244 ds_trim (struct string
*st
, struct substring trim_set
)
1246 size_t n
= ds_rtrim (st
, trim_set
);
1247 return n
+ ds_ltrim (st
, trim_set
);
1250 /* If the last byte in ST is C, removes it and returns true.
1251 Otherwise, returns false without modifying ST. */
1253 ds_chomp_byte (struct string
*st
, char c
)
1255 return ss_chomp_byte (&st
->ss
, c
);
1258 /* If ST ends with SUFFIX, removes it and returns true.
1259 Otherwise, returns false without modifying ST. */
1261 ds_chomp (struct string
*st
, struct substring suffix
)
1263 return ss_chomp (&st
->ss
, suffix
);
1266 /* Divides ST into tokens separated by any of the DELIMITERS.
1267 Each call replaces TOKEN by the next token in ST, or by an
1268 empty string if no tokens remain. Returns true if a token was
1269 obtained, false otherwise.
1271 Before the first call, initialize *SAVE_IDX to 0. Do not
1272 modify *SAVE_IDX between calls.
1274 ST divides into exactly one more tokens than it contains
1275 delimiters. That is, a delimiter at the start or end of ST or
1276 a pair of adjacent delimiters yields an empty token, and the
1277 empty string contains a single token. */
1279 ds_separate (const struct string
*st
, struct substring delimiters
,
1280 size_t *save_idx
, struct substring
*token
)
1282 return ss_separate (ds_ss (st
), delimiters
, save_idx
, token
);
1285 /* Divides ST into tokens separated by any of the DELIMITERS,
1286 merging adjacent delimiters so that the empty string is never
1287 produced as a token. Each call replaces TOKEN by the next
1288 token in ST, or by an empty string if no tokens remain.
1289 Returns true if a token was obtained, false otherwise.
1291 Before the first call, initialize *SAVE_IDX to 0. Do not
1292 modify *SAVE_IDX between calls. */
1294 ds_tokenize (const struct string
*st
, struct substring delimiters
,
1295 size_t *save_idx
, struct substring
*token
)
1297 return ss_tokenize (ds_ss (st
), delimiters
, save_idx
, token
);
1300 /* Pad ST on the right with copies of PAD until ST is at least
1301 LENGTH bytes in size. If ST is initially LENGTH
1302 bytes or longer, this is a no-op. */
1304 ds_rpad (struct string
*st
, size_t length
, char pad
)
1306 if (length
> st
->ss
.length
)
1307 ds_put_byte_multiple (st
, pad
, length
- st
->ss
.length
);
1310 /* Sets the length of ST to exactly NEW_LENGTH,
1311 either by truncating bytes from the end,
1312 or by padding on the right with PAD. */
1314 ds_set_length (struct string
*st
, size_t new_length
, char pad
)
1316 if (st
->ss
.length
< new_length
)
1317 ds_rpad (st
, new_length
, pad
);
1319 st
->ss
.length
= new_length
;
1322 /* Removes N bytes from ST starting at offset START. */
1324 ds_remove (struct string
*st
, size_t start
, size_t n
)
1326 if (n
> 0 && start
< st
->ss
.length
)
1328 if (st
->ss
.length
- start
<= n
)
1330 /* All bytes at or beyond START are deleted. */
1331 st
->ss
.length
= start
;
1335 /* Some bytes remain and must be shifted into
1337 memmove (st
->ss
.string
+ st
->ss
.length
,
1338 st
->ss
.string
+ st
->ss
.length
+ n
,
1339 st
->ss
.length
- start
- n
);
1345 /* There are no bytes to delete or no bytes at or
1346 beyond START, hence deletion is a no-op. */
1350 /* Returns true if ST is empty, false otherwise. */
1352 ds_is_empty (const struct string
*st
)
1354 return ss_is_empty (st
->ss
);
1357 /* Returns the length of ST. */
1359 ds_length (const struct string
*st
)
1361 return ss_length (ds_ss (st
));
1364 /* Returns the string data inside ST. */
1366 ds_data (const struct string
*st
)
1368 return ss_data (ds_ss (st
));
1371 /* Returns a pointer to the null terminator ST.
1372 This might not be an actual null byte unless ds_c_str() has
1373 been called since the last modification to ST. */
1375 ds_end (const struct string
*st
)
1377 return ss_end (ds_ss (st
));
1380 /* Returns the byte in position IDX in ST, as a value in the
1381 range of unsigned char. Returns EOF if IDX is out of the
1382 range of indexes for ST. */
1384 ds_at (const struct string
*st
, size_t idx
)
1386 return ss_at (ds_ss (st
), idx
);
1389 /* Returns the first byte in ST as a value in the range of
1390 unsigned char. Returns EOF if ST is the empty string. */
1392 ds_first (const struct string
*st
)
1394 return ss_first (ds_ss (st
));
1397 /* Returns the last byte in ST as a value in the range of
1398 unsigned char. Returns EOF if ST is the empty string. */
1400 ds_last (const struct string
*st
)
1402 return ss_last (ds_ss (st
));
1405 /* Returns true if ST ends with SUFFIX, false otherwise. */
1407 ds_ends_with (const struct string
*st
, struct substring suffix
)
1409 return ss_ends_with (st
->ss
, suffix
);
1412 /* Returns the number of consecutive bytes at the beginning
1413 of ST that are in SKIP_SET. */
1415 ds_span (const struct string
*st
, struct substring skip_set
)
1417 return ss_span (ds_ss (st
), skip_set
);
1420 /* Returns the number of consecutive bytes at the beginning
1421 of ST that are not in STOP_SET. */
1423 ds_cspan (const struct string
*st
, struct substring stop_set
)
1425 return ss_cspan (ds_ss (st
), stop_set
);
1428 /* Returns the position of the first occurrence of byte C in
1429 ST at or after position OFS, or SIZE_MAX if there is no such
1432 ds_find_byte (const struct string
*st
, char c
)
1434 return ss_find_byte (ds_ss (st
), c
);
1437 /* Compares A and B and returns a strcmp()-type comparison
1440 ds_compare (const struct string
*a
, const struct string
*b
)
1442 return ss_compare (ds_ss (a
), ds_ss (b
));
1445 /* Returns the position in ST that the byte at P occupies.
1446 P must point within ST or one past its end. */
1448 ds_pointer_to_position (const struct string
*st
, const char *p
)
1450 return ss_pointer_to_position (ds_ss (st
), p
);
1453 /* Allocates and returns a null-terminated string that contains
1456 ds_xstrdup (const struct string
*st
)
1458 return ss_xstrdup (ds_ss (st
));
1461 /* Returns the allocation size of ST. */
1463 ds_capacity (const struct string
*st
)
1465 return st
->capacity
;
1468 /* Returns the value of ST as a null-terminated string. */
1470 ds_cstr (const struct string
*st_
)
1472 struct string
*st
= CONST_CAST (struct string
*, st_
);
1473 if (st
->ss
.string
== NULL
)
1475 st
->ss
.string
[st
->ss
.length
] = '\0';
1476 return st
->ss
.string
;
1479 /* Returns the value of ST as a null-terminated string and then
1480 reinitialized ST as an empty string. The caller must free the
1481 returned string with free(). */
1483 ds_steal_cstr (struct string
*st
)
1485 char *s
= ds_cstr (st
);
1490 /* Reads bytes from STREAM and appends them to ST, stopping
1491 after MAX_LENGTH bytes, after appending a newline, or
1492 after an I/O error or end of file was encountered, whichever
1493 comes first. Returns true if at least one byte was added
1494 to ST, false if no bytes were read before an I/O error or
1495 end of file (or if MAX_LENGTH was 0).
1497 This function treats LF and CR LF sequences as new-line,
1498 translating each of them to a single '\n' in ST. */
1500 ds_read_line (struct string
*st
, FILE *stream
, size_t max_length
)
1504 for (length
= 0; length
< max_length
; length
++)
1506 int c
= getc (stream
);
1513 ds_put_byte (st
, c
);
1520 /* CR followed by LF is special: translate to \n. */
1521 ds_put_byte (st
, '\n');
1526 /* CR followed by anything else is just CR. */
1527 ds_put_byte (st
, '\r');
1535 ds_put_byte (st
, c
);
1542 /* Removes a comment introduced by `#' from ST,
1543 ignoring occurrences inside quoted strings. */
1545 remove_comment (struct string
*st
)
1550 for (cp
= ds_data (st
); cp
< ds_end (st
); cp
++)
1555 else if (*cp
== '\\')
1558 else if (*cp
== '\'' || *cp
== '"')
1560 else if (*cp
== '#')
1562 ds_truncate (st
, cp
- ds_cstr (st
));
1567 /* Reads a line from STREAM into ST, then preprocesses as follows:
1569 - Splices lines terminated with `\'.
1571 - Deletes comments introduced by `#' outside of single or double
1574 - Deletes trailing white space.
1576 Returns true if a line was successfully read, false on
1577 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
1578 incremented by the number of lines read. */
1580 ds_read_config_line (struct string
*st
, int *line_number
, FILE *stream
)
1585 if (!ds_read_line (st
, stream
, SIZE_MAX
))
1588 ds_rtrim (st
, ss_cstr (CC_SPACES
));
1590 while (ds_chomp_byte (st
, '\\'));
1592 remove_comment (st
);
1596 /* Attempts to read SIZE * N bytes from STREAM and append them
1598 Returns true if all the requested data was read, false otherwise. */
1600 ds_read_stream (struct string
*st
, size_t size
, size_t n
, FILE *stream
)
1604 size_t try_bytes
= xtimes (n
, size
);
1605 if (size_in_bounds_p (xsum (ds_length (st
), try_bytes
)))
1607 char *buffer
= ds_put_uninit (st
, try_bytes
);
1608 size_t got_bytes
= fread (buffer
, 1, try_bytes
, stream
);
1609 ds_truncate (st
, ds_length (st
) - (try_bytes
- got_bytes
));
1610 return got_bytes
== try_bytes
;
1622 /* Concatenates S onto ST. */
1624 ds_put_cstr (struct string
*st
, const char *s
)
1627 ds_put_substring (st
, ss_cstr (s
));
1630 /* Concatenates SS to ST. */
1632 ds_put_substring (struct string
*st
, struct substring ss
)
1635 memcpy (ds_put_uninit (st
, ss_length (ss
)), ss_data (ss
), ss_length (ss
));
1638 /* Returns ds_end(ST) and THEN increases the length by INCR. */
1640 ds_put_uninit (struct string
*st
, size_t incr
)
1643 ds_extend (st
, ds_length (st
) + incr
);
1645 st
->ss
.length
+= incr
;
1649 /* Moves the bytes in ST following offset OFS + OLD_LEN in ST to offset OFS +
1650 NEW_LEN and returns the byte at offset OFS. The first min(OLD_LEN, NEW_LEN)
1651 bytes at the returned position are unchanged; if NEW_LEN > OLD_LEN then the
1652 following NEW_LEN - OLD_LEN bytes are initially indeterminate.
1654 The intention is that the caller should write NEW_LEN bytes at the returned
1655 position, to effectively replace the OLD_LEN bytes previously at that
1658 ds_splice_uninit (struct string
*st
,
1659 size_t ofs
, size_t old_len
, size_t new_len
)
1661 if (new_len
!= old_len
)
1663 if (new_len
> old_len
)
1664 ds_extend (st
, ds_length (st
) + (new_len
- old_len
));
1666 assert (ds_length (st
) >= ofs
+ old_len
);
1668 memmove (ds_data (st
) + (ofs
+ new_len
),
1669 ds_data (st
) + (ofs
+ old_len
),
1670 ds_length (st
) - (ofs
+ old_len
));
1671 st
->ss
.length
+= new_len
- old_len
;
1673 return ds_data (st
) + ofs
;
1676 /* Formats FORMAT as a printf string and appends the result to ST. */
1678 ds_put_format (struct string
*st
, const char *format
, ...)
1682 va_start (args
, format
);
1683 ds_put_vformat (st
, format
, args
);
1687 /* Formats FORMAT as a printf string as if in the C locale and appends the result to ST. */
1689 ds_put_c_format (struct string
*st
, const char *format
, ...)
1693 va_start (args
, format
);
1694 ds_put_c_vformat (st
, format
, args
);
1698 /* Formats FORMAT as a printf string and appends the result to ST. */
1700 ds_put_vformat (struct string
*st
, const char *format
, va_list args_
)
1705 va_copy (args
, args_
);
1706 avail
= st
->ss
.string
!= NULL
? st
->capacity
- st
->ss
.length
+ 1 : 0;
1707 needed
= vsnprintf (st
->ss
.string
+ st
->ss
.length
, avail
, format
, args
);
1710 if (needed
>= avail
)
1712 va_copy (args
, args_
);
1713 vsnprintf (ds_put_uninit (st
, needed
), needed
+ 1, format
, args
);
1718 /* Some old libc's returned -1 when the destination string
1720 while (needed
== -1)
1722 ds_extend (st
, (st
->capacity
+ 1) * 2);
1723 avail
= st
->capacity
- st
->ss
.length
+ 1;
1725 va_copy (args
, args_
);
1726 needed
= vsnprintf (ds_end (st
), avail
, format
, args
);
1729 st
->ss
.length
+= needed
;
1733 /* Formats FORMAT as a printf string, as if in the C locale,
1734 and appends the result to ST. */
1736 ds_put_c_vformat (struct string
*st
, const char *format
, va_list args
)
1739 size_t len
= sizeof buf
;
1740 char *output
= c_vasnprintf (buf
, &len
, format
, args
);
1743 ds_put_cstr (st
, output
);
1749 /* Appends byte CH to ST. */
1751 ds_put_byte (struct string
*st
, int ch
)
1753 ds_put_uninit (st
, 1)[0] = ch
;
1756 /* Appends N copies of byte CH to ST. */
1758 ds_put_byte_multiple (struct string
*st
, int ch
, size_t n
)
1760 memset (ds_put_uninit (st
, n
), ch
, n
);
1763 /* Appends Unicode code point UC to ST in UTF-8 encoding. */
1765 ds_put_unichar (struct string
*st
, ucs4_t uc
)
1767 ds_extend (st
, ds_length (st
) + 6);
1768 st
->ss
.length
+= u8_uctomb (CHAR_CAST (uint8_t *, ds_end (st
)), uc
, 6);
1771 /* Appends N copies of S to ST. */
1773 ds_put_substring_multiple (struct string
*dst
, struct substring src
, size_t n
)
1775 char *p
= ds_put_uninit (dst
, n
* src
.length
);
1776 for (size_t i
= 0; i
< n
; i
++)
1778 memcpy (p
, src
.string
, src
.length
);
1783 /* If relocation has been enabled, replace ST,
1784 with its relocated version */
1786 ds_relocate (struct string
*st
)
1788 const char *orig
= ds_cstr (st
);
1789 const char *rel
= relocate (orig
);
1794 ds_put_cstr (st
, rel
);
1795 /* The documentation for relocate says that casting away const
1796 and then freeing is appropriate ... */
1797 free (CONST_CAST (char *, rel
));
1801 /* Returns a relocated version of S as a malloc()'d string that the caller must
1802 eventually free(). */
1804 relocate_clone (const char *s
)
1806 char *r
= CONST_CAST (char *, relocate (s
));
1807 return r
!= s
? r
: xstrdup (s
);
1810 /* Formats FORMAT in a printf()-like manner and returns a relocated version of
1811 it. The caller must eventually free() the returned string. */
1812 char * PRINTF_FORMAT (1, 2) MALLOC_LIKE
1813 relocate_format (const char *format
, ...)
1816 va_start (args
, format
);
1817 char *s
= xvasprintf (format
, args
);
1820 char *r
= CONST_CAST (char *, relocate (s
));
1827 /* Operations on uint8_t "strings" */
1829 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
1830 DST is truncated to DST_SIZE bytes or padded on the right with
1831 copies of PAD as needed. */
1833 u8_buf_copy_rpad (uint8_t *dst
, size_t dst_size
,
1834 const uint8_t *src
, size_t src_size
,
1837 if (src_size
>= dst_size
)
1838 memmove (dst
, src
, dst_size
);
1841 memmove (dst
, src
, src_size
);
1842 memset (&dst
[src_size
], pad
, dst_size
- src_size
);