2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
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 * Normalizes the star-only globbing pattern `pattern`, that is, crushes
25 * consecutive `*` characters into a single `*`, avoiding `\*`.
28 void strutils_normalize_star_glob_pattern(char *pattern
)
32 bool got_star
= false;
36 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
40 /* Avoid consecutive stars. */
47 /* Copy backslash character. */
56 /* Fall through default case. */
62 /* Copy single character. */
72 enum star_glob_pattern_type_flags
strutils_test_glob_pattern(const char *pattern
)
74 enum star_glob_pattern_type_flags ret
=
75 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
;
80 for (p
= pattern
; *p
!= '\0'; p
++) {
83 ret
= STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
86 ret
|= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
107 * Returns true if `pattern` is a star-only globbing pattern, that is,
108 * it contains at least one non-escaped `*`.
111 bool strutils_is_star_glob_pattern(const char *pattern
)
113 return strutils_test_glob_pattern(pattern
) &
114 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
118 * Returns true if `pattern` is a globbing pattern with a globbing,
119 * non-escaped star only at its very end.
122 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern
)
124 return strutils_test_glob_pattern(pattern
) &
125 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
129 * Unescapes the input string `input`, that is, in a `\x` sequence,
130 * removes `\`. If `only_char` is not 0, only this character is
134 char *strutils_unescape_string(const char *input
, char only_char
)
141 output
= zmalloc(strlen(input
) + 1);
146 for (i
= input
, o
= output
; *i
!= '\0'; i
++) {
149 if (only_char
&& i
[1] != only_char
) {
165 /* Copy single character. */
175 * Frees a null-terminated array of strings, including each contained
179 void strutils_free_null_terminated_array_of_strings(char **array
)
187 for (item
= array
; *item
; item
++) {
195 * Splits the input string `input` using the given delimiter `delim`.
197 * The return value is a dynamic pointer array that is assumed to be empty. The
198 * array must be discarded by the caller by invoking
199 * lttng_dynamic_pointer_array_reset().
201 * Empty substrings are part of the result. For example:
203 * Input: ,hello,,there,
211 * If `escape_delim` is true, then `\,`, where `,` is the delimiter,
212 * escapes the delimiter and is copied as `,` only in the resulting
213 * substring. For example:
215 * Input: hello\,world,zoom,\,hi
221 * Other characters are not escaped (this is the caller's job if
222 * needed). However they are considering during the parsing, that is,
223 * `\x`, where `x` is any character, is copied as is to the resulting
226 * Input: hello\,wo\rld\\,zoom\,
231 * If `escape_delim` is false, nothing at all is escaped, and `delim`,
232 * when found in `input`, is always a delimiter, e.g.:
234 * Input: hello\,world,zoom,\,hi
242 * Returns -1 if there's an error.
245 int strutils_split(const char *input
,
248 struct lttng_dynamic_pointer_array
*out_strings
)
252 size_t number_of_substrings
= 1;
253 size_t longest_substring_len
= 0;
258 assert(!(escape_delim
&& delim
== '\\'));
259 assert(delim
!= '\0');
260 lttng_dynamic_pointer_array_init(out_strings
, free
);
262 /* First pass: count the number of substrings. */
263 for (s
= input
, last
= input
- 1; *s
!= '\0'; s
++) {
264 if (escape_delim
&& *s
== '\\') {
265 /* Ignore following (escaped) character. */
276 size_t last_len
= s
- last
- 1;
278 number_of_substrings
++;
280 if (last_len
> longest_substring_len
) {
281 longest_substring_len
= last_len
;
286 if ((s
- last
- 1) > longest_substring_len
) {
287 longest_substring_len
= s
- last
- 1;
290 /* Second pass: actually split and copy substrings. */
291 for (at
= 0, s
= input
; at
< number_of_substrings
; at
++) {
294 char *substring
= zmalloc(longest_substring_len
+ 1);
300 ret
= lttng_dynamic_pointer_array_add_pointer(
301 out_strings
, substring
);
308 * Copy characters to substring until we find the next
309 * delimiter or the end of the input string.
311 for (ss
= s
, d
= substring
; *ss
!= '\0'; ss
++) {
312 if (escape_delim
&& *ss
== '\\') {
313 if (ss
[1] == delim
) {
315 * '\' followed by delimiter and
316 * we need to escape this ('\'
317 * won't be part of the
318 * resulting substring).
326 * Copy '\' and the following
337 } else if (*ss
== delim
) {
338 /* We're done with this substring. */
346 /* Next substring starts after the last delimiter. */
360 size_t strutils_array_of_strings_len(char * const *array
)
367 for (item
= array
; *item
; item
++) {