/cpw/tomkiewicz/gg11: 7cf367cc1141: Gadu-Gadu: compile regular e...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Fri Sep 14 10:41:36 EDT 2012


Changeset: 7cf367cc11414996c1406ffec886a39b7cba1c8d
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-09-14 16:41 +0200
Branch:	 default
URL: http://hg.pidgin.im/cpw/tomkiewicz/gg11/rev/7cf367cc1141

Description:

Gadu-Gadu: compile regular expressions only once per plugin load

diffstat:

 libpurple/protocols/gg/gg.c           |   8 ++++-
 libpurple/protocols/gg/html.c         |  52 +++++++++++++++++++++++++---------
 libpurple/protocols/gg/html.h         |   3 ++
 libpurple/protocols/gg/message-prpl.c |  35 ++++++++++++-----------
 libpurple/protocols/gg/message-prpl.h |   3 ++
 5 files changed, 69 insertions(+), 32 deletions(-)

diffs (254 lines):

diff --git a/libpurple/protocols/gg/gg.c b/libpurple/protocols/gg/gg.c
--- a/libpurple/protocols/gg/gg.c
+++ b/libpurple/protocols/gg/gg.c
@@ -53,6 +53,7 @@
 #include "servconn.h"
 #include "pubdir-prpl.h"
 #include "message-prpl.h"
+#include "html.h"
 
 /* ---------------------------------------------------------------------- */
 
@@ -1070,13 +1071,18 @@ static gboolean ggp_load(PurplePlugin *p
 
 	ggp_resolver_purple_setup();
 	ggp_servconn_setup(ggp_server_option);
-	
+	ggp_html_setup();
+	ggp_message_setup_global();
+
 	return TRUE;
 }
 
 static gboolean ggp_unload(PurplePlugin *plugin)
 {
 	ggp_servconn_cleanup();
+	ggp_html_cleanup();
+	ggp_message_cleanup_global();
+
 	return TRUE;
 }
 
diff --git a/libpurple/protocols/gg/html.c b/libpurple/protocols/gg/html.c
--- a/libpurple/protocols/gg/html.c
+++ b/libpurple/protocols/gg/html.c
@@ -2,9 +2,42 @@
 
 #include <debug.h>
 
+typedef struct
+{
+	GRegex *re_html_attr;
+	GRegex *re_css_attr;
+	GRegex *re_color_hex;
+	GRegex *re_color_rgb;
+} ggp_html_global_data;
+
+static ggp_html_global_data global_data;
+
+void ggp_html_setup(void)
+{
+	global_data.re_html_attr = g_regex_new(
+		"([a-z-]+)=\"([^\"]+)\"",
+		G_REGEX_OPTIMIZE, 0, NULL);
+	global_data.re_css_attr = g_regex_new(
+		"([a-z-]+): *([^;]+)",
+		G_REGEX_OPTIMIZE, 0, NULL);
+	global_data.re_color_hex = g_regex_new(
+		"^#([0-9a-fA-F]+){6}$",
+		G_REGEX_OPTIMIZE, 0, NULL);
+	global_data.re_color_rgb = g_regex_new(
+		"^rgb\\(([0-9]+), *([0-9]+), *([0-9]+)\\)$",
+		G_REGEX_OPTIMIZE, 0, NULL);
+}
+
+void ggp_html_cleanup(void)
+{
+	g_regex_unref(global_data.re_html_attr);
+	g_regex_unref(global_data.re_css_attr);
+	g_regex_unref(global_data.re_color_hex);
+	g_regex_unref(global_data.re_color_rgb);
+}
+
 GHashTable * ggp_html_tag_attribs(const gchar *attribs_str)
 {
-	GRegex *re_attr = g_regex_new("([a-z-]+)=\"([^\"]+)\"", G_REGEX_OPTIMIZE, 0, NULL);
 	GMatchInfo *match;
 	GHashTable *attribs = g_hash_table_new_full(g_str_hash, g_str_equal,
 		g_free, g_free);
@@ -12,7 +45,7 @@ GHashTable * ggp_html_tag_attribs(const 
 	if (attribs_str == NULL)
 		return attribs;
 
-	g_regex_match(re_attr, attribs_str, 0, &match);
+	g_regex_match(global_data.re_html_attr, attribs_str, 0, &match);
 	while (g_match_info_matches(match))
 	{
 		g_hash_table_insert(attribs,
@@ -22,14 +55,12 @@ GHashTable * ggp_html_tag_attribs(const 
 		g_match_info_next(match, NULL);
 	}
 	g_match_info_free(match);
-	g_regex_unref(re_attr); /* TODO: static */
 
 	return attribs;
 }
 
 GHashTable * ggp_html_css_attribs(const gchar *attribs_str)
 {
-	GRegex *re_css = g_regex_new("([a-z-]+): *([^;]+)", G_REGEX_OPTIMIZE, 0, NULL);
 	GMatchInfo *match;
 	GHashTable *attribs = g_hash_table_new_full(g_str_hash, g_str_equal,
 		g_free, g_free);
@@ -37,7 +68,7 @@ GHashTable * ggp_html_css_attribs(const 
 	if (attribs_str == NULL)
 		return attribs;
 
-	g_regex_match(re_css, attribs_str, 0, &match);
+	g_regex_match(global_data.re_css_attr, attribs_str, 0, &match);
 	while (g_match_info_matches(match))
 	{
 		g_hash_table_insert(attribs,
@@ -47,19 +78,16 @@ GHashTable * ggp_html_css_attribs(const 
 		g_match_info_next(match, NULL);
 	}
 	g_match_info_free(match);
-	g_regex_unref(re_css); /* TODO: static */
 
 	return attribs;
 }
 
 int ggp_html_decode_color(const gchar *str)
 {
-	GRegex *re_color_hex = g_regex_new("^#([0-9a-fA-F]+){6}$", G_REGEX_OPTIMIZE, 0, NULL); // TODO: static
-	GRegex *re_color_rgb = g_regex_new("^rgb\\(([0-9]+), *([0-9]+), *([0-9]+)\\)$", G_REGEX_OPTIMIZE, 0, NULL);
 	GMatchInfo *match;
 	int color = -1;
 
-	g_regex_match(re_color_hex, str, 0, &match);
+	g_regex_match(global_data.re_color_hex, str, 0, &match);
 	if (g_match_info_matches(match))
 	{
 		if (sscanf(str + 1, "%x", &color) != 1)
@@ -67,12 +95,9 @@ int ggp_html_decode_color(const gchar *s
 	}
 	g_match_info_free(match);
 	if (color >= 0)
-	{
-		g_regex_unref(re_color_hex); g_regex_unref(re_color_rgb); /* TODO: static */
 		return color;
-	}
 
-	g_regex_match(re_color_rgb, str, 0, &match);
+	g_regex_match(global_data.re_color_rgb, str, 0, &match);
 	if (g_match_info_matches(match))
 	{
 		int r = -1, g = -1, b = -1;
@@ -97,7 +122,6 @@ int ggp_html_decode_color(const gchar *s
 			color = (r << 16) | (g << 8) | b;
 	}
 	g_match_info_free(match);
-	g_regex_unref(re_color_hex); g_regex_unref(re_color_rgb); /* TODO: static */
 	if (color >= 0)
 		return color;
 
diff --git a/libpurple/protocols/gg/html.h b/libpurple/protocols/gg/html.h
--- a/libpurple/protocols/gg/html.h
+++ b/libpurple/protocols/gg/html.h
@@ -19,6 +19,9 @@ typedef enum
 	GGP_HTML_TAG_HR,
 } ggp_html_tag;
 
+void ggp_html_setup(void);
+void ggp_html_cleanup(void);
+
 GHashTable * ggp_html_tag_attribs(const gchar *attribs_str);
 GHashTable * ggp_html_css_attribs(const gchar *attribs_str);
 int ggp_html_decode_color(const gchar *str);
diff --git a/libpurple/protocols/gg/message-prpl.c b/libpurple/protocols/gg/message-prpl.c
--- a/libpurple/protocols/gg/message-prpl.c
+++ b/libpurple/protocols/gg/message-prpl.c
@@ -27,21 +27,12 @@ typedef struct
 	uint64_t chat_id;
 } ggp_message_got_data;
 
-typedef enum
+typedef struct
 {
-	GGP_TAG_UNKNOWN,
-	GGP_TAG_IGNORED,
-	GGP_TAG_EOM,
-	GGP_TAG_B,
-	GGP_TAG_I,
-	GGP_TAG_U,
-	GGP_TAG_S,
-	GGP_TAG_FONT,
-	GGP_TAG_SPAN,
-	GGP_TAG_DIV,
-	GGP_TAG_BR,
-	GGP_TAG_HR,
-} ggp_tag;
+	GRegex *re_html_tag;
+} ggp_message_global_data;
+
+static ggp_message_global_data global_data;
 
 typedef struct
 {
@@ -66,6 +57,18 @@ static gchar * ggp_message_format_to_gg(
 
 /**************/
 
+void ggp_message_setup_global(void)
+{
+	global_data.re_html_tag = g_regex_new(
+		"<(/)?([a-z]+)( [^>]+)?>",
+		G_REGEX_OPTIMIZE, 0, NULL);
+}
+
+void ggp_message_cleanup_global(void)
+{
+	g_regex_unref(global_data.re_html_tag);
+}
+
 static ggp_font * ggp_font_new(void)
 {
 	ggp_font *font;
@@ -204,7 +207,6 @@ static void ggp_message_format_from_gg(g
 
 static gchar * ggp_message_format_to_gg(const gchar *text)
 {
-	GRegex *re_tag = g_regex_new("<(/)?([a-z]+)( [^>]+)?>", G_REGEX_OPTIMIZE, 0, NULL);
 	gchar *text_new, *tmp;
 	GList *rt = NULL; /* reformatted text */
 	GMatchInfo *match;
@@ -241,7 +243,7 @@ static gchar * ggp_message_format_to_gg(
 	text_new = g_strdup_printf("%s<eom></eom>", text_new);
 	g_free(tmp);
 
-	g_regex_match(re_tag, text_new, 0, &match);
+	g_regex_match(global_data.re_html_tag, text_new, 0, &match);
 	while (g_match_info_matches(match))
 	{
 		int m_start, m_end, m_pos;
@@ -493,7 +495,6 @@ static gchar * ggp_message_format_to_gg(
 		g_free(attribs_str);
 	}
 	g_match_info_free(match);
-	g_regex_unref(re_tag); /* TODO: static */
 
 	if (pos < strlen(text_new) || in_any_tag)
 	{
diff --git a/libpurple/protocols/gg/message-prpl.h b/libpurple/protocols/gg/message-prpl.h
--- a/libpurple/protocols/gg/message-prpl.h
+++ b/libpurple/protocols/gg/message-prpl.h
@@ -4,6 +4,9 @@
 #include <internal.h>
 #include <libgadu.h>
 
+void ggp_message_setup_global(void);
+void ggp_message_cleanup_global(void);
+
 void ggp_message_got(PurpleConnection *gc, const struct gg_event_msg *ev);
 void ggp_message_got_multilogon(PurpleConnection *gc,
 	const struct gg_event_msg *ev);



More information about the Commits mailing list