diff --git a/src/parser.c b/src/parser.c index 447e1b4a..f0e76e59 100644 --- a/src/parser.c +++ b/src/parser.c @@ -75,6 +75,12 @@ parse_args(const char * const inp, int min, int max, int *num) *num = 0; return NULL; + // if min allowed is 0 and 0 found, return empty char* array + } else if (min == 0 && *num == 0) { + gchar **args = malloc((*num + 1) * sizeof(*args)); + args[0] = NULL; + return args; + // otherwise return args array } else { gchar **args = malloc((*num + 1) * sizeof(*args)); @@ -151,6 +157,12 @@ parse_args_with_freetext(const char * const inp, int min, int max, int *num) *num = 0; return NULL; + // if min allowed is 0 and 0 found, return empty char* array + } else if (min == 0 && *num == 0) { + gchar **args = malloc((*num + 1) * sizeof(*args)); + args[0] = NULL; + return args; + // otherwise return args array } else { gchar **args = malloc((*num + 1) * sizeof(*args)); diff --git a/src/parser.h b/src/parser.h index a0442f33..a8a52e22 100644 --- a/src/parser.h +++ b/src/parser.h @@ -26,5 +26,6 @@ #include gchar** parse_args(const char * const inp, int min, int max, int *num); +gchar** parse_args_with_freetext(const char * const inp, int min, int max, int *num); #endif diff --git a/tests/test_parser.c b/tests/test_parser.c index a3e31ed6..9a692edc 100644 --- a/tests/test_parser.c +++ b/tests/test_parser.c @@ -172,6 +172,30 @@ parse_cmd_two_args_with_freetext(void) g_strfreev(result); } +void +parse_cmd_min_zero(void) +{ + char *inp = "/cmd"; + int num = 0; + gchar **result = parse_args(inp, 0, 2, &num); + + assert_int_equals(0, num); + assert_is_null(result[0]); + g_strfreev(result); +} + +void +parse_cmd_min_zero_with_freetext(void) +{ + char *inp = "/cmd"; + int num = 0; + gchar **result = parse_args_with_freetext(inp, 0, 2, &num); + + assert_int_equals(0, num); + assert_is_null(result[0]); + g_strfreev(result); +} + void register_parser_tests(void) { @@ -190,4 +214,6 @@ register_parser_tests(void) TEST(parse_cmd_two_args_with_freetext); TEST(parse_cmd_with_too_few_returns_null); TEST(parse_cmd_with_too_many_returns_null); + TEST(parse_cmd_min_zero); + TEST(parse_cmd_min_zero_with_freetext); }