[template-glib] eval: auto convert to string for addition



commit 21c55c84fcc4617251ca768b6879e25e51255307
Author: Christian Hergert <chergert redhat com>
Date:   Fri Oct 27 03:22:12 2017 -0700

    eval: auto convert to string for addition
    
    This is fine for a templating language since text output is primarily
    what we want to deal with, and I don't like the idea of adding str()
    functions around everything.

 src/tmpl-expr-eval.c |   42 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 42 insertions(+), 0 deletions(-)
---
diff --git a/src/tmpl-expr-eval.c b/src/tmpl-expr-eval.c
index 6441f9f..3003e26 100644
--- a/src/tmpl-expr-eval.c
+++ b/src/tmpl-expr-eval.c
@@ -79,6 +79,10 @@ static gboolean ne_enum_string               (const GValue  *left,
                                               const GValue  *right,
                                               GValue        *return_value,
                                               GError       **error);
+static gboolean add_string_string_slow       (const GValue  *left,
+                                              const GValue  *right,
+                                              GValue        *return_value,
+                                              GError       **error);
 
 static GHashTable *fast_dispatch;
 static BuiltinFunc builtin_funcs [] = {
@@ -200,6 +204,12 @@ find_dispatch_slow (TmplExprSimple *node,
         return ne_gtype_gtype;
     }
 
+  if (node->type == TMPL_EXPR_ADD)
+    {
+      if (G_VALUE_HOLDS_STRING (left) || G_VALUE_HOLDS_STRING (right))
+        return add_string_string_slow;
+    }
+
   return NULL;
 }
 
@@ -1351,6 +1361,38 @@ add_string_string (const GValue  *left,
 }
 
 static gboolean
+add_string_string_slow (const GValue  *left,
+                        const GValue  *right,
+                        GValue        *return_value,
+                        GError       **error)
+{
+  GValue trans = G_VALUE_INIT;
+
+  g_value_init (&trans, G_TYPE_STRING);
+  g_value_init (return_value, G_TYPE_STRING);
+
+  if (G_VALUE_HOLDS_STRING (left))
+    {
+      if (!g_value_transform (right, &trans))
+        return FALSE;
+      right = &trans;
+    }
+  else
+    {
+      if (!g_value_transform (left, &trans))
+        return FALSE;
+      left = &trans;
+    }
+
+  g_value_take_string (return_value,
+                       g_strdup_printf ("%s%s",
+                                        g_value_get_string (left),
+                                        g_value_get_string (right)));
+
+  return TRUE;
+}
+
+static gboolean
 eq_string_string (const GValue  *left,
                   const GValue  *right,
                   GValue        *return_value,


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