Add an inline version of g_strcmp0

This can be a significant optimization, since
compilers know how to optimize strcmp, and
inlining g_strcmp0 will make the strcmp call
visible to the compiler at the call site.
This commit is contained in:
Matthias Clasen 2022-10-02 17:02:21 -04:00
parent 2843eef4a4
commit 7994898475
2 changed files with 33 additions and 27 deletions

View File

@ -19,8 +19,27 @@
*/
#include "config.h"
#include "gversionmacros.h"
/**
* g_strcmp0:
* @str1: (nullable): a C string or %NULL
* @str2: (nullable): another C string or %NULL
*
* Compares @str1 and @str2 like strcmp(). Handles %NULL
* gracefully by sorting it before non-%NULL strings.
* Comparing two %NULL pointers returns 0.
*
* Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2.
*
* Since: 2.16
*/
GLIB_AVAILABLE_IN_ALL
int g_strcmp0 (const char *str1, const char *str2);
#include "gtestutils.h"
#include "gfileutils.h"
#include <sys/types.h>
@ -3397,30 +3416,6 @@ g_assertion_message_error (const char *domain,
g_string_free (gstring, TRUE);
}
/**
* g_strcmp0:
* @str1: (nullable): a C string or %NULL
* @str2: (nullable): another C string or %NULL
*
* Compares @str1 and @str2 like strcmp(). Handles %NULL
* gracefully by sorting it before non-%NULL strings.
* Comparing two %NULL pointers returns 0.
*
* Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2.
*
* Since: 2.16
*/
int
g_strcmp0 (const char *str1,
const char *str2)
{
if (!str1)
return -(str1 != str2);
if (!str2)
return str1 != str2;
return strcmp (str1, str2);
}
static void
test_trap_clear (void)
{

View File

@ -238,9 +238,20 @@ typedef void (*GTestFixtureFunc) (gpointer fixture,
} G_STMT_END
#endif /* !G_DISABLE_ASSERT */
GLIB_AVAILABLE_IN_ALL
int g_strcmp0 (const char *str1,
const char *str2);
#ifndef GLIB_INLINE
#define GLIB_INLINE
#endif
GLIB_INLINE int
g_strcmp0 (const char *str1,
const char *str2)
{
if (!str1)
return -(str1 != str2);
if (!str2)
return str1 != str2;
return strcmp (str1, str2);
}
/* report performance results */
GLIB_AVAILABLE_IN_ALL