#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
-#include <assert.h>
#include "string-utils.h"
#include "../macros.h"
* Normalizes the star-only globbing pattern `pattern`, that is, crushes
* consecutive `*` characters into a single `*`, avoiding `\*`.
*/
-LTTNG_HIDDEN
void strutils_normalize_star_glob_pattern(char *pattern)
{
const char *p;
char *np;
bool got_star = false;
- assert(pattern);
+ LTTNG_ASSERT(pattern);
for (p = pattern, np = pattern; *p != '\0'; p++) {
switch (*p) {
STAR_GLOB_PATTERN_TYPE_FLAG_NONE;
const char *p;
- assert(pattern);
+ LTTNG_ASSERT(pattern);
for (p = pattern; *p != '\0'; p++) {
switch (*p) {
* Returns true if `pattern` is a star-only globbing pattern, that is,
* it contains at least one non-escaped `*`.
*/
-LTTNG_HIDDEN
bool strutils_is_star_glob_pattern(const char *pattern)
{
return strutils_test_glob_pattern(pattern) &
* Returns true if `pattern` is a globbing pattern with a globbing,
* non-escaped star only at its very end.
*/
-LTTNG_HIDDEN
bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern)
{
return strutils_test_glob_pattern(pattern) &
* removes `\`. If `only_char` is not 0, only this character is
* escaped.
*/
-LTTNG_HIDDEN
char *strutils_unescape_string(const char *input, char only_char)
{
char *output;
char *o;
const char *i;
- assert(input);
+ LTTNG_ASSERT(input);
output = zmalloc(strlen(input) + 1);
if (!output) {
goto end;
* Frees a null-terminated array of strings, including each contained
* string.
*/
-LTTNG_HIDDEN
void strutils_free_null_terminated_array_of_strings(char **array)
{
char **item;
/*
* Splits the input string `input` using the given delimiter `delim`.
*
- * The return value is an allocated null-terminated array of the
- * resulting substrings (also allocated). You can free this array and
- * its content with strutils_free_null_terminated_array_of_strings(). You
- * can get the number of substrings in it with
- * strutils_array_of_strings_len().
+ * The return value is a dynamic pointer array that is assumed to be empty. The
+ * array must be discarded by the caller by invoking
+ * lttng_dynamic_pointer_array_reset().
*
* Empty substrings are part of the result. For example:
*
* `\`
* `hi`
*
- * Returns NULL if there's an error.
+ * Returns -1 if there's an error.
*/
-LTTNG_HIDDEN
-char **strutils_split(const char *input, char delim, bool escape_delim)
+int strutils_split(const char *input,
+ char delim,
+ bool escape_delim,
+ struct lttng_dynamic_pointer_array *out_strings)
{
+ int ret;
size_t at;
size_t number_of_substrings = 1;
size_t longest_substring_len = 0;
const char *s;
const char *last;
- char **substrings = NULL;
- assert(input);
- assert(!(escape_delim && delim == '\\'));
- assert(delim != '\0');
+ LTTNG_ASSERT(input);
+ LTTNG_ASSERT(!(escape_delim && delim == '\\'));
+ LTTNG_ASSERT(delim != '\0');
+ lttng_dynamic_pointer_array_init(out_strings, free);
/* First pass: count the number of substrings. */
for (s = input, last = input - 1; *s != '\0'; s++) {
longest_substring_len = s - last - 1;
}
- substrings = calloc(number_of_substrings + 1, sizeof(*substrings));
- if (!substrings) {
- goto error;
- }
-
/* Second pass: actually split and copy substrings. */
for (at = 0, s = input; at < number_of_substrings; at++) {
const char *ss;
char *d;
+ char *substring = zmalloc(longest_substring_len + 1);
+
+ if (!substring) {
+ goto error;
+ }
- substrings[at] = zmalloc(longest_substring_len + 1);
- if (!substrings[at]) {
+ ret = lttng_dynamic_pointer_array_add_pointer(
+ out_strings, substring);
+ if (ret) {
+ free(substring);
goto error;
}
* Copy characters to substring until we find the next
* delimiter or the end of the input string.
*/
- for (ss = s, d = substrings[at]; *ss != '\0'; ss++) {
+ for (ss = s, d = substring; *ss != '\0'; ss++) {
if (escape_delim && *ss == '\\') {
if (ss[1] == delim) {
/*
s = ss + 1;
}
+ ret = 0;
goto end;
error:
- strutils_free_null_terminated_array_of_strings(substrings);
- substrings = NULL;
+ ret = -1;
end:
- return substrings;
+ return ret;
}
-LTTNG_HIDDEN
size_t strutils_array_of_strings_len(char * const *array)
{
char * const *item;
size_t count = 0;
- assert(array);
+ LTTNG_ASSERT(array);
for (item = array; *item; item++) {
count++;