builder: Simplify _gtk_builder_boolean_from_string

We can do this without calling strlen() or allocating memory.
This commit is contained in:
Matthias Clasen 2015-09-07 12:54:16 -04:00
parent 3d01f29bd9
commit 97a29ab8c4

View File

@ -2270,15 +2270,9 @@ _gtk_builder_boolean_from_string (const gchar *string,
gboolean *value, gboolean *value,
GError **error) GError **error)
{ {
gboolean retval = TRUE; if (string[0] == '\0')
int length; goto error;
else if (string[1] == '\0')
g_assert (string != NULL);
length = strlen (string);
if (length == 0)
retval = FALSE;
else if (length == 1)
{ {
gchar c = g_ascii_tolower (string[0]); gchar c = g_ascii_tolower (string[0]);
if (c == 'y' || c == 't' || c == '1') 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') else if (c == 'n' || c == 'f' || c == '0')
*value = FALSE; *value = FALSE;
else else
retval = FALSE; goto error;
} }
else else
{ {
gchar *lower = g_ascii_strdown (string, length); if (g_ascii_strcasecmp (string, "true") == 0 ||
g_ascii_strcasecmp (string, "yes") == 0)
if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
*value = TRUE; *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; *value = FALSE;
else else
retval = FALSE; goto error;
g_free (lower);
} }
if (!retval) return TRUE;
error:
g_set_error (error, g_set_error (error,
GTK_BUILDER_ERROR, GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE, GTK_BUILDER_ERROR_INVALID_VALUE,
"Could not parse boolean '%s'", "Could not parse boolean '%s'",
string); string);
return FALSE;
return retval;
} }
/** /**