sequencer: factor out todo command name parsing

Factor out the code that parses the name of the command at the start of each line in the todo file into its own function so that it can be used in the next commit. As I don't want to burden other callers with having to pass in a pointer to the end of the line the test for an abbreviated command is changed. This change should not affect the behavior. Instead of testing `eol == bol + 1` the new code checks for the end of the line by testing for '\n', '\r' or '\0' following the abbreviated name. To avoid reading past the end of an empty string it also checks that there is actually a single character abbreviation before testing if it matches. This prevents it from matching '\0' as the abbreviated name and then trying to read another character. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jun 27, 2019 at 07:12 UTC 3e81bccdf3ceab531e95ba3846083dbc8ba0e319
1 file changed +13 -6
sequencer.c
+13 -6
@@ -2076,6 +2076,18 @@ const char *todo_item_get_arg(struct todo_list *todo_list,
2076 return todo_list->buf.buf + item->arg_offset;
2077 }
2078
2079 +static int is_command(enum todo_command command, const char **bol)
2080 +{
2081 + const char *str = todo_command_info[command].str;
2082 + const char nick = todo_command_info[command].c;
2083 + const char *p = *bol + 1;
2084 +
2085 + return skip_prefix(*bol, str, bol) ||
2086 + ((nick && **bol == nick) &&
2087 + (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) &&
2088 + (*bol = p));
2089 +}
2090 +
2091 static int parse_insn_line(struct repository *r, struct todo_item *item,
2092 const char *buf, const char *bol, char *eol)
2093 {
@@ -2097,12 +2109,7 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
2109 }
2110
2111 for (i = 0; i < TODO_COMMENT; i++)
2100 - if (skip_prefix(bol, todo_command_info[i].str, &bol)) {
2101 - item->command = i;
2102 - break;
2103 - } else if ((bol + 1 == eol || bol[1] == ' ' || bol[1] == '\t') &&
2104 - *bol == todo_command_info[i].c) {
2105 - bol++;
2112 + if (is_command(i, &bol)) {
2113 item->command = i;
2114 break;
2115 }