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().
This commit is contained in:
Thomas Haller 2019-01-30 10:31:58 +01:00
parent d216e2f305
commit 4fab0d09a5

View file

@ -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 *