[gtranslator: 2/5] Improve nplurals inconsistency code




commit 1ec77e118532bef3c5f5ef00935152ab44bee136
Author: Daniel GarcĂ­a Moreno <dani danigm net>
Date:   Thu Jun 2 16:52:01 2022 +0200

    Improve nplurals inconsistency code
    
     * Move parse_nplurals_header to a new function in utils, to use the
       same function in the two places it's used
     * Do not create a new GtrHeader object in
       gtr_po_consistent_with_profile, use the one existing in the GtrPo.
     * Some code styling improvements

 src/gtr-header.c | 27 ++-------------------------
 src/gtr-po.c     | 45 ++++++++++++---------------------------------
 src/gtr-po.h     |  2 +-
 src/gtr-utils.c  | 28 ++++++++++++++++++++++++++++
 src/gtr-utils.h  |  2 ++
 5 files changed, 45 insertions(+), 59 deletions(-)
---
diff --git a/src/gtr-header.c b/src/gtr-header.c
index 02f53776..9a6f4082 100644
--- a/src/gtr-header.c
+++ b/src/gtr-header.c
@@ -72,7 +72,7 @@ gtr_header_set_field (GtrHeader * header,
 static void
 parse_nplurals (GtrHeader * header)
 {
-  gchar *pointer, *plural_forms;
+  gchar *plural_forms;
   gboolean use_profile_values;
   GtrHeaderPrivate *priv = gtr_header_get_instance_private (header);
 
@@ -111,30 +111,7 @@ parse_nplurals (GtrHeader * header)
         return;
     }
 
-  pointer = g_strrstr (plural_forms, "nplurals");
-
-  if (pointer != NULL)
-    {
-      while (*pointer != '\0' && *pointer != '=')
-        pointer++;
-
-      if (*pointer != '\0')
-        {
-          pointer++;
-          while (*pointer != '\0' && *pointer == ' ')
-            pointer++;
-
-          if (*pointer == '\0')
-            return;
-        }
-      else
-        return;
-
-      priv->nplurals = g_ascii_digit_value (*pointer);
-    }
-
-  /*g_message ("nplurals: %d", priv->nplurals); */
-
+  priv->nplurals = parse_nplurals_header (plural_forms);
   g_free (plural_forms);
 }
 
diff --git a/src/gtr-po.c b/src/gtr-po.c
index 0355c7ea..75ebe49a 100644
--- a/src/gtr-po.c
+++ b/src/gtr-po.c
@@ -27,7 +27,6 @@
  *   Gediminas Paulauskas <menesis kabalak net>
  */
 
-#include "gtr-header.h"
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
@@ -346,7 +345,8 @@ gtr_po_class_init (GtrPoClass * klass)
     g_signal_new ("file-is-inconsistent-with-profile",
                   G_OBJECT_CLASS_TYPE (klass),
                   G_SIGNAL_RUN_LAST,
-                  0,NULL,NULL,g_cclosure_marshal_VOID__VOID,
+                  0, NULL, NULL,
+                  g_cclosure_marshal_VOID__VOID,
                   G_TYPE_NONE, 0);
 }
 
@@ -1425,45 +1425,24 @@ gtr_po_check_po_file (GtrPo * po)
  * Tests whether the po file is consistent or not with profile values
  * Returns: If the po file is not consistent with profile then it returns 0 .
  **/
-int gtr_po_consistent_with_profile(GtrPo * po){
-  GtrPoPrivate *priv; 
+gboolean
+gtr_po_consistent_with_profile (GtrPo * po)
+{
   GtrProfileManager *prof_manager;
   GtrProfile *profile;
   gint po_header_nplurals;
-  gint profile_nplurals;
+  gint profile_nplurals = -1;
 
-  priv= gtr_po_get_instance_private(po);
-  po_message_iterator_t iter = po_message_iterator (priv->gettext_po_file, NULL);
-  GtrHeader *header = gtr_header_new (iter, po_next_message(iter));
-  po_header_nplurals = gtr_header_get_nplurals(header);
+  GtrHeader *header = gtr_po_get_header (po);
+  po_header_nplurals = gtr_header_get_nplurals (header);
 
   prof_manager = gtr_profile_manager_get_default ();
   profile = gtr_profile_manager_get_active_profile (prof_manager);
-  gchar *pointer = g_strrstr (gtr_profile_get_plural_forms(profile), "nplurals");
-  if (pointer != NULL)
-    {
-      while (*pointer != '\0' && *pointer != '=')
-        pointer++;
-
-      if (*pointer != '\0')
-        {
-          pointer++;
-          while (*pointer != '\0' && *pointer == ' ')
-            pointer++;
-
-          if (*pointer == '\0')
-            return 0;
-        }
-      else
-        return 0;
-
-      profile_nplurals = g_ascii_digit_value (*pointer);
-    }
+  profile_nplurals = parse_nplurals_header (
+    gtr_profile_get_plural_forms (profile)
+  );
 
-  if (profile_nplurals == po_header_nplurals) 
-    return 1;
-  else 
-    return 0;
+  return profile_nplurals == po_header_nplurals;
 }
 
 const gchar *
diff --git a/src/gtr-po.h b/src/gtr-po.h
index 27828561..520c54e8 100644
--- a/src/gtr-po.h
+++ b/src/gtr-po.h
@@ -155,7 +155,7 @@ gtr_po_get_message_position (GtrPo * po);
 void
 gtr_po_emit_file_not_consistent (GtrPo * po);
 
-int
+gboolean
 gtr_po_consistent_with_profile (GtrPo * po);
 /* Unexported funcs */
      void
diff --git a/src/gtr-utils.c b/src/gtr-utils.c
index c89d1514..75655019 100644
--- a/src/gtr-utils.c
+++ b/src/gtr-utils.c
@@ -818,3 +818,31 @@ gtk_list_box_remove (GtkListBox *box, GtkWidget *child)
 {
   gtk_container_remove (GTK_CONTAINER (box), child);
 }
+
+// TODO: Improve this parser, this string parsing is weak
+// It could be better to use GRegex: https://docs.gtk.org/glib/method.Regex.match.html
+int
+parse_nplurals_header (const gchar * plurals_header)
+{
+  gchar * pointer = g_strrstr (plurals_header, "nplurals");
+
+  if (!pointer)
+    return -1;
+
+  while (*pointer != '\0' && *pointer != '=')
+    pointer++;
+
+  if (*pointer != '\0')
+    {
+      pointer++;
+      while (*pointer != '\0' && *pointer == ' ')
+        pointer++;
+
+      if (*pointer == '\0')
+        return -1;
+    }
+  else
+    return -1;
+
+  return g_ascii_digit_value (*pointer);
+}
diff --git a/src/gtr-utils.h b/src/gtr-utils.h
index 10aea191..78362d6e 100644
--- a/src/gtr-utils.h
+++ b/src/gtr-utils.h
@@ -86,4 +86,6 @@ void gtr_utils_menu_position_under_tree_view (GtkMenu * menu,
      void gtk_list_box_append (GtkListBox *box, GtkWidget *child);
 
      void gtk_list_box_remove (GtkListBox *box, GtkWidget *child);
+
+     int parse_nplurals_header (const gchar * plurals_header);
 #endif


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