mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-12 23:46:17 +01:00
Add length accessor for GRefString
Since we store the size of the allocation in the underlying ArcBox, we can get a constant time getter for the length of the string.
This commit is contained in:
parent
43b7a8f158
commit
32ecb86f5b
@ -3499,4 +3499,5 @@ g_ref_string_new
|
||||
g_ref_string_new_intern
|
||||
g_ref_string_acquire
|
||||
g_ref_string_release
|
||||
g_ref_string_length
|
||||
</SECTION>
|
||||
|
@ -173,3 +173,21 @@ g_ref_string_release (char *str)
|
||||
|
||||
g_arc_box_release_full (str, remove_if_interned);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_ref_string_length:
|
||||
* @str: a reference counted string
|
||||
*
|
||||
* Retrieves the length of @str.
|
||||
*
|
||||
* Returns: the length of the given string, in bytes
|
||||
*
|
||||
* Since: 2.58
|
||||
*/
|
||||
gsize
|
||||
g_ref_string_length (char *str)
|
||||
{
|
||||
g_return_val_if_fail (str != NULL && *str != '\0', 0);
|
||||
|
||||
return g_arc_box_get_size (str) - 1;
|
||||
}
|
||||
|
@ -33,6 +33,9 @@ char * g_ref_string_acquire (char *str);
|
||||
GLIB_AVAILABLE_IN_2_58
|
||||
void g_ref_string_release (char *str);
|
||||
|
||||
GLIB_AVAILABLE_IN_2_58
|
||||
gsize g_ref_string_length (char *str);
|
||||
|
||||
typedef char GRefString;
|
||||
|
||||
G_END_DECLS
|
||||
|
@ -28,6 +28,7 @@ test_refstring_base (void)
|
||||
g_test_message ("s = '%s' (%p)", s, s);
|
||||
g_assert_cmpint (strcmp (s, "hello, world"), ==, 0);
|
||||
g_assert_cmpint (strlen (s), ==, strlen ("hello, world"));
|
||||
g_assert_cmpuint (g_ref_string_length (s), ==, strlen ("hello, world"));
|
||||
|
||||
g_ref_string_release (s);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user