]> git.ipfire.org Git - thirdparty/git.git/commitdiff
find multi-byte comment chars in NUL-terminated strings
authorJeff King <peff@peff.net>
Tue, 12 Mar 2024 09:17:37 +0000 (05:17 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Mar 2024 20:28:10 +0000 (13:28 -0700)
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>
add-patch.c
sequencer.c
trailer.c
wt-status.c

index 4a10237d50eb2568801b78b397af8a635623ad02..d599ca53e19d01ff35657c8d03d16c274f42a5da 100644 (file)
@@ -1139,7 +1139,7 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
        for (i = 0; i < s->buf.len; ) {
                size_t next = find_next_line(&s->buf, i);
 
-               if (s->buf.buf[i] != comment_line_char)
+               if (!starts_with(s->buf.buf + i, comment_line_str))
                        strbuf_add(&s->plain, s->buf.buf + i, next - i);
                i = next;
        }
index 241e185f87fb59bf9ef301f425d2494d2584962c..991a2dbe9628252053a13606c872a0b1b36f3036 100644 (file)
@@ -2003,7 +2003,7 @@ static int update_squash_messages(struct repository *r,
                        return error(_("could not read '%s'"),
                                rebase_path_squash_msg());
 
-               eol = buf.buf[0] != comment_line_char ?
+               eol = !starts_with(buf.buf, comment_line_str) ?
                        buf.buf : strchrnul(buf.buf, '\n');
 
                strbuf_addf(&header, "%s ", comment_line_str);
index ef9df4af558fbb18849279cff2e67507026af106..fe18faf6c506d927bf232d671899c53ea0790648 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -1013,7 +1013,7 @@ static void parse_trailers(struct trailer_info *info,
        for (i = 0; i < info->trailer_nr; i++) {
                int separator_pos;
                char *trailer = info->trailers[i];
-               if (trailer[0] == comment_line_char)
+               if (starts_with(trailer, comment_line_str))
                        continue;
                separator_pos = find_separator(trailer, separators);
                if (separator_pos >= 1) {
index b66c30775b59da78bb0e762f3757d144a9ecb372..084bfc584f7944868cdf5251cd72e09d4ae772a7 100644 (file)
@@ -1382,7 +1382,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines)
                          git_path("%s", fname));
        }
        while (!strbuf_getline_lf(&line, f)) {
-               if (line.len && line.buf[0] == comment_line_char)
+               if (starts_with(line.buf, comment_line_str))
                        continue;
                strbuf_trim(&line);
                if (!line.len)