Skip to content

Commit 39c575c

Browse files
rscharfegitster
authored andcommitted
config: simplify parsing of unit factors
Just return the value of the factor or zero for unrecognized strings instead of using an output reference and a separate return value to indicate success. This is shorter and simpler. It basically reverts that function to before c8deb5a ("Improve error messages when int/long cannot be parsed from config", 2007-12-25), while keeping the better messages, so restore its old name, get_unit_factor(), as well. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 664178e commit 39c575c

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

config.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -834,24 +834,16 @@ static int git_parse_source(config_fn_t fn, void *data,
834834
return error_return;
835835
}
836836

837-
static int parse_unit_factor(const char *end, uintmax_t *factor)
837+
static uintmax_t get_unit_factor(const char *end)
838838
{
839-
if (!*end) {
840-
*factor = 1;
839+
if (!*end)
841840
return 1;
842-
}
843-
else if (!strcasecmp(end, "k")) {
844-
*factor = 1024;
845-
return 1;
846-
}
847-
else if (!strcasecmp(end, "m")) {
848-
*factor = 1024 * 1024;
849-
return 1;
850-
}
851-
else if (!strcasecmp(end, "g")) {
852-
*factor = 1024 * 1024 * 1024;
853-
return 1;
854-
}
841+
else if (!strcasecmp(end, "k"))
842+
return 1024;
843+
else if (!strcasecmp(end, "m"))
844+
return 1024 * 1024;
845+
else if (!strcasecmp(end, "g"))
846+
return 1024 * 1024 * 1024;
855847
return 0;
856848
}
857849

@@ -867,7 +859,8 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
867859
val = strtoimax(value, &end, 0);
868860
if (errno == ERANGE)
869861
return 0;
870-
if (!parse_unit_factor(end, &factor)) {
862+
factor = get_unit_factor(end);
863+
if (!factor) {
871864
errno = EINVAL;
872865
return 0;
873866
}
@@ -896,7 +889,8 @@ static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
896889
val = strtoumax(value, &end, 0);
897890
if (errno == ERANGE)
898891
return 0;
899-
if (!parse_unit_factor(end, &factor)) {
892+
factor = get_unit_factor(end);
893+
if (!factor) {
900894
errno = EINVAL;
901895
return 0;
902896
}

0 commit comments

Comments
 (0)