2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <type_traits>
16 #include "string-utils.h"
17 #include "../macros.h"
19 enum star_glob_pattern_type_flags
{
20 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
= 0,
21 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
= 1,
22 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
= 2,
26 star_glob_pattern_type_flags
&operator|=(star_glob_pattern_type_flags
&l
,
27 star_glob_pattern_type_flags r
)
29 using T
= std::underlying_type
<star_glob_pattern_type_flags
>::type
;
30 l
= static_cast<star_glob_pattern_type_flags
> (
31 static_cast<T
> (l
) | static_cast<T
> (r
));
36 * Normalizes the star-only globbing pattern `pattern`, that is, crushes
37 * consecutive `*` characters into a single `*`, avoiding `\*`.
39 void strutils_normalize_star_glob_pattern(char *pattern
)
43 bool got_star
= false;
45 LTTNG_ASSERT(pattern
);
47 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
51 /* Avoid consecutive stars. */
58 /* Copy backslash character. */
67 /* Fall through default case. */
73 /* Copy single character. */
83 enum star_glob_pattern_type_flags
strutils_test_glob_pattern(const char *pattern
)
85 enum star_glob_pattern_type_flags ret
=
86 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
;
89 LTTNG_ASSERT(pattern
);
91 for (p
= pattern
; *p
!= '\0'; p
++) {
94 ret
= STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
97 ret
|= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
118 * Returns true if `pattern` is a star-only globbing pattern, that is,
119 * it contains at least one non-escaped `*`.
121 bool strutils_is_star_glob_pattern(const char *pattern
)
123 return strutils_test_glob_pattern(pattern
) &
124 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
128 * Returns true if `pattern` is a globbing pattern with a globbing,
129 * non-escaped star only at its very end.
131 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern
)
133 return strutils_test_glob_pattern(pattern
) &
134 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
138 * Unescapes the input string `input`, that is, in a `\x` sequence,
139 * removes `\`. If `only_char` is not 0, only this character is
142 char *strutils_unescape_string(const char *input
, char only_char
)
149 output
= (char *) zmalloc(strlen(input
) + 1);
154 for (i
= input
, o
= output
; *i
!= '\0'; i
++) {
157 if (only_char
&& i
[1] != only_char
) {
173 /* Copy single character. */
183 * Frees a null-terminated array of strings, including each contained
186 void strutils_free_null_terminated_array_of_strings(char **array
)
194 for (item
= array
; *item
; item
++) {
202 * Splits the input string `input` using the given delimiter `delim`.
204 * The return value is a dynamic pointer array that is assumed to be empty. The
205 * array must be discarded by the caller by invoking
206 * lttng_dynamic_pointer_array_reset().
208 * Empty substrings are part of the result. For example:
210 * Input: ,hello,,there,
218 * If `escape_delim` is true, then `\,`, where `,` is the delimiter,
219 * escapes the delimiter and is copied as `,` only in the resulting
220 * substring. For example:
222 * Input: hello\,world,zoom,\,hi
228 * Other characters are not escaped (this is the caller's job if
229 * needed). However they are considering during the parsing, that is,
230 * `\x`, where `x` is any character, is copied as is to the resulting
233 * Input: hello\,wo\rld\\,zoom\,
238 * If `escape_delim` is false, nothing at all is escaped, and `delim`,
239 * when found in `input`, is always a delimiter, e.g.:
241 * Input: hello\,world,zoom,\,hi
249 * Returns -1 if there's an error.
251 int strutils_split(const char *input
,
254 struct lttng_dynamic_pointer_array
*out_strings
)
258 size_t number_of_substrings
= 1;
259 size_t longest_substring_len
= 0;
264 LTTNG_ASSERT(!(escape_delim
&& delim
== '\\'));
265 LTTNG_ASSERT(delim
!= '\0');
266 lttng_dynamic_pointer_array_init(out_strings
, free
);
268 /* First pass: count the number of substrings. */
269 for (s
= input
, last
= input
- 1; *s
!= '\0'; s
++) {
270 if (escape_delim
&& *s
== '\\') {
271 /* Ignore following (escaped) character. */
282 size_t last_len
= s
- last
- 1;
284 number_of_substrings
++;
286 if (last_len
> longest_substring_len
) {
287 longest_substring_len
= last_len
;
292 if ((s
- last
- 1) > longest_substring_len
) {
293 longest_substring_len
= s
- last
- 1;
296 /* Second pass: actually split and copy substrings. */
297 for (at
= 0, s
= input
; at
< number_of_substrings
; at
++) {
300 char *substring
= (char *) zmalloc(longest_substring_len
+ 1);
306 ret
= lttng_dynamic_pointer_array_add_pointer(
307 out_strings
, substring
);
314 * Copy characters to substring until we find the next
315 * delimiter or the end of the input string.
317 for (ss
= s
, d
= substring
; *ss
!= '\0'; ss
++) {
318 if (escape_delim
&& *ss
== '\\') {
319 if (ss
[1] == delim
) {
321 * '\' followed by delimiter and
322 * we need to escape this ('\'
323 * won't be part of the
324 * resulting substring).
332 * Copy '\' and the following
343 } else if (*ss
== delim
) {
344 /* We're done with this substring. */
352 /* Next substring starts after the last delimiter. */
365 size_t strutils_array_of_strings_len(char * const *array
)
372 for (item
= array
; *item
; item
++) {
379 int strutils_append_str(char **s
, const char *append
)
383 size_t oldlen
= (old
== NULL
) ? 0 : strlen(old
);
384 size_t appendlen
= strlen(append
);
386 new_str
= (char *) zmalloc(oldlen
+ appendlen
+ 1);
391 strcpy(new_str
, old
);
393 strcat(new_str
, append
);