[gnome-builder] css: use \0 terminated strings for CSS loading



commit e8671ede009258be64f31f142bd297f7ed459140
Author: Christian Hergert <chergert redhat com>
Date:   Sun Nov 12 16:41:25 2017 -0800

    css: use \0 terminated strings for CSS loading
    
    By using -1, we avoid a string copy in the CSS provider.

 src/libide/keybindings/ide-keybindings.c    |   15 +++++++++++----
 src/libide/layout/ide-layout-stack-header.c |    5 ++++-
 2 files changed, 15 insertions(+), 5 deletions(-)
---
diff --git a/src/libide/keybindings/ide-keybindings.c b/src/libide/keybindings/ide-keybindings.c
index 446a82e..9494387 100644
--- a/src/libide/keybindings/ide-keybindings.c
+++ b/src/libide/keybindings/ide-keybindings.c
@@ -146,10 +146,17 @@ ide_keybindings_reload (IdeKeybindings *self)
     bytes = g_resources_lookup_data (path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
 
     if (error == NULL)
-      gtk_css_provider_load_from_data (self->css_provider,
-                                       g_bytes_get_data (bytes, NULL),
-                                       g_bytes_get_size (bytes),
-                                       &error);
+      {
+        /*
+         * We use -1 for the length so that the CSS provider knows that the
+         * string is \0 terminated. This is guaranteed to us by GResources so
+         * that interned data can be used as C strings.
+         */
+        gtk_css_provider_load_from_data (self->css_provider,
+                                         g_bytes_get_data (bytes, NULL),
+                                         -1,
+                                         &error);
+      }
 
     if (error)
       g_warning ("%s", error->message);
diff --git a/src/libide/layout/ide-layout-stack-header.c b/src/libide/layout/ide-layout-stack-header.c
index 72f79eb..3f7e3fe 100644
--- a/src/libide/layout/ide-layout-stack-header.c
+++ b/src/libide/layout/ide-layout-stack-header.c
@@ -358,7 +358,10 @@ ide_layout_stack_header_update_css (IdeLayoutStackHeader *self)
         }
     }
 
-  if (!gtk_css_provider_load_from_data (self->css_provider, str->str, str->len, &error))
+  /* Use -1 for length so CSS provider knows the string is NULL terminated
+   * and there-by avoid a string copy.
+   */
+  if (!gtk_css_provider_load_from_data (self->css_provider, str->str, -1, &error))
     g_warning ("Failed to load CSS: '%s': %s", str->str, error->message);
 
   self->update_css_handler = 0;


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