Skip to contentSkip to conversation

use starts_with() instead of !memcmp()

PATCH In review Quint Guvernator 3 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 use starts_with() instead of !memcmp()

Another version, this time very in line with the review and commentary of Junio, Eric, and Michael. This version boasts a revamped commit message and fewer but surer hunks changed.

Thanks again for the guidance.

Quint Guvernator (1):
use starts_with() instead of !memcmp()

 builtin/apply.c        |  4 ++--
 builtin/for-each-ref.c |  2 +-
 builtin/mktag.c        |  2 +-
 builtin/patch-id.c     | 10 +++++-----
 connect.c              |  4 ++--
 imap-send.c            |  6 +++---
 remote.c               |  2 +-
 7 files changed, 15 insertions(+), 15 deletions(-)
Signature · 1 line
1.9.0
#1 lore raw

PATCH v5 1/1 use starts_with() instead of !memcmp()

When checking if a string begins with a constant string, using starts_with() indicates the intention of the check more clearly and is less error-prone than calling !memcmp() with an explicit byte count.

---
 builtin/apply.c        |  4 ++--
 builtin/for-each-ref.c |  2 +-
 builtin/mktag.c        |  2 +-
 builtin/patch-id.c     | 10 +++++-----
 connect.c              |  4 ++--
 imap-send.c            |  6 +++---
 remote.c               |  2 +-
 7 files changed, 15 insertions(+), 15 deletions(-)
builtin/apply.c +2 -2
index 0189523..826b3e2 100644
@@ -1631,7 +1631,7 @@ static int parse_fragment(const char *line, unsigned long size,
 		 * l10n of "\ No newline..." is at least that long.
 		 */
 		case '\\':
-			if (len < 12 || memcmp(line, "\\ ", 2))
+			if (len < 12 || !starts_with(line, "\\ "))
 				return -1;
 			break;
 		}
@@ -1646,7 +1646,7 @@ static int parse_fragment(const char *line, unsigned long size,
 	 * it in the above loop because we hit oldlines == newlines == 0
 	 * before seeing it.
 	 */
-	if (12 < size && !memcmp(line, "\\ ", 2))
+	if (12 < size && starts_with(line, "\\ "))
 		offset += linelen(line, size);
 
 	patch->lines_added += added;
builtin/for-each-ref.c +1 -1
index 3e1d5c3..4135980 100644
@@ -193,7 +193,7 @@ static int verify_format(const char *format)
 		at = parse_atom(sp + 2, ep);
 		cp = ep + 1;
 
-		if (!memcmp(used_atom[at], "color:", 6))
+		if (starts_with(used_atom[at], "color:"))
 			need_color_reset_at_eol = !!strcmp(used_atom[at], color_reset);
 	}
 	return 0;
builtin/mktag.c +1 -1
index 640ab64..d2d9310 100644
@@ -57,7 +57,7 @@ static int verify_tag(char *buffer, unsigned long size)
 
 	/* Verify type line */
 	type_line = object + 48;
-	if (memcmp(type_line - 1, "\ntype ", 6))
+	if (!starts_with(type_line - 1, "\ntype "))
 		return error("char%d: could not find \"\\ntype \"", 47);
 
 	/* Verify tag-line */
builtin/patch-id.c +5 -5
index 3cfe02d..23ef424 100644
@@ -81,14 +81,14 @@ static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx, struct st
 		}
 
 		/* Ignore commit comments */
-		if (!patchlen && memcmp(line, "diff ", 5))
+		if (!patchlen && !starts_with(line, "diff "))
 			continue;
 
 		/* Parsing diff header?  */
 		if (before == -1) {
-			if (!memcmp(line, "index ", 6))
+			if (starts_with(line, "index "))
 				continue;
-			else if (!memcmp(line, "--- ", 4))
+			else if (starts_with(line, "--- "))
 				before = after = 1;
 			else if (!isalpha(line[0]))
 				break;
@@ -96,14 +96,14 @@ static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx, struct st
 
 		/* Looking for a valid hunk header?  */
 		if (before == 0 && after == 0) {
-			if (!memcmp(line, "@@ -", 4)) {
+			if (starts_with(line, "@@ -")) {
 				/* Parse next hunk, but ignore line numbers.  */
 				scan_hunk_header(line, &before, &after);
 				continue;
 			}
 
 			/* Split at the end of the patch.  */
-			if (memcmp(line, "diff ", 5))
+			if (!starts_with(line, "diff "))
 				break;
 
 			/* Else we're parsing another header.  */
connect.c +2 -2
index 4150412..5ae2aaa 100644
@@ -30,11 +30,11 @@ static int check_ref(const char *name, int len, unsigned int flags)
 		return 0;
 
 	/* REF_HEADS means that we want regular branch heads */
-	if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
+	if ((flags & REF_HEADS) && starts_with(name, "heads/"))
 		return 1;
 
 	/* REF_TAGS means that we want tags */
-	if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
+	if ((flags & REF_TAGS) && starts_with(name, "tags/"))
 		return 1;
 
 	/* All type bits clear means that we are ok with anything */
imap-send.c +3 -3
index 0bc6f7f..019de18 100644
@@ -524,7 +524,7 @@ static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
 	if (Verbose) {
 		if (imap->num_in_progress)
 			printf("(%d in progress) ", imap->num_in_progress);
-		if (memcmp(cmd->cmd, "LOGIN", 5))
+		if (!starts_with(cmd->cmd, "LOGIN"))
 			printf(">>> %s", buf);
 		else
 			printf(">>> %d LOGIN <user> <pass>\n", cmd->tag);
@@ -802,7 +802,7 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
 				resp = DRV_OK;
 			else {
 				if (!strcmp("NO", arg)) {
-					if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp(cmd, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */
+					if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || starts_with(cmd, "[TRYCREATE]"))) { /* SELECT, APPEND or UID COPY */
 						p = strchr(cmdp->cmd, '"');
 						if (!issue_imap_cmd(ctx, NULL, "CREATE \"%.*s\"", (int)(strchr(p + 1, '"') - p + 1), p)) {
 							resp = RESP_BAD;
@@ -827,7 +827,7 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
 				} else /*if (!strcmp("BAD", arg))*/
 					resp = RESP_BAD;
 				fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n",
-					 memcmp(cmdp->cmd, "LOGIN", 5) ?
+					 !starts_with(cmdp->cmd, "LOGIN") ?
 							cmdp->cmd : "LOGIN <user> <pass>",
 							arg, cmd ? cmd : "");
 			}
remote.c +1 -1
index 5f63d55..bd02b0e 100644
@@ -1149,7 +1149,7 @@ static int match_explicit(struct ref *src, struct ref *dst,
 	case 1:
 		break;
 	case 0:
-		if (!memcmp(dst_value, "refs/", 5))
+		if (starts_with(dst_value, "refs/"))
 			matched_dst = make_linked_ref(dst_value, dst_tail);
 		else if (is_null_sha1(matched_src->new_sha1))
 			error("unable to delete '%s': remote ref does not exist",
Signature · 1 line
1.9.0
#2 lore raw
In reply to Quint Guvernator
Quoted 3 lines · Quint Guvernator

Another version, this time very in line with the review and commentary of Junio, Eric, and Michael. This version boasts a revamped commit message and fewer but surer hunks changed.

Explaining what changed in this version is indeed a courtesy to reviewers. Thanks.

For bonus points, provide a link to the previous attempt, like this [1].

[1]: http://thread.gmane.org/gmane.comp.version-control.git/244292

Quoted 16 lines

Thanks again for the guidance.

Quint Guvernator (1):
use starts_with() instead of !memcmp()

 builtin/apply.c        |  4 ++--
 builtin/for-each-ref.c |  2 +-
 builtin/mktag.c        |  2 +-
 builtin/patch-id.c     | 10 +++++-----
 connect.c              |  4 ++--
 imap-send.c            |  6 +++---
 remote.c               |  2 +-
 7 files changed, 15 insertions(+), 15 deletions(-)

--
1.9.0

#3 lore raw
Quoted 8 lines · Eric Sunshine
Quoted 3 lines · Quint Guvernator

Another version, this time very in line with the review and commentary of Junio, Eric, and Michael. This version boasts a revamped commit message and fewer but surer hunks changed.

Explaining what changed in this version is indeed a courtesy to reviewers. Thanks.

So, is that a "reviewed-by: Eric"?

#4 lore raw
Quoted 4 lines · Junio C Hamano
Quoted 2 lines · Eric Sunshine

On Tue, Mar 18, 2014 at 9:18 PM, Quint Guvernator
<[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Another version, this time very in line with the review and commentary of Junio, Eric, and Michael. This version boasts a revamped commit message and fewer but surer hunks changed.

Explaining what changed in this version is indeed a courtesy to reviewers. Thanks.

So, is that a "reviewed-by: Eric"?

No, sorry. You and Peff were actively reviewing Quint's submissions, so I merely scanned them quickly without making a careful examination. I've been commenting upon so many GSoC submissions that it's hard to remember which is which, and upon reading "Another version, this time very in line with the review and commentary Junio, Eric, and Michael", I had to search the list archive to figure out why my name was listed. Hence, the suggestion from me that providing a link to the previous attempt is a welcome courtesy.