git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Matheus Tavares <matheus.bernardino@usp.br>, git@vger.kernel.org
Cc: gitster@pobox.com, avarab@gmail.com, l.s.r@web.de
Subject: Re: [PATCH v2 1/2] patchbreak(), is_scissors_line(): work with a buf/len pair
Date: Wed, 7 Sep 2022 19:20:31 +0100	[thread overview]
Message-ID: <435b2b8b-2577-0af4-6f69-b250f64cbdf8@gmail.com> (raw)
In-Reply-To: <99012733e440be15afc7fd45272e738c71b3ef27.1662559356.git.matheus.bernardino@usp.br>

On 07/09/2022 15:44, Matheus Tavares wrote:
> From: René Scharfe <l.s.r@web.de>
> 
> The next patch will add calls to these two functions from code that
> works with a char */size_t pair. So let's adapt the functions in
> preparation.

Reading this I wonder if we should add a starts_with_mem() function, 
rather than having to pass pointers to buf and len to skip_prefix_mem().

Best Wishes

Phillip

> ---
>   mailinfo.c | 37 +++++++++++++++++++------------------
>   1 file changed, 19 insertions(+), 18 deletions(-)
> 
> diff --git a/mailinfo.c b/mailinfo.c
> index 9621ba62a3..f0a690b6e8 100644
> --- a/mailinfo.c
> +++ b/mailinfo.c
> @@ -646,32 +646,30 @@ static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line)
>   	free(ret);
>   }
>   
> -static inline int patchbreak(const struct strbuf *line)
> +static inline int patchbreak(const char *buf, size_t len)
>   {
> -	size_t i;
> -
>   	/* Beginning of a "diff -" header? */
> -	if (starts_with(line->buf, "diff -"))
> +	if (skip_prefix_mem(buf, len, "diff -", &buf, &len))
>   		return 1;
>   
>   	/* CVS "Index: " line? */
> -	if (starts_with(line->buf, "Index: "))
> +	if (skip_prefix_mem(buf, len, "Index: ", &buf, &len))
>   		return 1;
>   
>   	/*
>   	 * "--- <filename>" starts patches without headers
>   	 * "---<sp>*" is a manual separator
>   	 */
> -	if (line->len < 4)
> +	if (len < 4)
>   		return 0;
>   
> -	if (starts_with(line->buf, "---")) {
> +	if (skip_prefix_mem(buf, len, "---", &buf, &len)) {
>   		/* space followed by a filename? */
> -		if (line->buf[3] == ' ' && !isspace(line->buf[4]))
> +		if (len > 1 && buf[0] == ' ' && !isspace(buf[1]))
>   			return 1;
>   		/* Just whitespace? */
> -		for (i = 3; i < line->len; i++) {
> -			unsigned char c = line->buf[i];
> +		for (; len; buf++, len--) {
> +			unsigned char c = buf[0];
>   			if (c == '\n')
>   				return 1;
>   			if (!isspace(c))
> @@ -682,14 +680,14 @@ static inline int patchbreak(const struct strbuf *line)
>   	return 0;
>   }
>   
> -static int is_scissors_line(const char *line)
> +static int is_scissors_line(const char *line, size_t len)
>   {
>   	const char *c;
>   	int scissors = 0, gap = 0;
>   	const char *first_nonblank = NULL, *last_nonblank = NULL;
>   	int visible, perforation = 0, in_perforation = 0;
>   
> -	for (c = line; *c; c++) {
> +	for (c = line; len; c++, len--) {
>   		if (isspace(*c)) {
>   			if (in_perforation) {
>   				perforation++;
> @@ -705,12 +703,14 @@ static int is_scissors_line(const char *line)
>   			perforation++;
>   			continue;
>   		}
> -		if (starts_with(c, ">8") || starts_with(c, "8<") ||
> -		    starts_with(c, ">%") || starts_with(c, "%<")) {
> +		if (skip_prefix_mem(c, len, ">8", &c, &len) ||
> +		    skip_prefix_mem(c, len, "8<", &c, &len) ||
> +		    skip_prefix_mem(c, len, ">%", &c, &len) ||
> +		    skip_prefix_mem(c, len, "%<", &c, &len)) {
>   			in_perforation = 1;
>   			perforation += 2;
>   			scissors += 2;
> -			c++;
> +			c--, len++;
>   			continue;
>   		}
>   		in_perforation = 0;
> @@ -747,7 +747,8 @@ static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
>   {
>   	if (mi->inbody_header_accum.len &&
>   	    (line->buf[0] == ' ' || line->buf[0] == '\t')) {
> -		if (mi->use_scissors && is_scissors_line(line->buf)) {
> +		if (mi->use_scissors &&
> +		    is_scissors_line(line->buf, line->len)) {
>   			/*
>   			 * This is a scissors line; do not consider this line
>   			 * as a header continuation line.
> @@ -808,7 +809,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
>   	if (convert_to_utf8(mi, line, mi->charset.buf))
>   		return 0; /* mi->input_error already set */
>   
> -	if (mi->use_scissors && is_scissors_line(line->buf)) {
> +	if (mi->use_scissors && is_scissors_line(line->buf, line->len)) {
>   		int i;
>   
>   		strbuf_setlen(&mi->log_message, 0);
> @@ -826,7 +827,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
>   		return 0;
>   	}
>   
> -	if (patchbreak(line)) {
> +	if (patchbreak(line->buf, line->len)) {
>   		if (mi->message_id)
>   			strbuf_addf(&mi->log_message,
>   				    "Message-Id: %s\n", mi->message_id);


  reply	other threads:[~2022-09-07 18:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-04 23:12 [PATCH] format-patch: warn if commit msg contains a patch delimiter Matheus Tavares
2022-09-05  8:01 ` Ævar Arnfjörð Bjarmason
2022-09-05 10:57   ` René Scharfe
2022-09-07 14:44   ` [PATCH v2 0/2] " Matheus Tavares
2022-09-07 14:44     ` [PATCH v2 1/2] patchbreak(), is_scissors_line(): work with a buf/len pair Matheus Tavares
2022-09-07 18:20       ` Phillip Wood [this message]
2022-09-08  0:35       ` Eric Sunshine
2022-09-07 14:44     ` [PATCH v2 2/2] format-patch: warn if commit msg contains a patch delimiter Matheus Tavares
2022-09-07 18:09       ` Phillip Wood
2022-09-07 18:36         ` Junio C Hamano
2022-09-09  1:08           ` Matheus Tavares
2022-09-09 16:47             ` Junio C Hamano
2022-09-07 17:44     ` [PATCH v2 0/2] " René Scharfe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=435b2b8b-2577-0af4-6f69-b250f64cbdf8@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    --cc=matheus.bernardino@usp.br \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).