Add purple_tag_parse for parsing tags

Sat, 05 Nov 2022 00:15:03 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Sat, 05 Nov 2022 00:15:03 -0500
changeset 41892
39e595f7d056
parent 41891
a45d6db4f857
child 41893
c66637c918bb

Add purple_tag_parse for parsing tags

Testing Done:
Ran the unit tests. Output everything via printf to verify unicode characters were fine with a forced failure, and learned that `g_message` won't output emoji I guess..

Reviewed at https://reviews.imfreedom.org/r/2027/

libpurple/purpletags.c file | annotate | diff | comparison | revisions
libpurple/purpletags.h file | annotate | diff | comparison | revisions
libpurple/tests/test_tags.c file | annotate | diff | comparison | revisions
--- a/libpurple/purpletags.c	Fri Nov 04 23:58:18 2022 -0500
+++ b/libpurple/purpletags.c	Sat Nov 05 00:15:03 2022 -0500
@@ -175,3 +175,27 @@
 
 	return g_string_free(value, FALSE);
 }
+
+void
+purple_tag_parse(const char *tag, char **name, char **value) {
+	const char *colon = NULL;
+
+	g_return_if_fail(tag != NULL);
+
+	colon = g_strstr_len(tag, -1, ":");
+	if(colon == NULL) {
+		if(name != NULL) {
+			*name = g_strdup(tag);
+		}
+		if(value != NULL) {
+			*value = NULL;
+		}
+	} else {
+		if(name != NULL) {
+			*name = g_strndup(tag, colon - tag);
+		}
+		if(value != NULL) {
+			*value = g_strdup(colon + 1);
+		}
+	}
+}
--- a/libpurple/purpletags.h	Fri Nov 04 23:58:18 2022 -0500
+++ b/libpurple/purpletags.h	Sat Nov 05 00:15:03 2022 -0500
@@ -152,6 +152,20 @@
  */
 gchar *purple_tags_to_string(PurpleTags *tags, const gchar *separator);
 
+/**
+ * purple_tag_parse:
+ * @tag: The tag.
+ * @name: (nullable) (optional) (out): An optional return address for the name
+ *        of the tag.
+ * @value: (nullable) (optional) (out): An optional return address for the
+ *         value of the tag.
+ *
+ * Splits a tag into its name and value parts.
+ *
+ * Since: 3.0.0
+ */
+void purple_tag_parse(const char *tag, char **name, char **value);
+
 G_END_DECLS
 
 #endif /* PURPLE_TAGS_H */
--- a/libpurple/tests/test_tags.c	Fri Nov 04 23:58:18 2022 -0500
+++ b/libpurple/tests/test_tags.c	Sat Nov 05 00:15:03 2022 -0500
@@ -245,6 +245,47 @@
 }
 
 /******************************************************************************
+ * purple_tag_parse Tests
+ *****************************************************************************/
+typedef struct {
+	char *tag;
+	char *name;
+	char *value;
+} TestPurpleTagParseData;
+
+static void
+test_purple_tag_parse(void) {
+	TestPurpleTagParseData data[] = {
+		{"", "", NULL},
+		{"foo", "foo", NULL},
+		{"🐦", "🐦", NULL},
+		{":", "", ""},
+		{"foo:bar", "foo", "bar"},
+		{"🐦:", "🐦", ""},
+		{":🐦", "", "🐦"},
+		{NULL},
+	};
+
+	for(int i = 0; data[i].tag != NULL; i++) {
+		char *name = NULL;
+		char *value = NULL;
+
+		purple_tag_parse(data[i].tag, &name, &value);
+
+		g_assert_cmpstr(name, ==, data[i].name);
+		g_assert_cmpstr(value, ==, data[i].value);
+
+		g_free(name);
+		g_free(value);
+	}
+
+	/* Finally make sure that we don't crash if neither optional argument was
+	 * passed.
+	 */
+	purple_tag_parse("", NULL, NULL);
+}
+
+/******************************************************************************
  * Public API
  *****************************************************************************/
 gint
@@ -281,5 +322,7 @@
 	g_test_add_func("/tags/to-string-multiple-with-null-separator",
 	                test_purple_tags_to_string_multiple_with_null_separator);
 
+	g_test_add_func("/tag/parse", test_purple_tag_parse);
+
 	return g_test_run();
 }

mercurial