Optimize string assignments and comparisons in XBEL loading

We can do fast string comparisons in places where we know that
strings are not NULL.

We can directly assign values to items we create ourselves.
This commit is contained in:
Christian Dywan 2009-11-30 23:21:59 +01:00
parent abba290278
commit aa5b5850c0

View file

@ -23,6 +23,8 @@
#include <libxml/tree.h> #include <libxml/tree.h>
#endif #endif
#define katze_str_equal(str1, str2) !strcmp (str1, str2)
static void static void
katze_xbel_parse_info (KatzeItem* item, katze_xbel_parse_info (KatzeItem* item,
xmlNodePtr cur); xmlNodePtr cur);
@ -35,29 +37,18 @@ static KatzeItem*
katze_item_from_xmlNodePtr (xmlNodePtr cur) katze_item_from_xmlNodePtr (xmlNodePtr cur)
{ {
KatzeItem* item; KatzeItem* item;
xmlChar* key;
item = katze_item_new (); item = katze_item_new ();
key = xmlGetProp (cur, (xmlChar*)"href"); item->uri = (gchar*)xmlGetProp (cur, (xmlChar*)"href");
katze_item_set_uri (item, (gchar*)key);
g_free (key);
cur = cur->xmlChildrenNode; cur = cur->xmlChildrenNode;
while (cur) while (cur)
{ {
if (!xmlStrcmp (cur->name, (const xmlChar*)"title")) if (katze_str_equal ((gchar*)cur->name, "title"))
{ item->name = g_strstrip ((gchar*)xmlNodeGetContent (cur));
key = xmlNodeGetContent (cur); else if (katze_str_equal ((gchar*)cur->name, "desc"))
katze_item_set_name (item, g_strstrip ((gchar*)key)); item->text = g_strstrip ((gchar*)xmlNodeGetContent (cur));
g_free (key); else if (katze_str_equal ((gchar*)cur->name, "info"))
}
else if (!xmlStrcmp (cur->name, (const xmlChar*)"desc"))
{
key = xmlNodeGetContent (cur);
katze_item_set_text (item, g_strstrip ((gchar*)key));
g_free (key);
}
else if (!xmlStrcmp (cur->name, (const xmlChar*)"info"))
katze_xbel_parse_info (item, cur); katze_xbel_parse_info (item, cur);
cur = cur->next; cur = cur->next;
} }
@ -89,29 +80,21 @@ katze_array_from_xmlNodePtr (xmlNodePtr cur)
cur = cur->xmlChildrenNode; cur = cur->xmlChildrenNode;
while (cur) while (cur)
{ {
if (!xmlStrcmp (cur->name, (const xmlChar*)"title")) if (katze_str_equal ((gchar*)cur->name, "title"))
{ ((KatzeItem*)array)->name = g_strstrip ((gchar*)xmlNodeGetContent (cur));
key = xmlNodeGetContent (cur); else if (katze_str_equal ((gchar*)cur->name, "desc"))
katze_item_set_name (KATZE_ITEM (array), g_strstrip ((gchar*)key)); ((KatzeItem*)array)->text = g_strstrip ((gchar*)xmlNodeGetContent (cur));
xmlFree (key); else if (katze_str_equal ((gchar*)cur->name, "folder"))
}
else if (!xmlStrcmp (cur->name, (const xmlChar*)"desc"))
{
key = xmlNodeGetContent (cur);
katze_item_set_text (KATZE_ITEM (array), g_strstrip ((gchar*)key));
xmlFree (key);
}
else if (!xmlStrcmp (cur->name, (const xmlChar*)"folder"))
{ {
item = (KatzeItem*)katze_array_from_xmlNodePtr (cur); item = (KatzeItem*)katze_array_from_xmlNodePtr (cur);
katze_array_add_item (array, item); katze_array_add_item (array, item);
} }
else if (!xmlStrcmp (cur->name, (const xmlChar*)"bookmark")) else if (katze_str_equal ((gchar*)cur->name, "bookmark"))
{ {
item = katze_item_from_xmlNodePtr (cur); item = katze_item_from_xmlNodePtr (cur);
katze_array_add_item (array, item); katze_array_add_item (array, item);
} }
else if (!xmlStrcmp (cur->name, (const xmlChar*)"separator")) else if (katze_str_equal ((gchar*)cur->name, "separator"))
{ {
item = katze_item_new (); item = katze_item_new ();
katze_array_add_item (array, item); katze_array_add_item (array, item);
@ -128,7 +111,7 @@ katze_xbel_parse_info (KatzeItem* item,
cur = cur->xmlChildrenNode; cur = cur->xmlChildrenNode;
while (cur) while (cur)
{ {
if (!xmlStrcmp (cur->name, (const xmlChar*)"metadata")) if (katze_str_equal ((gchar*)cur->name, "metadata"))
{ {
xmlChar* owner = xmlGetProp (cur, (xmlChar*)"owner"); xmlChar* owner = xmlGetProp (cur, (xmlChar*)"owner");
if (owner) if (owner)
@ -137,7 +120,7 @@ katze_xbel_parse_info (KatzeItem* item,
/* Albeit required, "owner" is not set by MicroB */ /* Albeit required, "owner" is not set by MicroB */
owner = (xmlChar*)NULL; owner = (xmlChar*)NULL;
/* FIXME: Save metadata from unknown owners */ /* FIXME: Save metadata from unknown owners */
if (!owner || !strcmp ((gchar*)owner, "http://www.twotoasts.de")) if (!owner || katze_str_equal ((gchar*)owner, "http://www.twotoasts.de"))
{ {
xmlAttrPtr properties = cur->properties; xmlAttrPtr properties = cur->properties;
xmlNodePtr children = cur->children; xmlNodePtr children = cur->children;
@ -145,7 +128,7 @@ katze_xbel_parse_info (KatzeItem* item,
{ {
xmlChar* value; xmlChar* value;
if (!xmlStrcmp (properties->name, (xmlChar*)"owner")) if (katze_str_equal ((gchar*)properties->name, "owner"))
{ {
properties = properties->next; properties = properties->next;
continue; continue;
@ -174,7 +157,7 @@ katze_xbel_parse_info (KatzeItem* item,
gchar* ns_value; gchar* ns_value;
if (!owner) if (!owner)
ns_value = g_strdup_printf (":%s", children->name); ns_value = g_strdup_printf (":%s", children->name);
else if (xmlStrEqual (owner, (xmlChar*)"http://www.twotoasts.de")) else if (katze_str_equal ((gchar*)owner, "http://www.twotoasts.de"))
ns_value = g_strdup_printf ("midori:%s", children->name); ns_value = g_strdup_printf ("midori:%s", children->name);
else /* FIXME: Save metadata from unknown owners */ else /* FIXME: Save metadata from unknown owners */
ns_value = g_strdup_printf (":%s", children->name); ns_value = g_strdup_printf (":%s", children->name);
@ -188,7 +171,7 @@ katze_xbel_parse_info (KatzeItem* item,
} }
xmlFree (owner); xmlFree (owner);
} }
else if (g_strcmp0 ((gchar*)cur->name, "text")) else if (!katze_str_equal ((gchar*)cur->name, "text"))
g_critical ("Unexpected element <%s> in <metadata>.", cur->name); g_critical ("Unexpected element <%s> in <metadata>.", cur->name);
cur = cur->next; cur = cur->next;
} }
@ -223,7 +206,7 @@ katze_array_from_xmlDocPtr (KatzeArray* array,
/* Empty document */ /* Empty document */
return FALSE; return FALSE;
} }
if (xmlStrcmp (cur->name, (const xmlChar*)"xbel")) if (!katze_str_equal ((gchar*)cur->name, "xbel"))
{ {
/* Wrong document kind */ /* Wrong document kind */
return FALSE; return FALSE;
@ -232,20 +215,20 @@ katze_array_from_xmlDocPtr (KatzeArray* array,
while (cur) while (cur)
{ {
item = NULL; item = NULL;
if (!xmlStrcmp (cur->name, (const xmlChar*)"folder")) if (katze_str_equal ((gchar*)cur->name, "folder"))
item = (KatzeItem*)katze_array_from_xmlNodePtr (cur); item = (KatzeItem*)katze_array_from_xmlNodePtr (cur);
else if (!xmlStrcmp (cur->name, (const xmlChar*)"bookmark")) else if (katze_str_equal ((gchar*)cur->name, "bookmark"))
item = katze_item_from_xmlNodePtr (cur); item = katze_item_from_xmlNodePtr (cur);
else if (!xmlStrcmp (cur->name, (const xmlChar*)"separator")) else if (katze_str_equal ((gchar*)cur->name, "separator"))
item = katze_item_new (); item = katze_item_new ();
else if (!xmlStrcmp (cur->name, (const xmlChar*)"info")) else if (katze_str_equal ((gchar*)cur->name, "info"))
katze_xbel_parse_info (KATZE_ITEM (array), cur); katze_xbel_parse_info (KATZE_ITEM (array), cur);
else if (!xmlStrcmp (cur->name, (xmlChar*)"title")) else if (katze_str_equal ((gchar*)cur->name, "title"))
{ {
xmlNodePtr node = cur->xmlChildrenNode; xmlNodePtr node = cur->xmlChildrenNode;
katze_item_set_name (KATZE_ITEM (array), (gchar*)node->content); katze_item_set_name (KATZE_ITEM (array), (gchar*)node->content);
} }
else if (!xmlStrcmp (cur->name, (xmlChar*)"desc")) else if (katze_str_equal ((gchar*)cur->name, "desc"))
{ {
xmlNodePtr node = cur->xmlChildrenNode; xmlNodePtr node = cur->xmlChildrenNode;
katze_item_set_text (KATZE_ITEM (array), (gchar*)node->content); katze_item_set_text (KATZE_ITEM (array), (gchar*)node->content);
@ -305,24 +288,22 @@ katze_array_from_opera_file (KatzeArray* array,
gchar** parts = g_strsplit (line, "=", 2); gchar** parts = g_strsplit (line, "=", 2);
if (parts && parts[0] && parts[1]) if (parts && parts[0] && parts[1])
{ {
if (g_str_equal (parts[0], "NAME")) if (katze_str_equal (parts[0], "NAME"))
katze_item_set_name (item, parts[1]); item->name = g_strdup (parts[1]);
else if (g_str_equal (parts[0], "URL")) else if (katze_str_equal (parts[0], "URL"))
katze_item_set_uri (item, parts[1]); item->uri = g_strdup (parts[1]);
else if (g_str_equal (parts[0], "DESCRIPTION")) else if (katze_str_equal (parts[0], "DESCRIPTION"))
katze_item_set_text (item, parts[1]); item->text = g_strdup (parts[1]);
else if (g_str_equal (parts[0], "CREATED")) else if (katze_str_equal (parts[0], "CREATED"))
katze_item_set_added (item, item->added = g_ascii_strtoull (parts[1], NULL, 10);
g_ascii_strtoull (parts[1], NULL, 10));
/* FIXME: Implement visited time /* FIXME: Implement visited time
else if (g_str_equal (parts[0], "VISITED")) else if (katze_str_equal (parts[0], "VISITED"))
katze_item_set_visited (item, item->visited = g_ascii_strtoull (parts[1], NULL, 10); */
g_ascii_strtoull (parts[1], NULL, 10)); */ else if (katze_str_equal (parts[0], "ON PERSONALBAR"))
else if (g_str_equal (parts[0], "ON PERSONALBAR"))
katze_item_set_meta_integer (item, "toolbar", katze_item_set_meta_integer (item, "toolbar",
!g_strcmp0 (parts[1], "YES") ? 1 : -1); katze_str_equal (parts[1], "YES") ? 1 : -1);
/* FIXME: Implement websites as panels /* FIXME: Implement websites as panels
else if (g_str_equal (parts[0], "IN PANEL")) else if (katze_str_equal (parts[0], "IN PANEL"))
; */ ; */
} }
else else
@ -370,9 +351,12 @@ midori_array_from_file (KatzeArray* array,
return FALSE; return FALSE;
} }
if (!format)
format = "";
/* Opera6 */ /* Opera6 */
if (!g_strcmp0 (format, "opera") if (katze_str_equal (format, "opera")
|| (!format && g_str_has_suffix (filename, ".adr"))) || (!*format && g_str_has_suffix (filename, ".adr")))
{ {
FILE* file; FILE* file;
if ((file = g_fopen (filename, "r"))) if ((file = g_fopen (filename, "r")))
@ -384,10 +368,10 @@ midori_array_from_file (KatzeArray* array,
while (fgets (line, 50, file)) while (fgets (line, 50, file))
{ {
g_strstrip (line); g_strstrip (line);
if (verify == 0 && !strcmp (line, "Opera Hotlist version 2.0")) if (verify == 0 && katze_str_equal (line, "Opera Hotlist version 2.0"))
verify++; verify++;
else if (verify == 1 else if (verify == 1
&& !strcmp (line, "Options: encoding = utf8, version=3")) && katze_str_equal (line, "Options: encoding = utf8, version=3"))
verify++; verify++;
else if (verify == 2) else if (verify == 2)
{ {
@ -410,8 +394,8 @@ midori_array_from_file (KatzeArray* array,
} }
/* XBEL */ /* XBEL */
if (!g_strcmp0 (format, "xbel") if (katze_str_equal (format, "xbel")
|| !format) || !*format)
{ {
xmlDocPtr doc; xmlDocPtr doc;