From b942af65fedbc171601644001e88414d14669a0f Mon Sep 17 00:00:00 2001 From: Arun Raghavan Date: Wed, 13 Jun 2012 16:31:59 +0530 Subject: [PATCH] core-util: Add a pa_split_in_place() string utility function For specialised uses of pa_split() such as finding substrings for comparison, this avoids the need to repeatedly allocate and deallocate memory. --- src/pulsecore/core-util.c | 23 +++++++++++++++++++++++ src/pulsecore/core-util.h | 1 + 2 files changed, 24 insertions(+) diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index d79d289f1..154a74862 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -937,6 +937,29 @@ char *pa_split(const char *c, const char *delimiter, const char**state) { return pa_xstrndup(current, l); } +/* Split the specified string wherever one of the strings in delimiter + * occurs. Each time it is called returns a pointer to the substring within the + * string and the length in 'n'. Note that the resultant string cannot be used + * as-is without the length parameter, since it is merely pointing to a point + * within the original string. The variable state points to, should be + * initialized to NULL before the first call. */ +const char *pa_split_in_place(const char *c, const char *delimiter, int *n, const char**state) { + const char *current = *state ? *state : c; + size_t l; + + if (!*current) + return NULL; + + l = strcspn(current, delimiter); + *state = current+l; + + if (**state) + (*state)++; + + *n = l; + return current; +} + /* Split a string into words. Otherwise similar to pa_split(). */ char *pa_split_spaces(const char *c, const char **state) { const char *current = *state ? *state : c; diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h index 4a1c096ed..a3c22470d 100644 --- a/src/pulsecore/core-util.h +++ b/src/pulsecore/core-util.h @@ -100,6 +100,7 @@ static inline const char *pa_strna(const char *x) { } char *pa_split(const char *c, const char*delimiters, const char **state); +const char *pa_split_in_place(const char *c, const char*delimiters, int *n, const char **state); char *pa_split_spaces(const char *c, const char **state); char *pa_strip_nl(char *s);