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 c8deb5a146 ("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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 22, 2019 at 12:03 UTC 39c575c96967f325a995cf9716a46f7e924714f5
1 file changed +12 -18
config.c
+12 -18
@@ -834,24 +834,16 @@ static int git_parse_source(config_fn_t fn, void *data,
834 return error_return;
835 }
836
837 -static int parse_unit_factor(const char *end, uintmax_t *factor)
837 +static uintmax_t get_unit_factor(const char *end)
838 {
839 - if (!*end) {
840 - *factor = 1;
839 + if (!*end)
840 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;
847 return 0;
848 }
849
@@ -867,7 +859,8 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
859 val = strtoimax(value, &end, 0);
860 if (errno == ERANGE)
861 return 0;
870 - if (!parse_unit_factor(end, &factor)) {
862 + factor = get_unit_factor(end);
863 + if (!factor) {
864 errno = EINVAL;
865 return 0;
866 }
@@ -896,7 +889,8 @@ static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
889 val = strtoumax(value, &end, 0);
890 if (errno == ERANGE)
891 return 0;
899 - if (!parse_unit_factor(end, &factor)) {
892 + factor = get_unit_factor(end);
893 + if (!factor) {
894 errno = EINVAL;
895 return 0;
896 }