strv: introduce strv_split_newlines_full()
This commit is contained in:
+13
-12
@@ -240,27 +240,28 @@ int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char **strv_split_newlines(const char *s) {
|
||||
char **l;
|
||||
int strv_split_newlines_full(char ***ret, const char *s, ExtractFlags flags) {
|
||||
_cleanup_strv_free_ char **l = NULL;
|
||||
size_t n;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
|
||||
/* Special version of strv_split() that splits on newlines and
|
||||
* suppresses an empty string at the end */
|
||||
/* Special version of strv_split_full() that splits on newlines and
|
||||
* suppresses an empty string at the end. */
|
||||
|
||||
l = strv_split(s, NEWLINE);
|
||||
if (!l)
|
||||
return NULL;
|
||||
r = strv_split_full(&l, s, NEWLINE, flags);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
n = strv_length(l);
|
||||
if (n <= 0)
|
||||
return l;
|
||||
|
||||
if (isempty(l[n - 1]))
|
||||
if (n > 0 && isempty(l[n - 1])) {
|
||||
l[n - 1] = mfree(l[n - 1]);
|
||||
n--;
|
||||
}
|
||||
|
||||
return l;
|
||||
*ret = TAKE_PTR(l);
|
||||
return n;
|
||||
}
|
||||
|
||||
int strv_split_full(char ***t, const char *s, const char *separators, ExtractFlags flags) {
|
||||
|
||||
+11
-5
@@ -73,15 +73,21 @@ static inline bool strv_isempty(char * const *l) {
|
||||
return !l || !*l;
|
||||
}
|
||||
|
||||
char **strv_split_newlines(const char *s);
|
||||
|
||||
int strv_split_full(char ***t, const char *s, const char *separators, ExtractFlags flags);
|
||||
static inline char **strv_split(const char *s, const char *separators) {
|
||||
char **ret;
|
||||
int r;
|
||||
|
||||
r = strv_split_full(&ret, s, separators, 0);
|
||||
if (r < 0)
|
||||
if (strv_split_full(&ret, s, separators, 0) < 0)
|
||||
return NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int strv_split_newlines_full(char ***ret, const char *s, ExtractFlags flags);
|
||||
static inline char **strv_split_newlines(const char *s) {
|
||||
char **ret;
|
||||
|
||||
if (strv_split_newlines_full(&ret, s, 0) < 0)
|
||||
return NULL;
|
||||
|
||||
return ret;
|
||||
|
||||
Reference in New Issue
Block a user