2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
13 #include <type_traits>
18 #include "string-utils.hpp"
19 #include "../macros.hpp"
21 enum star_glob_pattern_type_flags
{
22 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
= 0,
23 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
= 1,
24 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
= 2,
28 star_glob_pattern_type_flags
&operator|=(star_glob_pattern_type_flags
&l
,
29 star_glob_pattern_type_flags r
)
31 using T
= std::underlying_type
<star_glob_pattern_type_flags
>::type
;
32 l
= static_cast<star_glob_pattern_type_flags
> (
33 static_cast<T
> (l
) | static_cast<T
> (r
));
38 * Normalizes the star-only globbing pattern `pattern`, that is, crushes
39 * consecutive `*` characters into a single `*`, avoiding `\*`.
41 void strutils_normalize_star_glob_pattern(char *pattern
)
45 bool got_star
= false;
47 LTTNG_ASSERT(pattern
);
49 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
53 /* Avoid consecutive stars. */
60 /* Copy backslash character. */
75 /* Copy single character. */
85 enum star_glob_pattern_type_flags
strutils_test_glob_pattern(const char *pattern
)
87 enum star_glob_pattern_type_flags ret
=
88 STAR_GLOB_PATTERN_TYPE_FLAG_NONE
;
91 LTTNG_ASSERT(pattern
);
93 for (p
= pattern
; *p
!= '\0'; p
++) {
96 ret
= STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
99 ret
|= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
120 * Returns true if `pattern` is a star-only globbing pattern, that is,
121 * it contains at least one non-escaped `*`.
123 bool strutils_is_star_glob_pattern(const char *pattern
)
125 return strutils_test_glob_pattern(pattern
) &
126 STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN
;
130 * Returns true if `pattern` is a globbing pattern with a globbing,
131 * non-escaped star only at its very end.
133 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern
)
135 return strutils_test_glob_pattern(pattern
) &
136 STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY
;
140 * Unescapes the input string `input`, that is, in a `\x` sequence,
141 * removes `\`. If `only_char` is not 0, only this character is
144 char *strutils_unescape_string(const char *input
, char only_char
)
151 output
= (char *) zmalloc(strlen(input
) + 1);
156 for (i
= input
, o
= output
; *i
!= '\0'; i
++) {
159 if (only_char
&& i
[1] != only_char
) {
175 /* Copy single character. */
185 * Frees a null-terminated array of strings, including each contained
188 void strutils_free_null_terminated_array_of_strings(char **array
)
196 for (item
= array
; *item
; item
++) {
204 * Splits the input string `input` using the given delimiter `delim`.
206 * The return value is a dynamic pointer array that is assumed to be empty. The
207 * array must be discarded by the caller by invoking
208 * lttng_dynamic_pointer_array_reset().
210 * Empty substrings are part of the result. For example:
212 * Input: ,hello,,there,
220 * If `escape_delim` is true, then `\,`, where `,` is the delimiter,
221 * escapes the delimiter and is copied as `,` only in the resulting
222 * substring. For example:
224 * Input: hello\,world,zoom,\,hi
230 * Other characters are not escaped (this is the caller's job if
231 * needed). However they are considering during the parsing, that is,
232 * `\x`, where `x` is any character, is copied as is to the resulting
235 * Input: hello\,wo\rld\\,zoom\,
240 * If `escape_delim` is false, nothing at all is escaped, and `delim`,
241 * when found in `input`, is always a delimiter, e.g.:
243 * Input: hello\,world,zoom,\,hi
251 * Returns -1 if there's an error.
253 int strutils_split(const char *input
,
256 struct lttng_dynamic_pointer_array
*out_strings
)
260 size_t number_of_substrings
= 1;
261 size_t longest_substring_len
= 0;
266 LTTNG_ASSERT(!(escape_delim
&& delim
== '\\'));
267 LTTNG_ASSERT(delim
!= '\0');
268 lttng_dynamic_pointer_array_init(out_strings
, free
);
270 /* First pass: count the number of substrings. */
271 for (s
= input
, last
= input
- 1; *s
!= '\0'; s
++) {
272 if (escape_delim
&& *s
== '\\') {
273 /* Ignore following (escaped) character. */
284 size_t last_len
= s
- last
- 1;
286 number_of_substrings
++;
288 if (last_len
> longest_substring_len
) {
289 longest_substring_len
= last_len
;
294 if ((s
- last
- 1) > longest_substring_len
) {
295 longest_substring_len
= s
- last
- 1;
298 /* Second pass: actually split and copy substrings. */
299 for (at
= 0, s
= input
; at
< number_of_substrings
; at
++) {
302 char *substring
= (char *) zmalloc(longest_substring_len
+ 1);
308 ret
= lttng_dynamic_pointer_array_add_pointer(
309 out_strings
, substring
);
316 * Copy characters to substring until we find the next
317 * delimiter or the end of the input string.
319 for (ss
= s
, d
= substring
; *ss
!= '\0'; ss
++) {
320 if (escape_delim
&& *ss
== '\\') {
321 if (ss
[1] == delim
) {
323 * '\' followed by delimiter and
324 * we need to escape this ('\'
325 * won't be part of the
326 * resulting substring).
334 * Copy '\' and the following
345 } else if (*ss
== delim
) {
346 /* We're done with this substring. */
354 /* Next substring starts after the last delimiter. */
367 size_t strutils_array_of_strings_len(char * const *array
)
374 for (item
= array
; *item
; item
++) {
381 int strutils_append_str(char **s
, const char *append
)
385 size_t oldlen
= (old
== NULL
) ? 0 : strlen(old
);
386 size_t appendlen
= strlen(append
);
388 new_str
= (char *) zmalloc(oldlen
+ appendlen
+ 1);
393 strcpy(new_str
, old
);
395 strcat(new_str
, append
);
401 int strutils_appendf(char **s
, const char *fmt
, ...)
404 size_t oldlen
= (*s
) ? strlen(*s
) : 0;
408 /* Compute length of formatted string we append. */
410 ret
= vsnprintf(NULL
, 0, fmt
, args
);
417 /* Allocate space for old string + new string + \0. */
418 new_str
= (char *) zmalloc(oldlen
+ ret
+ 1);
424 /* Copy old string, if there was one. */
429 /* Format new string in-place. */
431 ret
= vsprintf(&new_str
[oldlen
], fmt
, args
);