[gtk/wip/baedert/for-master] widget: Avoid allocating GStrings for accessibility data




commit 0693582b869e2abd52a16fe7a604047e4d4a2121
Author: Timm Bäder <mail baedert org>
Date:   Wed Jan 6 10:42:37 2021 +0100

    widget: Avoid allocating GStrings for accessibility data
    
    This works, right?

 gtk/gtkwidget.c | 68 +++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 24 deletions(-)
---
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 239f7735e8..df5bdf44d9 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -8608,9 +8608,10 @@ static const GtkBuildableParser layout_parser =
 typedef struct
 {
   char *name;
-  GString *value;
   char *context;
   gboolean translatable;
+  const char *value; /* Not nul-terminated */
+  gsize value_len;
 } AccessibilityAttributeInfo;
 
 typedef struct
@@ -8636,7 +8637,6 @@ accessibility_attribute_info_free (gpointer data)
 
   g_free (pinfo->name);
   g_free (pinfo->context);
-  g_string_free (pinfo->value, TRUE);
   g_free (pinfo);
 }
 
@@ -8679,7 +8679,6 @@ accessibility_start_element (GtkBuildableParseContext  *context,
       pinfo->name = g_strdup (name);
       pinfo->translatable = translatable;
       pinfo->context = g_strdup (ctx);
-      pinfo->value = g_string_new (NULL);
 
       accessibility_data->cur_attribute = pinfo;
     }
@@ -8709,7 +8708,17 @@ accessibility_text (GtkBuildableParseContext  *context,
   AccessibilityParserData *accessibility_data = user_data;
 
   if (accessibility_data->cur_attribute != NULL)
-    g_string_append_len (accessibility_data->cur_attribute->value, text, text_len);
+    {
+      accessibility_data->cur_attribute->value = text;
+      accessibility_data->cur_attribute->value_len = text_len;
+    }
+}
+
+static char *
+nul_terminate (const char *str,
+               gsize       len)
+{
+  return g_strdup_printf ("%.*s", (int)len, str);
 }
 
 static void
@@ -8725,16 +8734,19 @@ accessibility_end_element (GtkBuildableParseContext  *context,
       AccessibilityAttributeInfo *pinfo = g_steal_pointer (&accessibility_data->cur_attribute);
 
       /* Translate the string, if needed */
-      if (pinfo->value->len != 0 && pinfo->translatable)
+      if (pinfo->value_len != 0 && pinfo->translatable)
         {
           const char *translated;
           const char *domain;
+          char *str;
 
           domain = gtk_builder_get_translation_domain (accessibility_data->builder);
 
-          translated = _gtk_builder_parser_translate (domain, pinfo->context, pinfo->value->str);
-
-          g_string_assign (pinfo->value, translated);
+          str = nul_terminate (pinfo->value, pinfo->value_len);
+          translated = _gtk_builder_parser_translate (domain, pinfo->context, pinfo->value);
+          pinfo->value = translated;
+          pinfo->value_len = strlen (translated);
+          g_free (str);
         }
 
       /* We assign all properties at the end of the `accessibility` section */
@@ -8923,14 +8935,15 @@ gtk_widget_buildable_finish_accessibility_properties (GtkWidget *widget,
         }
 
       value = gtk_accessible_value_parse_for_property (property,
-                                                       pinfo->value->str,
-                                                       pinfo->value->len,
+                                                       pinfo->value,
+                                                       pinfo->value_len,
                                                        &error);
       if (error != NULL)
         {
-          g_warning ("Failed to set accessible property “%s” to “%s”: %s",
+          g_warning ("Failed to set accessible property “%s” to “%.*s”: %s",
                      pinfo->name,
-                     pinfo->value->str,
+                     (int)pinfo->value_len,
+                     pinfo->value,
                      error->message);
           g_error_free (error);
           continue;
@@ -8966,14 +8979,15 @@ gtk_widget_buildable_finish_accessibility_properties (GtkWidget *widget,
         }
 
       value = gtk_accessible_value_parse_for_relation (relation,
-                                                       pinfo->value->str,
-                                                       pinfo->value->len,
+                                                       pinfo->value,
+                                                       pinfo->value_len,
                                                        &error);
       if (error != NULL)
         {
-          g_warning ("Failed to set accessible relation “%s” to “%s”: %s",
+          g_warning ("Failed to set accessible relation “%s” to “%.*s”: %s",
                      pinfo->name,
-                     pinfo->value->str,
+                     (int)pinfo->value_len,
+                     pinfo->value,
                      error->message);
           g_error_free (error);
           continue;
@@ -8981,13 +8995,18 @@ gtk_widget_buildable_finish_accessibility_properties (GtkWidget *widget,
 
       if (value == NULL)
         {
-          GObject *obj = gtk_builder_get_object (accessibility_data->builder,
-                                                 pinfo->value->str);
+          GObject *obj;
+          char *str;
+
+          str = nul_terminate (pinfo->value, pinfo->value_len);
+          obj = gtk_builder_get_object (accessibility_data->builder, str);
+          g_free (str);
 
           if (obj == NULL)
             {
-              g_warning ("Failed to find accessible object “%s” for relation “%s”",
-                         pinfo->value->str,
+              g_warning ("Failed to find accessible object “%.*s” for relation “%s”",
+                         (int)pinfo->value_len,
+                         pinfo->value,
                          pinfo->name);
               continue;
             }
@@ -9026,14 +9045,15 @@ gtk_widget_buildable_finish_accessibility_properties (GtkWidget *widget,
         }
 
       value = gtk_accessible_value_parse_for_state (state,
-                                                    pinfo->value->str,
-                                                    pinfo->value->len,
+                                                    pinfo->value,
+                                                    pinfo->value_len,
                                                     &error);
       if (error != NULL)
         {
-          g_warning ("Failed to set accessible state “%s” to “%s”: %s",
+          g_warning ("Failed to set accessible state “%s” to “%.*s”: %s",
                      pinfo->name,
-                     pinfo->value->str,
+                     (int)pinfo->value_len,
+                     pinfo->value,
                      error->message);
           g_error_free (error);
           continue;


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