find multi-byte comment chars in NUL-terminated strings

Several parts of the code need to identify lines that begin with the comment character, and do so with a simple byte equality check. As part of the transition to handling multi-byte characters, we need to match all of the bytes. For cases where we are looking in a NUL-terminated string, we can just use starts_with(), which checks all of the characters in comment_line_str. Note that we can drop the "line.len" check in wt-status.c's read_rebase_todolist(). The starts_with() function handles the case of an empty haystack buffer (it will always return false for a non-empty prefix). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 12, 2024 at 05:17 UTC 600559b7169c1353e3d46f773f39cf018d38ebbc
4 files changed +4 -4
add-patch.c
+1 -1
@@ -1139,7 +1139,7 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
1139 for (i = 0; i < s->buf.len; ) {
1140 size_t next = find_next_line(&s->buf, i);
1141
1142 - if (s->buf.buf[i] != comment_line_char)
1142 + if (!starts_with(s->buf.buf + i, comment_line_str))
1143 strbuf_add(&s->plain, s->buf.buf + i, next - i);
1144 i = next;
1145 }
sequencer.c
+1 -1
@@ -2003,7 +2003,7 @@ static int update_squash_messages(struct repository *r,
2003 return error(_("could not read '%s'"),
2004 rebase_path_squash_msg());
2005
2006 - eol = buf.buf[0] != comment_line_char ?
2006 + eol = !starts_with(buf.buf, comment_line_str) ?
2007 buf.buf : strchrnul(buf.buf, '\n');
2008
2009 strbuf_addf(&header, "%s ", comment_line_str);
trailer.c
+1 -1
@@ -1013,7 +1013,7 @@ static void parse_trailers(struct trailer_info *info,
1013 for (i = 0; i < info->trailer_nr; i++) {
1014 int separator_pos;
1015 char *trailer = info->trailers[i];
1016 - if (trailer[0] == comment_line_char)
1016 + if (starts_with(trailer, comment_line_str))
1017 continue;
1018 separator_pos = find_separator(trailer, separators);
1019 if (separator_pos >= 1) {
wt-status.c
+1 -1
@@ -1382,7 +1382,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines)
1382 git_path("%s", fname));
1383 }
1384 while (!strbuf_getline_lf(&line, f)) {
1385 - if (line.len && line.buf[0] == comment_line_char)
1385 + if (starts_with(line.buf, comment_line_str))
1386 continue;
1387 strbuf_trim(&line);
1388 if (!line.len)