[gimp] app: item uniquefy algorithm allowing generic numbering schemes.



commit 56b6dbaa87d441ab501195e9a771467cc19f3cc2
Author: Jehan <jehan girinstud io>
Date:   Tue Jun 16 19:49:10 2015 +0200

    app: item uniquefy algorithm allowing generic numbering schemes.
    
    Any item ending with numbers (optionally followed by spaces) would
    trigger incremental counting, not only hashed numbers.

 app/core/gimpitem.c     |    7 +------
 app/core/gimpitemtree.c |   44 +++++++++++++++++++++-----------------------
 2 files changed, 22 insertions(+), 29 deletions(-)
---
diff --git a/app/core/gimpitem.c b/app/core/gimpitem.c
index 649a23f..1b6cb35 100644
--- a/app/core/gimpitem.c
+++ b/app/core/gimpitem.c
@@ -512,22 +512,17 @@ gimp_item_real_duplicate (GimpItem *item,
   /*  formulate the new name  */
   {
     const gchar *name;
-    gchar       *ext;
-    gint         number;
     gint         len;
 
     name = gimp_object_get_name (item);
 
     g_return_val_if_fail (name != NULL, NULL);
 
-
-    ext = strrchr (name, '#');
     len = strlen (_("copy"));
 
     if ((strlen (name) >= len &&
          strcmp (&name[strlen (name) - len], _("copy")) == 0) ||
-        (ext && (number = atoi (ext + 1)) > 0 &&
-         ((int)(log10 (number) + 1)) == strlen (ext + 1)))
+        g_regex_match_simple ("([0-9]+)\\s*$", name, 0, 0))
       {
         /* don't have redundant "copy"s */
         new_name = g_strdup (name);
diff --git a/app/core/gimpitemtree.c b/app/core/gimpitemtree.c
index 96739a8..601304e 100644
--- a/app/core/gimpitemtree.c
+++ b/app/core/gimpitemtree.c
@@ -649,32 +649,27 @@ gimp_item_tree_uniquefy_name (GimpItemTree *tree,
   if (g_hash_table_lookup (private->name_hash,
                            gimp_object_get_name (item)))
     {
-      gchar *name     = g_strdup (gimp_object_get_name (item));
-      gchar *ext      = strrchr (name, '#');
-      gchar *new_name = NULL;
-      gint   number   = 0;
-
-      if (ext)
+      gchar      *name        = g_strdup (gimp_object_get_name (item));
+      gchar      *new_name    = NULL;
+      gint        number      = 0;
+      gboolean    hashed      = TRUE;
+      GRegex     *end_numbers = g_regex_new ("(^|[^0-9])([0-9]+)\\s*$", 0, 0, NULL);
+      GMatchInfo *match_info  = NULL;
+
+      if (g_regex_match (end_numbers, name, 0, &match_info))
         {
-          gchar ext_str[8];
-
-          number = atoi (ext + 1);
+          /* Allow counting styles without a hash as alternative. */
+          gint start_pos;
 
-          g_snprintf (ext_str, sizeof (ext_str), "%d", number);
-
-          /*  check if the extension really is of the form "#<n>"  */
-          if (! strcmp (ext_str, ext + 1))
-            {
-              if (ext > name && *(ext - 1) == ' ')
-                ext--;
+          hashed = FALSE;
+          number = atoi (g_match_info_fetch (match_info, 2));
 
-              *ext = '\0';
-            }
-          else
-            {
-              number = 0;
-            }
+          g_match_info_fetch_pos (match_info, 2,
+                                  &start_pos, NULL);
+          name[start_pos] = '\0';
         }
+      g_match_info_free (match_info);
+      g_regex_unref (end_numbers);
 
       do
         {
@@ -682,7 +677,10 @@ gimp_item_tree_uniquefy_name (GimpItemTree *tree,
 
           g_free (new_name);
 
-          new_name = g_strdup_printf ("%s #%d", name, number);
+          new_name = g_strdup_printf ("%s%s%d",
+                                      name,
+                                      hashed ? " #" : "",
+                                      number);
         }
       while (g_hash_table_lookup (private->name_hash, new_name));
 


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