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 `\*`.
27 void strutils_normalize_star_glob_pattern(char *pattern
)
31 bool got_star
= false;
33 LTTNG_ASSERT(pattern
);
35 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
39 /* Avoid consecutive stars. */
46 /* Copy backslash character. */
55 /* Fall through default case. */
61 /* Copy single character. */
71 enum star_glob_pattern_type_flags
strutils_test_glob_pattern(const char *pattern
)
73 enum star_glob_pattern_type_flags ret
=
74 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
;
77 LTTNG_ASSERT(pattern
);
79 for (p
= pattern
; *p
!= '\0'; p
++) {
82 ret
= STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
85 ret
|= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
106 * Returns true if `pattern` is a star-only globbing pattern, that is,
107 * it contains at least one non-escaped `*`.
110 bool strutils_is_star_glob_pattern(const char *pattern
)
112 return strutils_test_glob_pattern(pattern
) &
113 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
117 * Returns true if `pattern` is a globbing pattern with a globbing,
118 * non-escaped star only at its very end.
121 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern
)
123 return strutils_test_glob_pattern(pattern
) &
124 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
128 * Unescapes the input string `input`, that is, in a `\x` sequence,
129 * removes `\`. If `only_char` is not 0, only this character is
133 char *strutils_unescape_string(const char *input
, char only_char
)
140 output
= zmalloc(strlen(input
) + 1);
145 for (i
= input
, o
= output
; *i
!= '\0'; i
++) {
148 if (only_char
&& i
[1] != only_char
) {
164 /* Copy single character. */
174 * Frees a null-terminated array of strings, including each contained
178 void strutils_free_null_terminated_array_of_strings(char **array
)
186 for (item
= array
; *item
; item
++) {
194 * Splits the input string `input` using the given delimiter `delim`.
196 * The return value is a dynamic pointer array that is assumed to be empty. The
197 * array must be discarded by the caller by invoking
198 * lttng_dynamic_pointer_array_reset().
200 * Empty substrings are part of the result. For example:
202 * Input: ,hello,,there,
210 * If `escape_delim` is true, then `\,`, where `,` is the delimiter,
211 * escapes the delimiter and is copied as `,` only in the resulting
212 * substring. For example:
214 * Input: hello\,world,zoom,\,hi
220 * Other characters are not escaped (this is the caller's job if
221 * needed). However they are considering during the parsing, that is,
222 * `\x`, where `x` is any character, is copied as is to the resulting
225 * Input: hello\,wo\rld\\,zoom\,
230 * If `escape_delim` is false, nothing at all is escaped, and `delim`,
231 * when found in `input`, is always a delimiter, e.g.:
233 * Input: hello\,world,zoom,\,hi
241 * Returns -1 if there's an error.
244 int strutils_split(const char *input
,
247 struct lttng_dynamic_pointer_array
*out_strings
)
251 size_t number_of_substrings
= 1;
252 size_t longest_substring_len
= 0;
257 LTTNG_ASSERT(!(escape_delim
&& delim
== '\\'));
258 LTTNG_ASSERT(delim
!= '\0');
259 lttng_dynamic_pointer_array_init(out_strings
, free
);
261 /* First pass: count the number of substrings. */
262 for (s
= input
, last
= input
- 1; *s
!= '\0'; s
++) {
263 if (escape_delim
&& *s
== '\\') {
264 /* Ignore following (escaped) character. */
275 size_t last_len
= s
- last
- 1;
277 number_of_substrings
++;
279 if (last_len
> longest_substring_len
) {
280 longest_substring_len
= last_len
;
285 if ((s
- last
- 1) > longest_substring_len
) {
286 longest_substring_len
= s
- last
- 1;
289 /* Second pass: actually split and copy substrings. */
290 for (at
= 0, s
= input
; at
< number_of_substrings
; at
++) {
293 char *substring
= zmalloc(longest_substring_len
+ 1);
299 ret
= lttng_dynamic_pointer_array_add_pointer(
300 out_strings
, substring
);
307 * Copy characters to substring until we find the next
308 * delimiter or the end of the input string.
310 for (ss
= s
, d
= substring
; *ss
!= '\0'; ss
++) {
311 if (escape_delim
&& *ss
== '\\') {
312 if (ss
[1] == delim
) {
314 * '\' followed by delimiter and
315 * we need to escape this ('\'
316 * won't be part of the
317 * resulting substring).
325 * Copy '\' and the following
336 } else if (*ss
== delim
) {
337 /* We're done with this substring. */
345 /* Next substring starts after the last delimiter. */
359 size_t strutils_array_of_strings_len(char * const *array
)
366 for (item
= array
; *item
; item
++) {