2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
17 #include "string-utils.h"
18 #include "../macros.h"
20 enum star_glob_pattern_type_flags
{
21 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
= 0,
22 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
= 1,
23 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
= 2,
27 * Normalizes the star-only globbing pattern `pattern`, that is, crushes
28 * consecutive `*` characters into a single `*`, avoiding `\*`.
31 void strutils_normalize_star_glob_pattern(char *pattern
)
35 bool got_star
= false;
39 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
43 /* Avoid consecutive stars. */
50 /* Copy backslash character. */
59 /* Fall through default case. */
65 /* Copy single character. */
75 enum star_glob_pattern_type_flags
strutils_test_glob_pattern(const char *pattern
)
77 enum star_glob_pattern_type_flags ret
=
78 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
;
83 for (p
= pattern
; *p
!= '\0'; p
++) {
86 ret
= STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
89 ret
|= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
110 * Returns true if `pattern` is a star-only globbing pattern, that is,
111 * it contains at least one non-escaped `*`.
114 bool strutils_is_star_glob_pattern(const char *pattern
)
116 return strutils_test_glob_pattern(pattern
) &
117 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
121 * Returns true if `pattern` is a globbing pattern with a globbing,
122 * non-escaped star only at its very end.
125 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern
)
127 return strutils_test_glob_pattern(pattern
) &
128 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
132 * Unescapes the input string `input`, that is, in a `\x` sequence,
133 * removes `\`. If `only_char` is not 0, only this character is
137 char *strutils_unescape_string(const char *input
, char only_char
)
144 output
= zmalloc(strlen(input
) + 1);
149 for (i
= input
, o
= output
; *i
!= '\0'; i
++) {
152 if (only_char
&& i
[1] != only_char
) {
168 /* Copy single character. */
178 * Frees a null-terminated array of strings, including each contained
182 void strutils_free_null_terminated_array_of_strings(char **array
)
190 for (item
= array
; *item
; item
++) {
198 * Splits the input string `input` using the given delimiter `delim`.
200 * The return value is a dynamic pointer array that is assumed to be empty. The
201 * array must be discarded by the caller by invoking
202 * lttng_dynamic_pointer_array_reset().
204 * Empty substrings are part of the result. For example:
206 * Input: ,hello,,there,
214 * If `escape_delim` is true, then `\,`, where `,` is the delimiter,
215 * escapes the delimiter and is copied as `,` only in the resulting
216 * substring. For example:
218 * Input: hello\,world,zoom,\,hi
224 * Other characters are not escaped (this is the caller's job if
225 * needed). However they are considering during the parsing, that is,
226 * `\x`, where `x` is any character, is copied as is to the resulting
229 * Input: hello\,wo\rld\\,zoom\,
234 * If `escape_delim` is false, nothing at all is escaped, and `delim`,
235 * when found in `input`, is always a delimiter, e.g.:
237 * Input: hello\,world,zoom,\,hi
245 * Returns -1 if there's an error.
248 int strutils_split(const char *input
,
251 struct lttng_dynamic_pointer_array
*out_strings
)
255 size_t number_of_substrings
= 1;
256 size_t longest_substring_len
= 0;
261 assert(!(escape_delim
&& delim
== '\\'));
262 assert(delim
!= '\0');
263 lttng_dynamic_pointer_array_init(out_strings
, free
);
265 /* First pass: count the number of substrings. */
266 for (s
= input
, last
= input
- 1; *s
!= '\0'; s
++) {
267 if (escape_delim
&& *s
== '\\') {
268 /* Ignore following (escaped) character. */
279 size_t last_len
= s
- last
- 1;
281 number_of_substrings
++;
283 if (last_len
> longest_substring_len
) {
284 longest_substring_len
= last_len
;
289 if ((s
- last
- 1) > longest_substring_len
) {
290 longest_substring_len
= s
- last
- 1;
293 /* Second pass: actually split and copy substrings. */
294 for (at
= 0, s
= input
; at
< number_of_substrings
; at
++) {
297 char *substring
= zmalloc(longest_substring_len
+ 1);
303 ret
= lttng_dynamic_pointer_array_add_pointer(
304 out_strings
, substring
);
311 * Copy characters to substring until we find the next
312 * delimiter or the end of the input string.
314 for (ss
= s
, d
= substring
; *ss
!= '\0'; ss
++) {
315 if (escape_delim
&& *ss
== '\\') {
316 if (ss
[1] == delim
) {
318 * '\' followed by delimiter and
319 * we need to escape this ('\'
320 * won't be part of the
321 * resulting substring).
329 * Copy '\' and the following
340 } else if (*ss
== delim
) {
341 /* We're done with this substring. */
349 /* Next substring starts after the last delimiter. */
363 size_t strutils_array_of_strings_len(char * const *array
)
370 for (item
= array
; *item
; item
++) {
378 int strutils_append_str(char **s
, const char *append
)
382 size_t oldlen
= (old
== NULL
) ? 0 : strlen(old
);
383 size_t appendlen
= strlen(append
);
385 new = calloc(oldlen
+ appendlen
+ 1, 1);
399 int strutils_appendf(char **s
, const char *fmt
, ...)
402 size_t oldlen
= (*s
) ? strlen(*s
) : 0;
406 /* Compute length of formatted string we append. */
408 ret
= vsnprintf(NULL
, 0, fmt
, args
);
415 /* Allocate space for old string + new string + \0. */
416 new_str
= calloc(oldlen
+ ret
+ 1, 1);
422 /* Copy old string, if there was one. */
427 /* Format new string in-place. */
429 ret
= vsprintf(&new_str
[oldlen
], fmt
, args
);