commit.c: use skip_prefix() instead of starts_with()
PATCH v3 1/1 commit.c: use skip_prefix() instead of starts_with()
In record_author_date() & parse_gpg_output() ,using skip_prefix() instead of starts_with() is a more suitable abstraction.
---
Patch V3 Variable naming improved, removed assignments inside conditionals.
Thanks to Junio C Hamano and Max Horn.
Patch V2 Corrected email formatting ,reapplied the implementation according to suggestions.
Thanks to Michael Haggerty.
This is in respect to GSoC microproject #10.
In record_author_date(), extra and useless calls to strlen due to using starts_with() were removed by using skip_prefix(). Extra variable "skip" was used as "buf" is used in for loop update check.
Other usages of starts_with() in the same file can be found with,
$ grep -n starts_with commit.c
1116: else if (starts_with(line, gpg_sig_header) &&
1196: if (starts_with(buf, sigcheck_gpg_status[i].check + 1)) {
The starts_with() in line 1116 was left as it is, as strlen values were pre generated as global variables.
The starts_with() in line 1196 was replaced as it abstracts way the skip_prefix part by directly using the function.
Also skip_prefix() is inline when compared to starts_with().
commit.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
commit.c +9 -8
@@ -548,7 +548,7 @@ define_commit_slab(author_date_slab, unsigned long);static void record_author_date (struct author_date_slab *author_date,struct commit *commit) { -const char *buf, *line_end; +const char *buf, *line_end, *ident_line;char *buffer =NULL ;struct ident_split ident;char *date_end;
@@ -566,14 +566,16 @@ static void record_author_date(struct author_date_slab *author_date,buf ; buf = line_end + 1) { line_end =strchrnul (buf,'\n' ); -if (!starts_with (buf,"author " )) { + ident_line =skip_prefix (buf,"author " ); +if (!ident_line) {if (!line_end[0 ] || line_end[1 ] =='\n' )return ;/* end of header */ continue ; } + buf = ident_line;if (split_ident_line (&ident, - buf +strlen ("author " ), - line_end - (buf +strlen ("author " ))) || + buf, + line_end - buf) || !ident.date_begin || !ident.date_end) goto fail_exit;/* malformed "author" line */ break ;
@@ -1193,10 +1195,9 @@ static void parse_gpg_output(struct signature_check *sigc)for (i =0 ; i <ARRAY_SIZE (sigcheck_gpg_status); i++) {const char *found, *next; -if (starts_with (buf, sigcheck_gpg_status[i].check +1 )) { -/* At the very beginning of the buffer */ - found = buf +strlen (sigcheck_gpg_status[i].check +1 ); - }else { + found =skip_prefix (buf, sigcheck_gpg_status[i].check +1 ); +/* At the very beginning of the buffer */ +if (!found) { found =strstr (buf, sigcheck_gpg_status[i].check);if (!found)continue ;
Signature · 1 line
1.9.0
On 04.03.2014, at 09:42, Tanay Abhra <[email protected]> wrote:
[...]
Quoted 7 lines
commit.c| 17 +++++++++--------1 file changed, 9 insertions(+), 8 deletions(-)commit.c +0
-0
[...]
Quoted 10 lines
commit.c +2
-1@@ -566,14 +566,16 @@ static void record_author_date(struct author_date_slab *author_date,buf ; buf = line_end + 1) { line_end =strchrnul (buf,'\n' ); -if (!starts_with (buf,"author " )) { + ident_line =skip_prefix (buf,"author " ); +if (!ident_line) {if (!line_end[0 ] || line_end[1 ] =='\n' )return ;/* end of header */ continue ;
commit.c static void record_author_date(struct author_date_slab *author_date, · line 581
Quoted 10 lines
commit.c +3
-2} + buf = ident_line;if (split_ident_line (&ident, - buf +strlen ("author " ), - line_end - (buf +strlen ("author " ))) || + buf, + line_end - buf) || !ident.date_begin || !ident.date_end) goto fail_exit;/* malformed "author" line */ break ;
Why not get rid of that assignment to "buf", and use ident_line instead of buf below? That seems like it would be more readable, wouldn't it?
commit.c static void parse_gpg_output(struct signature_check *sigc) · line 1199
Quoted 10 lines
commit.c +2
-4@@ -1193,10 +1195,9 @@ static void parse_gpg_output(struct signature_check *sigc)for (i =0 ; i <ARRAY_SIZE (sigcheck_gpg_status); i++) {const char *found, *next; -if (starts_with (buf, sigcheck_gpg_status[i].check +1 )) { -/* At the very beginning of the buffer */ - found = buf +strlen (sigcheck_gpg_status[i].check +1 ); - }else { + found =skip_prefix (buf, sigcheck_gpg_status[i].check +1 ); +/* At the very beginning of the buffer */
Do we really need that comment, and in that spot? The code seemed clear enough to me without it. But if you think keeping is better, perhaps move it to *before* the skip_prefix, and add a trailing "?"
Quoted 4 lines
commit.c +1
-0+if (!found) { found =strstr (buf, sigcheck_gpg_status[i].check);if (!found)continue ;
Quoted 5 lines · Max Horn
Quoted 5 lines
commit.c +1
-0+ buf = ident_line;if (split_ident_line(&ident,
commit.c +1
-2- buf +strlen ("author " ), - line_end - (buf + strlen("author "))) || + buf,
Quoted 8 lines
Quoted 4 lines
commit.c +1
-0+ line_end - buf) ||!ident.date_begin || !ident.date_end) goto fail_exit; /* malformed "author" line */ break;Why not get rid of that assignment to "buf", and use
ident_lineinstead of buf below? That seems like it would be more readable, wouldn't it?
Yes, and also now the argument list is much shorter, you could probably do it on two lines instead of three:
if (split_ident_line(&ident,
ident_line, line_end - ident_line) ||
...Quoted 4 lines
Quoted 4 lines
commit.c +0
-0@@ -1193,10 +1195,9 @@ static void parse_gpg_output(struct signature_check *sigc)for (i =0 ; i <ARRAY_SIZE (sigcheck_gpg_status); i++) {const char *found, *next;
Quoted 11 lines
commit.c static void parse_gpg_output(struct signature_check *sigc) · line 1199
Quoted 6 lines
commit.c +2
-4-if (starts_with (buf, sigcheck_gpg_status[i].check +1 )) { -/* At the very beginning of the buffer */ - found = buf +strlen (sigcheck_gpg_status[i].check +1 ); - }else { + found =skip_prefix (buf, sigcheck_gpg_status[i].check +1 ); +/* At the very beginning of the buffer */ Do we really need that comment, and in that spot? The code seemed clear enough to me without it. But if you think keeping is better, perhaps move it to *before* the
skip_prefix, and add a trailing "?"
Both good suggestions (I tend to prefer the removal).
Thanks.
Quoted 2 lines · Tanay Abhra
In
record_author_date()&parse_gpg_output(),usingskip_prefix()instead ofstarts_with()is a more suitable abstraction.
Thanks. Will queue with a reworded message to clarify what exactly "A more suitable" means.
Here is what I tentatively came up with.
-- >8 --
From: Tanay Abhra <[email protected]>
Date: Tue, 4 Mar 2014 00:42:20 -0800
Subject: [PATCH] commit.c: use skip_prefix() instead of starts_with()
In record_author_date() & parse_gpg_output(), the callers of starts_with() not just want to know if the string starts with the prefix, but also can benefit from knowing the string that follows the prefix.
By using skip_prefix(), we can do both at the same time.
--- commit.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
commit.c +9 -8
@@ -548,7 +548,7 @@ define_commit_slab(author_date_slab, unsigned long);static void record_author_date (struct author_date_slab *author_date,struct commit *commit) { -const char *buf, *line_end; +const char *buf, *line_end, *ident_line;char *buffer =NULL ;struct ident_split ident;char *date_end;
@@ -566,14 +566,16 @@ static void record_author_date(struct author_date_slab *author_date,buf ; buf = line_end + 1) { line_end =strchrnul (buf,'\n' ); -if (!starts_with (buf,"author " )) { + ident_line =skip_prefix (buf,"author " ); +if (!ident_line) {if (!line_end[0 ] || line_end[1 ] =='\n' )return ;/* end of header */ continue ; } + buf = ident_line;if (split_ident_line (&ident, - buf +strlen ("author " ), - line_end - (buf +strlen ("author " ))) || + buf, + line_end - buf) || !ident.date_begin || !ident.date_end) goto fail_exit;/* malformed "author" line */ break ;
@@ -1193,10 +1195,9 @@ static void parse_gpg_output(struct signature_check *sigc)for (i =0 ; i <ARRAY_SIZE (sigcheck_gpg_status); i++) {const char *found, *next; -if (starts_with (buf, sigcheck_gpg_status[i].check +1 )) { -/* At the very beginning of the buffer */ - found = buf +strlen (sigcheck_gpg_status[i].check +1 ); - }else { + found =skip_prefix (buf, sigcheck_gpg_status[i].check +1 ); +/* At the very beginning of the buffer */ +if (!found) { found =strstr (buf, sigcheck_gpg_status[i].check);if (!found)continue ;
Signature · 1 line
1.9.0-186-gd464cb7