2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <type_traits>
14 #include "string-utils.h"
15 #include "../macros.h"
17 enum star_glob_pattern_type_flags
{
18 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
= 0,
19 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
= 1,
20 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
= 2,
24 star_glob_pattern_type_flags
&operator|=(star_glob_pattern_type_flags
&l
,
25 star_glob_pattern_type_flags r
)
27 using T
= std::underlying_type
<star_glob_pattern_type_flags
>::type
;
28 l
= static_cast<star_glob_pattern_type_flags
> (
29 static_cast<T
> (l
) | static_cast<T
> (r
));
34 * Normalizes the star-only globbing pattern `pattern`, that is, crushes
35 * consecutive `*` characters into a single `*`, avoiding `\*`.
37 void strutils_normalize_star_glob_pattern(char *pattern
)
41 bool got_star
= false;
43 LTTNG_ASSERT(pattern
);
45 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
49 /* Avoid consecutive stars. */
56 /* Copy backslash character. */
65 /* Fall through default case. */
71 /* Copy single character. */
81 enum star_glob_pattern_type_flags
strutils_test_glob_pattern(const char *pattern
)
83 enum star_glob_pattern_type_flags ret
=
84 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
;
87 LTTNG_ASSERT(pattern
);
89 for (p
= pattern
; *p
!= '\0'; p
++) {
92 ret
= STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
95 ret
|= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
116 * Returns true if `pattern` is a star-only globbing pattern, that is,
117 * it contains at least one non-escaped `*`.
119 bool strutils_is_star_glob_pattern(const char *pattern
)
121 return strutils_test_glob_pattern(pattern
) &
122 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
126 * Returns true if `pattern` is a globbing pattern with a globbing,
127 * non-escaped star only at its very end.
129 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern
)
131 return strutils_test_glob_pattern(pattern
) &
132 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
136 * Unescapes the input string `input`, that is, in a `\x` sequence,
137 * removes `\`. If `only_char` is not 0, only this character is
140 char *strutils_unescape_string(const char *input
, char only_char
)
147 output
= (char *) zmalloc(strlen(input
) + 1);
152 for (i
= input
, o
= output
; *i
!= '\0'; i
++) {
155 if (only_char
&& i
[1] != only_char
) {
171 /* Copy single character. */
181 * Frees a null-terminated array of strings, including each contained
184 void strutils_free_null_terminated_array_of_strings(char **array
)
192 for (item
= array
; *item
; item
++) {
200 * Splits the input string `input` using the given delimiter `delim`.
202 * The return value is a dynamic pointer array that is assumed to be empty. The
203 * array must be discarded by the caller by invoking
204 * lttng_dynamic_pointer_array_reset().
206 * Empty substrings are part of the result. For example:
208 * Input: ,hello,,there,
216 * If `escape_delim` is true, then `\,`, where `,` is the delimiter,
217 * escapes the delimiter and is copied as `,` only in the resulting
218 * substring. For example:
220 * Input: hello\,world,zoom,\,hi
226 * Other characters are not escaped (this is the caller's job if
227 * needed). However they are considering during the parsing, that is,
228 * `\x`, where `x` is any character, is copied as is to the resulting
231 * Input: hello\,wo\rld\\,zoom\,
236 * If `escape_delim` is false, nothing at all is escaped, and `delim`,
237 * when found in `input`, is always a delimiter, e.g.:
239 * Input: hello\,world,zoom,\,hi
247 * Returns -1 if there's an error.
249 int strutils_split(const char *input
,
252 struct lttng_dynamic_pointer_array
*out_strings
)
256 size_t number_of_substrings
= 1;
257 size_t longest_substring_len
= 0;
262 LTTNG_ASSERT(!(escape_delim
&& delim
== '\\'));
263 LTTNG_ASSERT(delim
!= '\0');
264 lttng_dynamic_pointer_array_init(out_strings
, free
);
266 /* First pass: count the number of substrings. */
267 for (s
= input
, last
= input
- 1; *s
!= '\0'; s
++) {
268 if (escape_delim
&& *s
== '\\') {
269 /* Ignore following (escaped) character. */
280 size_t last_len
= s
- last
- 1;
282 number_of_substrings
++;
284 if (last_len
> longest_substring_len
) {
285 longest_substring_len
= last_len
;
290 if ((s
- last
- 1) > longest_substring_len
) {
291 longest_substring_len
= s
- last
- 1;
294 /* Second pass: actually split and copy substrings. */
295 for (at
= 0, s
= input
; at
< number_of_substrings
; at
++) {
298 char *substring
= (char *) zmalloc(longest_substring_len
+ 1);
304 ret
= lttng_dynamic_pointer_array_add_pointer(
305 out_strings
, substring
);
312 * Copy characters to substring until we find the next
313 * delimiter or the end of the input string.
315 for (ss
= s
, d
= substring
; *ss
!= '\0'; ss
++) {
316 if (escape_delim
&& *ss
== '\\') {
317 if (ss
[1] == delim
) {
319 * '\' followed by delimiter and
320 * we need to escape this ('\'
321 * won't be part of the
322 * resulting substring).
330 * Copy '\' and the following
341 } else if (*ss
== delim
) {
342 /* We're done with this substring. */
350 /* Next substring starts after the last delimiter. */
363 size_t strutils_array_of_strings_len(char * const *array
)
370 for (item
= array
; *item
; item
++) {