refstring: add GEqualFunc for ref-counted strings

This is the same as g_str_equal() except that it will first check the
string length for equality before dereferencing string contents.
This commit is contained in:
Christian Hergert 2024-08-18 12:47:29 -07:00
parent 73f5ca5763
commit 3bb903d35d
2 changed files with 27 additions and 0 deletions

View File

@ -234,3 +234,26 @@ g_ref_string_length (char *str)
return g_atomic_rc_box_get_size (str) - 1;
}
/**
* g_ref_string_equal:
* @str1: a reference counted string
* @str2: a reference counted string
*
* Compares two ref-counted strings for byte-by-byte equality
* and returns `TRUE` if they are equal. It can be passed to
* `g_hash_table_new()` as the `@key_equal_func` parameter.
*
* Returns: `TRUE` if the strings are equal, otherwise `FALSE`
*
* Since: 2.82
*/
gboolean
g_ref_string_equal (char *str1,
char *str2)
{
if (g_atomic_rc_box_get_size (str1) != g_atomic_rc_box_get_size (str2))
return FALSE;
return strcmp (str1, str2) == 0;
}

View File

@ -56,4 +56,8 @@ gsize g_ref_string_length (char *str);
*/
typedef char GRefString;
GLIB_AVAILABLE_IN_2_82
gboolean g_ref_string_equal (char *str1,
char *str2);
G_END_DECLS