2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include "string-utils.h"
16 #include "../macros.h"
18 enum star_glob_pattern_type_flags
{
19 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
= 0,
20 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
= 1,
21 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
= 2,
25 * Normalizes the star-only globbing pattern `pattern`, that is, crushes
26 * consecutive `*` characters into a single `*`, avoiding `\*`.
29 void strutils_normalize_star_glob_pattern(char *pattern
)
33 bool got_star
= false;
37 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
41 /* Avoid consecutive stars. */
48 /* Copy backslash character. */
57 /* Fall through default case. */
63 /* Copy single character. */
73 enum star_glob_pattern_type_flags
strutils_test_glob_pattern(const char *pattern
)
75 enum star_glob_pattern_type_flags ret
=
76 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
;
81 for (p
= pattern
; *p
!= '\0'; p
++) {
84 ret
= STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
87 ret
|= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
108 * Returns true if `pattern` is a star-only globbing pattern, that is,
109 * it contains at least one non-escaped `*`.
112 bool strutils_is_star_glob_pattern(const char *pattern
)
114 return strutils_test_glob_pattern(pattern
) &
115 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
119 * Returns true if `pattern` is a globbing pattern with a globbing,
120 * non-escaped star only at its very end.
123 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern
)
125 return strutils_test_glob_pattern(pattern
) &
126 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
130 * Unescapes the input string `input`, that is, in a `\x` sequence,
131 * removes `\`. If `only_char` is not 0, only this character is
135 char *strutils_unescape_string(const char *input
, char only_char
)
142 output
= zmalloc(strlen(input
) + 1);
147 for (i
= input
, o
= output
; *i
!= '\0'; i
++) {
150 if (only_char
&& i
[1] != only_char
) {
166 /* Copy single character. */
176 * Frees a null-terminated array of strings, including each contained
180 void strutils_free_null_terminated_array_of_strings(char **array
)
188 for (item
= array
; *item
; item
++) {
196 * Splits the input string `input` using the given delimiter `delim`.
198 * The return value is a dynamic pointer array that is assumed to be empty. The
199 * array must be discarded by the caller by invoking
200 * lttng_dynamic_pointer_array_reset().
202 * Empty substrings are part of the result. For example:
204 * Input: ,hello,,there,
212 * If `escape_delim` is true, then `\,`, where `,` is the delimiter,
213 * escapes the delimiter and is copied as `,` only in the resulting
214 * substring. For example:
216 * Input: hello\,world,zoom,\,hi
222 * Other characters are not escaped (this is the caller's job if
223 * needed). However they are considering during the parsing, that is,
224 * `\x`, where `x` is any character, is copied as is to the resulting
227 * Input: hello\,wo\rld\\,zoom\,
232 * If `escape_delim` is false, nothing at all is escaped, and `delim`,
233 * when found in `input`, is always a delimiter, e.g.:
235 * Input: hello\,world,zoom,\,hi
243 * Returns -1 if there's an error.
246 int strutils_split(const char *input
,
249 struct lttng_dynamic_pointer_array
*out_strings
)
253 size_t number_of_substrings
= 1;
254 size_t longest_substring_len
= 0;
259 assert(!(escape_delim
&& delim
== '\\'));
260 assert(delim
!= '\0');
261 lttng_dynamic_pointer_array_init(out_strings
, free
);
263 /* First pass: count the number of substrings. */
264 for (s
= input
, last
= input
- 1; *s
!= '\0'; s
++) {
265 if (escape_delim
&& *s
== '\\') {
266 /* Ignore following (escaped) character. */
277 size_t last_len
= s
- last
- 1;
279 number_of_substrings
++;
281 if (last_len
> longest_substring_len
) {
282 longest_substring_len
= last_len
;
287 if ((s
- last
- 1) > longest_substring_len
) {
288 longest_substring_len
= s
- last
- 1;
291 /* Second pass: actually split and copy substrings. */
292 for (at
= 0, s
= input
; at
< number_of_substrings
; at
++) {
295 char *substring
= zmalloc(longest_substring_len
+ 1);
301 ret
= lttng_dynamic_pointer_array_add_pointer(
302 out_strings
, substring
);
309 * Copy characters to substring until we find the next
310 * delimiter or the end of the input string.
312 for (ss
= s
, d
= substring
; *ss
!= '\0'; ss
++) {
313 if (escape_delim
&& *ss
== '\\') {
314 if (ss
[1] == delim
) {
316 * '\' followed by delimiter and
317 * we need to escape this ('\'
318 * won't be part of the
319 * resulting substring).
327 * Copy '\' and the following
338 } else if (*ss
== delim
) {
339 /* We're done with this substring. */
347 /* Next substring starts after the last delimiter. */
361 size_t strutils_array_of_strings_len(char * const *array
)
368 for (item
= array
; *item
; item
++) {
376 int strutils_append_str(char **s
, const char *append
)
380 size_t oldlen
= (old
== NULL
) ? 0 : strlen(old
);
381 size_t appendlen
= strlen(append
);
383 new = calloc(oldlen
+ appendlen
+ 1, 1);