diff options
author | Thomas Haller <thaller@redhat.com> | 2019-04-02 12:24:40 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-04-04 21:01:15 +0200 |
commit | df89f57c1dd1b916eb7ff9b01e92584dc43066a4 (patch) | |
tree | dfdce56cf2859e121e5fd54ee65964b9e863566d /shared/nm-utils | |
parent | 2861a7ae0a7ceaa8b0d6726f5ca88b7c2841a928 (diff) | |
download | NetworkManager-df89f57c1dd1b916eb7ff9b01e92584dc43066a4.tar.gz |
shared: cleanup _nm_utils_ascii_str_to_bool()
Previously, this would re-implement what nm_strstrip_avoid_copy()
was doing.
Use nm_strstrip_avoid_copy_a() instead, which avoids the code
duplication and the heap allocation in common cases.
Diffstat (limited to 'shared/nm-utils')
-rw-r--r-- | shared/nm-utils/nm-shared-utils.c | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index 33d7ad12f2..5f317e4144 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -1181,31 +1181,27 @@ int _nm_utils_ascii_str_to_bool (const char *str, int default_value) { - gsize len; - char *s = NULL; + gs_free char *str_free = NULL; if (!str) return default_value; - while (str[0] && g_ascii_isspace (str[0])) - str++; - - if (!str[0]) + str = nm_strstrip_avoid_copy_a (300, str, &str_free); + if (str[0] == '\0') return default_value; - len = strlen (str); - if (g_ascii_isspace (str[len - 1])) { - s = g_strdup (str); - g_strchomp (s); - str = s; - } + if ( !g_ascii_strcasecmp (str, "true") + || !g_ascii_strcasecmp (str, "yes") + || !g_ascii_strcasecmp (str, "on") + || !g_ascii_strcasecmp (str, "1")) + return TRUE; + + if ( !g_ascii_strcasecmp (str, "false") + || !g_ascii_strcasecmp (str, "no") + || !g_ascii_strcasecmp (str, "off") + || !g_ascii_strcasecmp (str, "0")) + return FALSE; - if (!g_ascii_strcasecmp (str, "true") || !g_ascii_strcasecmp (str, "yes") || !g_ascii_strcasecmp (str, "on") || !g_ascii_strcasecmp (str, "1")) - default_value = TRUE; - else if (!g_ascii_strcasecmp (str, "false") || !g_ascii_strcasecmp (str, "no") || !g_ascii_strcasecmp (str, "off") || !g_ascii_strcasecmp (str, "0")) - default_value = FALSE; - if (s) - g_free (s); return default_value; } |