mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-08 23:30:18 +01:00
glib-aux: add nm_strv_dup_full() helper to preserve empty strv
This commit is contained in:
parent
503a76f604
commit
541979d098
2 changed files with 22 additions and 8 deletions
|
|
@ -3580,6 +3580,9 @@ nm_strv_make_deep_copied_n(const char **strv, gsize len)
|
|||
* the returned array must be freed with g_strfreev(). Otherwise, the
|
||||
* strings themself are not copied. You must take care of who owns the
|
||||
* strings yourself.
|
||||
* @preserved_empty: affects how to handle if the strv array is empty (length 0).
|
||||
* If TRUE, results in a non-NULL, empty, allocated strv array. If FALSE,
|
||||
* returns NULL instead of an empty strv array.
|
||||
*
|
||||
* Like g_strdupv(), with two differences:
|
||||
*
|
||||
|
|
@ -3598,7 +3601,10 @@ nm_strv_make_deep_copied_n(const char **strv, gsize len)
|
|||
* cloned or not.
|
||||
*/
|
||||
char **
|
||||
_nm_strv_dup(const char *const *strv, gssize len, gboolean deep_copied)
|
||||
_nm_strv_dup_full(const char *const *strv,
|
||||
gssize len,
|
||||
gboolean deep_copied,
|
||||
gboolean preserve_empty)
|
||||
{
|
||||
gsize i, l;
|
||||
char **v;
|
||||
|
|
@ -3607,13 +3613,16 @@ _nm_strv_dup(const char *const *strv, gssize len, gboolean deep_copied)
|
|||
l = NM_PTRARRAY_LEN(strv);
|
||||
else
|
||||
l = len;
|
||||
if (l == 0) {
|
||||
/* this function never returns an empty strv array. If you
|
||||
* need that, handle it yourself. */
|
||||
|
||||
if (l == 0 && !preserve_empty) {
|
||||
/* An empty strv array is not returned (as requested by
|
||||
* !preserved_empty). Instead, return NULL. */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
v = g_new(char *, l + 1);
|
||||
nm_assert(l < G_MAXSIZE);
|
||||
|
||||
v = g_new(char *, l + 1u);
|
||||
for (i = 0; i < l; i++) {
|
||||
if (G_UNLIKELY(!strv[i])) {
|
||||
/* NULL strings are not allowed. Clear the remainder of the array
|
||||
|
|
|
|||
|
|
@ -1863,10 +1863,15 @@ nm_strv_make_deep_copied_nonnull(const char **strv)
|
|||
return nm_strv_make_deep_copied(strv) ?: nm_strv_empty_new();
|
||||
}
|
||||
|
||||
char **_nm_strv_dup(const char *const *strv, gssize len, gboolean deep_copied);
|
||||
char **_nm_strv_dup_full(const char *const *strv,
|
||||
gssize len,
|
||||
gboolean deep_copied,
|
||||
gboolean preserve_empty);
|
||||
|
||||
#define nm_strv_dup(strv, len, deep_copied) \
|
||||
_nm_strv_dup(NM_CAST_STRV_CC(strv), (len), (deep_copied))
|
||||
#define nm_strv_dup_full(strv, len, deep_copied, preserve_empty) \
|
||||
_nm_strv_dup_full(NM_CAST_STRV_CC(strv), (len), (deep_copied), (preserve_empty))
|
||||
|
||||
#define nm_strv_dup(strv, len, deep_copied) nm_strv_dup_full((strv), (len), (deep_copied), FALSE)
|
||||
|
||||
const char **_nm_strv_dup_packed(const char *const *strv, gssize len);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue