[gtk+/multiroot-filechooser: 13/15] Do uri_has_prefix() without allocations



commit e4b736d8d8196bd8d02dc9d2d0faec7959898b87
Author: Federico Mena Quintero <federico novell com>
Date:   Tue Jun 15 16:42:57 2010 -0500

    Do uri_has_prefix() without allocations
    
    Signed-off-by: Federico Mena Quintero <federico novell com>

 gtk/gtkfilechooser.c        |   59 +++++++++++++++++++------------------------
 gtk/gtkfilechooserprivate.h |    2 +-
 2 files changed, 27 insertions(+), 34 deletions(-)
---
diff --git a/gtk/gtkfilechooser.c b/gtk/gtkfilechooser.c
index 2398ce2..752c70d 100644
--- a/gtk/gtkfilechooser.c
+++ b/gtk/gtkfilechooser.c
@@ -2865,48 +2865,41 @@ gtk_file_chooser_list_shortcut_folders (GtkFileChooser *chooser)
 
 #endif
 
+static gboolean
+uri_has_prefix (const char *uri, const char *prefix)
+{
+  int prefix_len;
+  const char *remainder;
+
+  prefix_len = strlen (prefix);
+  if (prefix_len > 0 && prefix[prefix_len - 1] == '/')
+    prefix_len--;
+
+  if (strncmp (uri, prefix, prefix_len) != 0)
+    return FALSE;
+
+  remainder = uri + prefix_len;
+
+  return (*remainder == '/') || (*remainder == '\0');
+}
+
 gboolean
-_gtk_file_chooser_uri_has_prefix (const char   *uri,
-                                  const GSList *prefixes)
+_gtk_file_chooser_uri_has_prefix (const char *uri, GSList *prefixes)
 {
-  int uri_len;
-  gboolean result = FALSE;
-  const GSList *l;
+  GSList *l;
 
   g_return_val_if_fail (uri != NULL, FALSE);
   g_return_val_if_fail (prefixes != NULL, FALSE);
 
-  uri_len = strlen (uri);
-
-  for (l = prefixes; l != NULL && !result; l = l->next)
+  for (l = prefixes; l != NULL; l = l->next)
     {
-      char *new_prefix = NULL;
-      char *prefix = (char *)l->data;
-      int prefix_len = strlen (prefix);
-
-      if (prefix[prefix_len - 1] != '/')
-        {
-          new_prefix = g_strdup_printf ("%s/", prefix);
-          prefix = new_prefix;
-          prefix_len++;
-        }
+      const char *prefix = l->data;
 
-      if (prefix_len == uri_len + 1)
-        {
-          /*
-           * Special case. The prefix URI may contain a trailing slash while the
-           * URI we're comparing against may not (or vice-versa), despite the
-           * URIs being equal.
-           */
-          result = !strncmp (prefix, uri, uri_len);
-        }
-      else
-        result = (uri_len >= prefix_len && g_str_has_prefix (uri, prefix));
-
-      g_free (new_prefix);
+      if (uri_has_prefix (uri, prefix))
+	return TRUE;
     }
 
-  return result;
+  return FALSE;
 }
 
 gboolean
diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h
index c02c3ee..7341649 100644
--- a/gtk/gtkfilechooserprivate.h
+++ b/gtk/gtkfilechooserprivate.h
@@ -113,7 +113,7 @@ gboolean       _gtk_file_chooser_is_file_in_root (GtkFileChooser *chooser,
 GSList *       _gtk_file_chooser_get_visible_roots (GtkFileChooser *chooser);
 
 gboolean       _gtk_file_chooser_uri_has_prefix (const char   *uri,
-                                                 const GSList *prefixes);
+                                                 GSList *prefixes);
 
 /* GtkFileChooserDialog private */
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]