]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
util: introduce is_null_or_empty() to avoid strlen()
authorCaleb Sander Mateos <csander@purestorage.com>
Tue, 16 Jul 2024 21:43:18 +0000 (15:43 -0600)
committerDaniel Wagner <wagi@monom.org>
Wed, 17 Jul 2024 14:57:45 +0000 (16:57 +0200)
argconfig uses strlen() in several places to check for empty strings.
strlen() traverses the entire string though only the first char matters.

Add a helper function is_null_or_empty() that only checks the first char
for NUL. It also encapsulates the NULL check performed by each caller.

Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
util/argconfig.c

index 5cd5889a673e5bc34eb2f500c3f494c151d3845b..3424e6913d409c89cbe4afa2d4a484f016aee49c 100644 (file)
 #include <stdbool.h>
 #include <locale.h>
 
+static bool is_null_or_empty(const char *s)
+{
+       return !s || !*s;
+}
+
 static const char *append_usage_str = "";
 
 void argconfig_append_usage(const char *str)
@@ -320,7 +325,7 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
                        if (s->argument_type == optional_argument)
                                short_opts[short_index++] = ':';
                }
-               if (s->option && strlen(s->option)) {
+               if (!is_null_or_empty(s->option)) {
                        long_opts[option_index].name = s->option;
                        long_opts[option_index].has_arg = s->argument_type;
                }
@@ -374,7 +379,7 @@ int argconfig_parse_comma_sep_array(char *string, int *val, unsigned int max_len
        char *tmp;
        char *p;
 
-       if (!string || !strlen(string))
+       if (is_null_or_empty(string))
                return 0;
 
        tmp = strtok(string, ",");
@@ -420,7 +425,7 @@ int argconfig_parse_comma_sep_array_short(char *string, unsigned short *val,
        char *tmp;
        char *p;
 
-       if (!string || !strlen(string))
+       if (is_null_or_empty(string))
                return 0;
 
        tmp = strtok(string, ",");
@@ -464,7 +469,7 @@ int argconfig_parse_comma_sep_array_long(char *string, unsigned long long *val,
        char *tmp;
        char *p;
 
-       if (!string || !strlen(string))
+       if (is_null_or_empty(string))
                return 0;
 
        tmp = strtok(string, ",");
@@ -501,7 +506,7 @@ int argconfig_parse_comma_sep_array_u##size(char *string,           \
        char *tmp;                                                      \
        char *p;                                                        \
                                                                        \
-       if (!string || !strlen(string))                         \
+       if (is_null_or_empty(string))                                   \
                return 0;                                               \
                                                                        \
        tmp = strtok(string, ",");                                      \