summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gnome.org>2018-06-12 14:05:02 +0100
committerEmmanuele Bassi <ebassi@gnome.org>2018-07-09 10:11:42 +0100
commit32ecb86f5bb2290d3481187514305c173f33fe6e (patch)
treec235677509ea4df63b3c2473ab585f93b09c6871
parent43b7a8f158343f1334a539d85a71dfb8a5e0328a (diff)
downloadglib-32ecb86f5bb2290d3481187514305c173f33fe6e.tar.gz
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.
-rw-r--r--docs/reference/glib/glib-sections.txt1
-rw-r--r--glib/grefstring.c18
-rw-r--r--glib/grefstring.h3
-rw-r--r--glib/tests/refstring.c1
4 files changed, 23 insertions, 0 deletions
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index b73ba1cc0..1717ba37d 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -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>
diff --git a/glib/grefstring.c b/glib/grefstring.c
index cc62c8011..9d64a968b 100644
--- a/glib/grefstring.c
+++ b/glib/grefstring.c
@@ -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;
+}
diff --git a/glib/grefstring.h b/glib/grefstring.h
index f2156fef4..2afe23a73 100644
--- a/glib/grefstring.h
+++ b/glib/grefstring.h
@@ -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
diff --git a/glib/tests/refstring.c b/glib/tests/refstring.c
index 0181be945..67b3ca302 100644
--- a/glib/tests/refstring.c
+++ b/glib/tests/refstring.c
@@ -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);
}