diff.c: refactor parse_ws_error_highlight()
Rename the function to parse_ws_error_highlight_opt(), because it is meant to parse a command line option, and then refactor the meat of the function into a helper function that reports the parsed result which is typically a small unsigned int (these are OR'ed bitmask after all), or a negative offset that indicates where in the input string a parse error happened. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Oct 4, 2016 at 15:03 UTC
077965f84a580b7e1de7d60ed13656bec19cc2fb
1 file changed
+16
-5
diff.c
+16
-5
@@ -3666,10 +3666,11 @@ static int parse_one_token(const char **arg, const char *token)
3666
return 0;
3667
}
3668
3669
-static int parse_ws_error_highlight(struct diff_options *opt, const char *arg)
3669
+static int parse_ws_error_highlight(const char *arg)
3670
{
3671
const char *orig_arg = arg;
3672
unsigned val = 0;
3673
+
3674
while (*arg) {
3675
if (parse_one_token(&arg, "none"))
3676
val = 0;
@@ -3684,13 +3685,23 @@ static int parse_ws_error_highlight(struct diff_options *opt, const char *arg)
3685
else if (parse_one_token(&arg, "context"))
3686
val |= WSEH_CONTEXT;
3687
else {
3687
- error("unknown value after ws-error-highlight=%.*s",
3688
- (int)(arg - orig_arg), orig_arg);
3689
- return 0;
3688
+ return -1 - (int)(arg - orig_arg);
3689
}
3690
if (*arg)
3691
arg++;
3692
}
3693
+ return val;
3694
+}
3695
+
3696
+static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *arg)
3697
+{
3698
+ int val = parse_ws_error_highlight(arg);
3699
+
3700
+ if (val < 0) {
3701
+ error("unknown value after ws-error-highlight=%.*s",
3702
+ -1 - val, arg);
3703
+ return 0;
3704
+ }
3705
opt->ws_error_highlight = val;
3706
return 1;
3707
}
@@ -3894,7 +3905,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3905
else if (skip_prefix(arg, "--submodule=", &arg))
3906
return parse_submodule_opt(options, arg);
3907
else if (skip_prefix(arg, "--ws-error-highlight=", &arg))
3897
- return parse_ws_error_highlight(options, arg);
3908
+ return parse_ws_error_highlight_opt(options, arg);
3909
3910
/* misc options */
3911
else if (!strcmp(arg, "-z"))