diff options
author | Thomas Haller <thaller@redhat.com> | 2020-05-08 10:46:13 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-05-08 11:10:51 +0200 |
commit | dbf14dc38caa69c234198c592e39c94616c4c202 (patch) | |
tree | 31e1cf37bf1115254355710dbd44b02690f16c39 | |
parent | f6e41c19ffcca6b2cd5a43ae480ebc5986be91c4 (diff) | |
download | NetworkManager-dbf14dc38caa69c234198c592e39c94616c4c202.tar.gz |
shared: add nm_str_is_empty() helper
We have nm_str_not_empty() which is the inverse of that. The purpose
of nm_str_not_empty() is to normalize a string to either return
%NULL or a non-empty string, like
const char *
get_name (Object *obj)
{
return nm_str_not_empty (obj->name);
}
Sometimes, we however want to check whether a string is not empty.
So, we previously had two choices:
1) use a temporary variable:
const char *tmp;
tmp = get_string ();
if (tmp && tmp[0])
...
The problem with this variant is that it's more verbose (by requiring a
temporary variable). Another downside is that there are multiple ways
how to check for an empty string (!tmp[0], tmp[0] == '\0', !strlen (tmp),
strlen (tmp) == 0), and sure enough they are all in use.
2) use !nm_str_not_empty(). But this double negation looks really odd
and confusing.
Add nm_str_is_empty() instead.
-rw-r--r-- | shared/nm-glib-aux/nm-macros-internal.h | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/shared/nm-glib-aux/nm-macros-internal.h b/shared/nm-glib-aux/nm-macros-internal.h index 637d62bb5d..f56ed85699 100644 --- a/shared/nm-glib-aux/nm-macros-internal.h +++ b/shared/nm-glib-aux/nm-macros-internal.h @@ -989,16 +989,23 @@ nm_gstring_add_space_delimiter (GString *str) return str; } +static inline gboolean +nm_str_is_empty (const char *str) +{ + /* %NULL is also accepted, and also "empty". */ + return !str || !str[0]; +} + static inline const char * nm_str_not_empty (const char *str) { - return str && str[0] ? str : NULL; + return !nm_str_is_empty (str) ? str : NULL; } static inline char * nm_strdup_not_empty (const char *str) { - return str && str[0] ? g_strdup (str) : NULL; + return !nm_str_is_empty (str) ? g_strdup (str) : NULL; } static inline char * |