2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include "string-utils.h"
14 #include "../macros.h"
16 enum star_glob_pattern_type_flags
{
17 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
= 0,
18 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
= 1,
19 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
= 2,
23 * Normalizes the star-only globbing pattern `pattern`, that is, crushes
24 * consecutive `*` characters into a single `*`, avoiding `\*`.
26 void strutils_normalize_star_glob_pattern(char *pattern
)
30 bool got_star
= false;
32 LTTNG_ASSERT(pattern
);
34 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
38 /* Avoid consecutive stars. */
45 /* Copy backslash character. */
54 /* Fall through default case. */
60 /* Copy single character. */
70 enum star_glob_pattern_type_flags
strutils_test_glob_pattern(const char *pattern
)
72 enum star_glob_pattern_type_flags ret
=
73 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
;
76 LTTNG_ASSERT(pattern
);
78 for (p
= pattern
; *p
!= '\0'; p
++) {
81 ret
= STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
84 ret
|= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
105 * Returns true if `pattern` is a star-only globbing pattern, that is,
106 * it contains at least one non-escaped `*`.
108 bool strutils_is_star_glob_pattern(const char *pattern
)
110 return strutils_test_glob_pattern(pattern
) &
111 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
115 * Returns true if `pattern` is a globbing pattern with a globbing,
116 * non-escaped star only at its very end.
118 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern
)
120 return strutils_test_glob_pattern(pattern
) &
121 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
125 * Unescapes the input string `input`, that is, in a `\x` sequence,
126 * removes `\`. If `only_char` is not 0, only this character is
129 char *strutils_unescape_string(const char *input
, char only_char
)
136 output
= zmalloc(strlen(input
) + 1);
141 for (i
= input
, o
= output
; *i
!= '\0'; i
++) {
144 if (only_char
&& i
[1] != only_char
) {
160 /* Copy single character. */
170 * Frees a null-terminated array of strings, including each contained
173 void strutils_free_null_terminated_array_of_strings(char **array
)
181 for (item
= array
; *item
; item
++) {
189 * Splits the input string `input` using the given delimiter `delim`.
191 * The return value is a dynamic pointer array that is assumed to be empty. The
192 * array must be discarded by the caller by invoking
193 * lttng_dynamic_pointer_array_reset().
195 * Empty substrings are part of the result. For example:
197 * Input: ,hello,,there,
205 * If `escape_delim` is true, then `\,`, where `,` is the delimiter,
206 * escapes the delimiter and is copied as `,` only in the resulting
207 * substring. For example:
209 * Input: hello\,world,zoom,\,hi
215 * Other characters are not escaped (this is the caller's job if
216 * needed). However they are considering during the parsing, that is,
217 * `\x`, where `x` is any character, is copied as is to the resulting
220 * Input: hello\,wo\rld\\,zoom\,
225 * If `escape_delim` is false, nothing at all is escaped, and `delim`,
226 * when found in `input`, is always a delimiter, e.g.:
228 * Input: hello\,world,zoom,\,hi
236 * Returns -1 if there's an error.
238 int strutils_split(const char *input
,
241 struct lttng_dynamic_pointer_array
*out_strings
)
245 size_t number_of_substrings
= 1;
246 size_t longest_substring_len
= 0;
251 LTTNG_ASSERT(!(escape_delim
&& delim
== '\\'));
252 LTTNG_ASSERT(delim
!= '\0');
253 lttng_dynamic_pointer_array_init(out_strings
, free
);
255 /* First pass: count the number of substrings. */
256 for (s
= input
, last
= input
- 1; *s
!= '\0'; s
++) {
257 if (escape_delim
&& *s
== '\\') {
258 /* Ignore following (escaped) character. */
269 size_t last_len
= s
- last
- 1;
271 number_of_substrings
++;
273 if (last_len
> longest_substring_len
) {
274 longest_substring_len
= last_len
;
279 if ((s
- last
- 1) > longest_substring_len
) {
280 longest_substring_len
= s
- last
- 1;
283 /* Second pass: actually split and copy substrings. */
284 for (at
= 0, s
= input
; at
< number_of_substrings
; at
++) {
287 char *substring
= zmalloc(longest_substring_len
+ 1);
293 ret
= lttng_dynamic_pointer_array_add_pointer(
294 out_strings
, substring
);
301 * Copy characters to substring until we find the next
302 * delimiter or the end of the input string.
304 for (ss
= s
, d
= substring
; *ss
!= '\0'; ss
++) {
305 if (escape_delim
&& *ss
== '\\') {
306 if (ss
[1] == delim
) {
308 * '\' followed by delimiter and
309 * we need to escape this ('\'
310 * won't be part of the
311 * resulting substring).
319 * Copy '\' and the following
330 } else if (*ss
== delim
) {
331 /* We're done with this substring. */
339 /* Next substring starts after the last delimiter. */
352 size_t strutils_array_of_strings_len(char * const *array
)
359 for (item
= array
; *item
; item
++) {