Skip to content

Commit 077965f

Browse files
committed
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>
1 parent f3f5c7f commit 077965f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

diff.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,10 +3666,11 @@ static int parse_one_token(const char **arg, const char *token)
36663666
return 0;
36673667
}
36683668

3669-
static int parse_ws_error_highlight(struct diff_options *opt, const char *arg)
3669+
static int parse_ws_error_highlight(const char *arg)
36703670
{
36713671
const char *orig_arg = arg;
36723672
unsigned val = 0;
3673+
36733674
while (*arg) {
36743675
if (parse_one_token(&arg, "none"))
36753676
val = 0;
@@ -3684,13 +3685,23 @@ static int parse_ws_error_highlight(struct diff_options *opt, const char *arg)
36843685
else if (parse_one_token(&arg, "context"))
36853686
val |= WSEH_CONTEXT;
36863687
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);
36903689
}
36913690
if (*arg)
36923691
arg++;
36933692
}
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+
}
36943705
opt->ws_error_highlight = val;
36953706
return 1;
36963707
}
@@ -3894,7 +3905,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
38943905
else if (skip_prefix(arg, "--submodule=", &arg))
38953906
return parse_submodule_opt(options, arg);
38963907
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);
38983909

38993910
/* misc options */
39003911
else if (!strcmp(arg, "-z"))

0 commit comments

Comments
 (0)