[gtk+] builder: Simplify _gtk_builder_boolean_from_string
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc: 
- Subject: [gtk+] builder: Simplify _gtk_builder_boolean_from_string
- Date: Mon,  7 Sep 2015 17:26:46 +0000 (UTC)
commit 97a29ab8c45413c232ec5244b9c2a1df8e89ce36
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Sep 7 12:54:16 2015 -0400
    builder: Simplify _gtk_builder_boolean_from_string
    
    We can do this without calling strlen() or allocating memory.
 gtk/gtkbuilder.c |   40 +++++++++++++++++-----------------------
 1 files changed, 17 insertions(+), 23 deletions(-)
---
diff --git a/gtk/gtkbuilder.c b/gtk/gtkbuilder.c
index 0f04ed7..b149b9a 100644
--- a/gtk/gtkbuilder.c
+++ b/gtk/gtkbuilder.c
@@ -2270,15 +2270,9 @@ _gtk_builder_boolean_from_string (const gchar  *string,
                                   gboolean     *value,
                                   GError      **error)
 {
-  gboolean retval = TRUE;
-  int length;
-
-  g_assert (string != NULL);
-  length = strlen (string);
-
-  if (length == 0)
-    retval = FALSE;
-  else if (length == 1)
+  if (string[0] == '\0')
+    goto error;
+  else if (string[1] == '\0')
     {
       gchar c = g_ascii_tolower (string[0]);
       if (c == 'y' || c == 't' || c == '1')
@@ -2286,29 +2280,29 @@ _gtk_builder_boolean_from_string (const gchar  *string,
       else if (c == 'n' || c == 'f' || c == '0')
         *value = FALSE;
       else
-        retval = FALSE;
+        goto error;
     }
   else
     {
-      gchar *lower = g_ascii_strdown (string, length);
-
-      if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
+      if (g_ascii_strcasecmp (string, "true") == 0 ||
+          g_ascii_strcasecmp (string, "yes") == 0)
         *value = TRUE;
-      else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
+      else if (g_ascii_strcasecmp (string, "false") == 0 ||
+               g_ascii_strcasecmp (string, "no") == 0)
         *value = FALSE;
       else
-        retval = FALSE;
-      g_free (lower);
+        goto error;
     }
 
-  if (!retval)
-    g_set_error (error,
-                 GTK_BUILDER_ERROR,
-                 GTK_BUILDER_ERROR_INVALID_VALUE,
-                 "Could not parse boolean '%s'",
-                 string);
+  return TRUE;
 
-  return retval;
+error:
+  g_set_error (error,
+               GTK_BUILDER_ERROR,
+               GTK_BUILDER_ERROR_INVALID_VALUE,
+               "Could not parse boolean '%s'",
+               string);
+  return FALSE;
 }
 
 /**
[
Date Prev][
Date Next]   [
Thread Prev][
Thread Next]   
[
Thread Index]
[
Date Index]
[
Author Index]