summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-01-30 10:31:58 +0100
committerThomas Haller <thaller@redhat.com>2019-02-13 16:03:23 +0100
commit4fab0d09a5c860701826eeaa4dd1f0a20440c0b9 (patch)
tree64d2a6073866b774249afd9d32f87eaedb544ab8
parentd216e2f30580155e18cc7641d8320c06ed4c9c55 (diff)
downloadNetworkManager-4fab0d09a5c860701826eeaa4dd1f0a20440c0b9.tar.gz
shared: add NM_STR_HAS_SUFFIX()
Contrary to g_str_has_suffix(), it exploits the fact the the suffix length is known at compile time. No need to call a glib function, to find out what we already know, to call strcmp(). Instead just calculate the string length and call memcmp().
-rw-r--r--shared/nm-utils/nm-macros-internal.h13
1 files changed, 12 insertions, 1 deletions
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 55cdd374da..6a62be103e 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -449,7 +449,7 @@ NM_G_ERROR_MSG (GError *error)
/*****************************************************************************/
/* macro to return strlen() of a compile time string. */
-#define NM_STRLEN(str) ( sizeof ("" str) - 1 )
+#define NM_STRLEN(str) ( sizeof (""str"") - 1 )
/* returns the length of a NULL terminated array of pointers,
* like g_strv_length() does. The difference is:
@@ -866,6 +866,17 @@ fcn (void) \
#define NM_STR_HAS_PREFIX(str, prefix) \
(strncmp ((str), ""prefix"", NM_STRLEN (prefix)) == 0)
+#define NM_STR_HAS_SUFFIX(str, suffix) \
+ ({ \
+ const char *_str = (str); \
+ gsize _l = strlen (_str); \
+ \
+ ( (_l >= NM_STRLEN (suffix)) \
+ && (memcmp (&_str[_l - NM_STRLEN (suffix)], \
+ ""suffix"", \
+ NM_STRLEN (suffix)) == 0)); \
+ })
+
/*****************************************************************************/
static inline GString *