[gtk+/wip/cssvalue] cssarrayvalue: Redo parsing arrays



commit a9611689d32327b1ec13691cb1d4d8c607fc9a09
Author: Benjamin Otte <otte redhat com>
Date:   Tue Apr 3 09:49:37 2012 +0200

    cssarrayvalue: Redo parsing arrays
    
    Does 3 things:
    1) Introduce a "none" array signleton
    2) Get rid of memleaks in error paths
    3) Reduce code in parse funcs

 gtk/gtkcssarrayvalue.c        |   56 ++++++++++++--
 gtk/gtkcssarrayvalueprivate.h |    7 ++-
 gtk/gtkcssshorthandproperty.c |    2 +-
 gtk/gtkcssstylepropertyimpl.c |  168 ++++++++++++++---------------------------
 4 files changed, 114 insertions(+), 119 deletions(-)
---
diff --git a/gtk/gtkcssarrayvalue.c b/gtk/gtkcssarrayvalue.c
index 5260eaf..baab5fa 100644
--- a/gtk/gtkcssarrayvalue.c
+++ b/gtk/gtkcssarrayvalue.c
@@ -94,18 +94,62 @@ static const GtkCssValueClass GTK_CSS_VALUE_ARRAY = {
   gtk_css_value_array_print
 };
 
+static GtkCssValue none_singleton = { &GTK_CSS_VALUE_ARRAY, 1, 0, { NULL } };
+
 GtkCssValue *
-_gtk_css_array_value_new (GtkCssValue **values,
-                          guint         n_values)
+_gtk_css_array_value_new (GtkCssValue *content)
 {
-  GtkCssValue *result;
-  
-  g_return_val_if_fail (values != NULL || n_values == 0, NULL);
+  if (content == NULL)
+    return _gtk_css_value_ref (&none_singleton);
 
-  result = _gtk_css_value_alloc (&GTK_CSS_VALUE_ARRAY, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (MAX (1, n_values) - 1));
+  return _gtk_css_array_value_new_from_array (&content, 1);
+}
+
+GtkCssValue *
+_gtk_css_array_value_new_from_array (GtkCssValue **values,
+                                     guint         n_values)
+{
+  GtkCssValue *result;
+           
+  g_return_val_if_fail (values != NULL, NULL);
+  g_return_val_if_fail (n_values > 0, NULL);
+         
+  result = _gtk_css_value_alloc (&GTK_CSS_VALUE_ARRAY, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (n_values - 1));
   result->n_values = n_values;
   memcpy (&result->values[0], values, sizeof (GtkCssValue *) * n_values);
+            
+  return result;
+}
+
+GtkCssValue *
+_gtk_css_array_value_parse (GtkCssParser *parser,
+                            GtkCssValue  *(* parse_func) (GtkCssParser *parser),
+                            gboolean      allow_none)
+{
+  GtkCssValue *value, *result;
+  GPtrArray *values;
+
+  if (allow_none &&
+      _gtk_css_parser_try (parser, "none", TRUE))
+    return _gtk_css_value_ref (&none_singleton);
+
+  values = g_ptr_array_new ();
+
+  do {
+    value = parse_func (parser);
+
+    if (value == NULL)
+      {
+        g_ptr_array_set_free_func (values, (GDestroyNotify) _gtk_css_value_unref);
+        g_ptr_array_free (values, TRUE);
+        return NULL;
+      }
+
+    g_ptr_array_add (values, value);
+  } while (_gtk_css_parser_try (parser, ",", TRUE));
 
+  result = _gtk_css_array_value_new_from_array ((GtkCssValue **) values->pdata, values->len);
+  g_ptr_array_free (values, TRUE);
   return result;
 }
 
diff --git a/gtk/gtkcssarrayvalueprivate.h b/gtk/gtkcssarrayvalueprivate.h
index 08b6e4a..38443b5 100644
--- a/gtk/gtkcssarrayvalueprivate.h
+++ b/gtk/gtkcssarrayvalueprivate.h
@@ -20,12 +20,17 @@
 #ifndef __GTK_CSS_ARRAY_VALUE_PRIVATE_H__
 #define __GTK_CSS_ARRAY_VALUE_PRIVATE_H__
 
+#include "gtkcssparserprivate.h"
 #include "gtkcssvalueprivate.h"
 
 G_BEGIN_DECLS
 
-GtkCssValue *       _gtk_css_array_value_new            (GtkCssValue          **values,
+GtkCssValue *       _gtk_css_array_value_new            (GtkCssValue           *content);
+GtkCssValue *       _gtk_css_array_value_new_from_array (GtkCssValue          **values,
                                                          guint                  n_values);
+GtkCssValue *       _gtk_css_array_value_parse          (GtkCssParser          *parser,
+                                                         GtkCssValue *          (* parse_func) (GtkCssParser *parser),
+                                                         gboolean               allow_none);
 
 GtkCssValue *       _gtk_css_array_value_get_nth        (const GtkCssValue     *value,
                                                          guint                  i);
diff --git a/gtk/gtkcssshorthandproperty.c b/gtk/gtkcssshorthandproperty.c
index f10201d..f63d3a5 100644
--- a/gtk/gtkcssshorthandproperty.c
+++ b/gtk/gtkcssshorthandproperty.c
@@ -139,7 +139,7 @@ gtk_css_shorthand_property_parse_value (GtkStyleProperty *property,
         data[i] = _gtk_css_initial_value_new ();
     }
 
-  result = _gtk_css_array_value_new (data, shorthand->subproperties->len);
+  result = _gtk_css_array_value_new_from_array (data, shorthand->subproperties->len);
   g_free (data);
   
   return result;
diff --git a/gtk/gtkcssstylepropertyimpl.c b/gtk/gtkcssstylepropertyimpl.c
index d5ce85b..a5dc631 100644
--- a/gtk/gtkcssstylepropertyimpl.c
+++ b/gtk/gtkcssstylepropertyimpl.c
@@ -218,49 +218,39 @@ color_assign (GtkCssStyleProperty *property,
 }
 
 static GtkCssValue *
-font_family_parse (GtkCssStyleProperty *property,
-                   GtkCssParser        *parser,
-                   GFile               *base)
+font_family_parse_one (GtkCssParser *parser)
 {
-  GPtrArray *names;
-  GtkCssValue *result;
   char *name;
 
-  /* We don't special case generic families. Pango should do
-   * that for us */
+  name = _gtk_css_parser_try_ident (parser, TRUE);
+  if (name)
+    {
+      GString *string = g_string_new (name);
+      g_free (name);
+      while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
+        {
+          g_string_append_c (string, ' ');
+          g_string_append (string, name);
+          g_free (name);
+        }
+      name = g_string_free (string, FALSE);
+    }
+  else 
+    {
+      name = _gtk_css_parser_read_string (parser);
+      if (name == NULL)
+        return NULL;
+    }
 
-  names = g_ptr_array_new ();
+  return _gtk_css_string_value_new_take (name);
+}
 
-  do {
-    name = _gtk_css_parser_try_ident (parser, TRUE);
-    if (name)
-      {
-        GString *string = g_string_new (name);
-        g_free (name);
-        while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
-          {
-            g_string_append_c (string, ' ');
-            g_string_append (string, name);
-            g_free (name);
-          }
-        name = g_string_free (string, FALSE);
-      }
-    else 
-      {
-        name = _gtk_css_parser_read_string (parser);
-        if (name == NULL)
-          {
-            g_ptr_array_free (names, TRUE);
-            return FALSE;
-          }
-      }
-
-    g_ptr_array_add (names, _gtk_css_string_value_new_take (name));
-  } while (_gtk_css_parser_try (parser, ",", TRUE));
-
-  result = _gtk_css_array_value_new ((GtkCssValue **) names->pdata, names->len);
-  g_ptr_array_free (names, TRUE);
-  return result;
+static GtkCssValue *
+font_family_parse (GtkCssStyleProperty *property,
+                   GtkCssParser        *parser,
+                   GFile               *base)
+{
+  return _gtk_css_array_value_parse (parser, font_family_parse_one, FALSE);
 }
 
 static void
@@ -300,7 +290,7 @@ font_family_assign (GtkCssStyleProperty *property,
       g_ptr_array_add (array, _gtk_css_string_value_new (*names));
     }
 
-  result = _gtk_css_array_value_new ((GtkCssValue **) array->pdata, array->len);
+  result = _gtk_css_array_value_new_from_array ((GtkCssValue **) array->pdata, array->len);
   g_ptr_array_free (array, TRUE);
   return result;
 }
@@ -726,35 +716,33 @@ border_image_width_parse (GtkCssStyleProperty *property,
 }
 
 static GtkCssValue *
-transition_property_parse (GtkCssStyleProperty *property,
-                           GtkCssParser        *parser,
-                           GFile               *base)
+transition_property_parse_one (GtkCssParser *parser)
 {
-  GPtrArray *names;
-  GtkCssValue *result;
   char *name;
 
-  if (_gtk_css_parser_try (parser, "none", TRUE))
-    return _gtk_css_array_value_new (NULL, 0);
+  name = _gtk_css_parser_try_ident (parser, TRUE);
 
-  names = g_ptr_array_new ();
-
-  do {
-    name = _gtk_css_parser_try_ident (parser, TRUE);
+  if (!name)
+    {
+      _gtk_css_parser_error (parser, "Expected an identifier");
+      return NULL;
+    }
 
-    if (!name)
-      {
-        _gtk_css_parser_error (parser, "Expected an identifier");
-        g_ptr_array_free (names, TRUE);
-        return NULL;
-      }
+  return _gtk_css_string_value_new_take (name);
+}
 
-    g_ptr_array_add (names, _gtk_css_string_value_new_take (name));
-  } while (_gtk_css_parser_try (parser, ",", TRUE));
+static GtkCssValue *
+transition_property_parse (GtkCssStyleProperty *property,
+                           GtkCssParser        *parser,
+                           GFile               *base)
+{
+  return _gtk_css_array_value_parse (parser, transition_property_parse_one, TRUE);
+}
 
-  result = _gtk_css_array_value_new ((GtkCssValue **) names->pdata, names->len);
-  g_ptr_array_free (names, TRUE);
-  return result;
+static GtkCssValue *
+transition_time_parse_one (GtkCssParser *parser)
+{
+  return _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_TIME);
 }
 
 static GtkCssValue *
@@ -762,26 +750,7 @@ transition_time_parse (GtkCssStyleProperty *property,
                        GtkCssParser        *parser,
                        GFile               *base)
 {
-  GPtrArray *times;
-  GtkCssValue *result, *next;
-
-  times = g_ptr_array_new ();
-
-  do {
-    next = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_TIME);
-
-    if (next == NULL)
-      {
-        g_ptr_array_free (times, TRUE);
-        return NULL;
-      }
-
-    g_ptr_array_add (times, next);
-  } while (_gtk_css_parser_try (parser, ",", TRUE));
-
-  result = _gtk_css_array_value_new ((GtkCssValue **) times->pdata, times->len);
-  g_ptr_array_free (times, TRUE);
-  return result;
+  return _gtk_css_array_value_parse (parser, transition_time_parse_one, FALSE);
 }
 
 static GtkCssValue *
@@ -789,26 +758,7 @@ transition_timing_function_parse (GtkCssStyleProperty *property,
                                   GtkCssParser        *parser,
                                   GFile               *base)
 {
-  GPtrArray *funcs;
-  GtkCssValue *result, *next;
-
-  funcs = g_ptr_array_new ();
-
-  do {
-    next = _gtk_css_ease_value_parse (parser);
-
-    if (next == NULL)
-      {
-        g_ptr_array_free (funcs, TRUE);
-        return NULL;
-      }
-
-    g_ptr_array_add (funcs, next);
-  } while (_gtk_css_parser_try (parser, ",", TRUE));
-
-  result = _gtk_css_array_value_new ((GtkCssValue **) funcs->pdata, funcs->len);
-  g_ptr_array_free (funcs, TRUE);
-  return result;
+  return _gtk_css_array_value_parse (parser, _gtk_css_ease_value_parse, FALSE);
 }
 
 static GtkCssValue *
@@ -1272,7 +1222,6 @@ gtk_symbolic_color_new_rgba (double red,
 void
 _gtk_css_style_property_init_properties (void)
 {
-  GtkCssValue *value;
   GtkCssBackgroundSize default_background_size = { GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), FALSE, FALSE };
   GtkCssBackgroundPosition default_background_position = { GTK_CSS_NUMBER_INIT (0, GTK_CSS_PERCENT), GTK_CSS_NUMBER_INIT (0, GTK_CSS_PERCENT)};
   GtkCssBorderCornerRadius no_corner_radius = { GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX) };
@@ -1323,7 +1272,6 @@ _gtk_css_style_property_init_properties (void)
                                           _gtk_css_value_new_take_symbolic_color (
                                             gtk_symbolic_color_new_rgba (0, 0, 0, 0)));
 
-  value = _gtk_css_string_value_new ("Sans");
   gtk_css_style_property_register        ("font-family",
                                           GTK_CSS_PROPERTY_FONT_FAMILY,
                                           G_TYPE_STRV,
@@ -1334,7 +1282,7 @@ _gtk_css_style_property_init_properties (void)
                                           font_family_query,
                                           font_family_assign,
                                           NULL,
-                                          _gtk_css_array_value_new (&value, 1));
+                                          _gtk_css_array_value_new (_gtk_css_string_value_new ("Sans")));
   gtk_css_style_property_register        ("font-style",
                                           GTK_CSS_PROPERTY_FONT_STYLE,
                                           PANGO_TYPE_STYLE,
@@ -1857,8 +1805,7 @@ _gtk_css_style_property_init_properties (void)
                                           NULL,
                                           NULL,
                                           NULL,
-                                          _gtk_css_array_value_new (NULL, 0));
-  value = _gtk_css_number_value_new (0, GTK_CSS_S);
+                                          _gtk_css_array_value_new (NULL));
   gtk_css_style_property_register        ("transition-duration",
                                           GTK_CSS_PROPERTY_TRANSITION_DURATION,
                                           G_TYPE_NONE,
@@ -1869,8 +1816,7 @@ _gtk_css_style_property_init_properties (void)
                                           NULL,
                                           NULL,
                                           NULL,
-                                          _gtk_css_array_value_new (&value, 1));
-  value = _gtk_css_ease_value_new_cubic_bezier (0.25, 0.1, 0.25, 0.1);
+                                          _gtk_css_array_value_new (_gtk_css_number_value_new (0, GTK_CSS_S)));
   gtk_css_style_property_register        ("transition-timing-function",
                                           GTK_CSS_PROPERTY_TRANSITION_TIMING_FUNCTION,
                                           G_TYPE_NONE,
@@ -1881,8 +1827,8 @@ _gtk_css_style_property_init_properties (void)
                                           NULL,
                                           NULL,
                                           NULL,
-                                          _gtk_css_array_value_new (&value, 1));
-  value = _gtk_css_number_value_new (0, GTK_CSS_S);
+                                          _gtk_css_array_value_new (
+                                            _gtk_css_ease_value_new_cubic_bezier (0.25, 0.1, 0.25, 0.1)));
   gtk_css_style_property_register        ("transition-delay",
                                           GTK_CSS_PROPERTY_TRANSITION_DELAY,
                                           G_TYPE_NONE,
@@ -1893,7 +1839,7 @@ _gtk_css_style_property_init_properties (void)
                                           NULL,
                                           NULL,
                                           NULL,
-                                          _gtk_css_array_value_new (&value, 1));
+                                          _gtk_css_array_value_new (_gtk_css_number_value_new (0, GTK_CSS_S)));
 
   gtk_css_style_property_register        ("engine",
                                           GTK_CSS_PROPERTY_ENGINE,



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