git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 04/12] Replace some use of starts_with() with skip_prefix()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 18, 2013, 14:53 UTC
Message-ID
<1387378437-20646-5-git-send-email-pclouds@gmail.com>
In-Reply-To
<1387378437-20646-1-git-send-email-pclouds@gmail.com>
All the changes follow the pattern
  if (!starts_with(foo, "bar"))
    return;
  foo += 3;
which is turned into
  if ((foo = skip_prefix(foo, "bar")) == NULL)
    return;
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/branch.c | 3 +--
 pretty.c         | 3 +--
 setup.c          | 3 +--
 tag.c            | 7 +++----
 wt-status.c      | 3 +--
 5 files changed, 7 insertions(+), 12 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index b4d7716..d063de2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -868,9 +868,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (!strcmp(head, "HEAD")) {
 		detached = 1;
 	} else {
-		if (!starts_with(head, "refs/heads/"))
+		if ((head = skip_prefix(head, "refs/heads/")) == NULL)
 			die(_("HEAD not found below refs/heads!"));
-		head += 11;
 	}
 	hashcpy(merge_filter_ref, head_sha1);
 
diff --git a/pretty.c b/pretty.c
index 87db08b..08e30ec 100644
--- a/pretty.c
+++ b/pretty.c
@@ -40,10 +40,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
 	const char *fmt;
 	int i;
 
-	if (!starts_with(var, "pretty."))
+	if ((name = skip_prefix(var, "pretty.")) == NULL)
 		return 0;
 
-	name = var + strlen("pretty.");
 	for (i = 0; i < builtin_formats_len; i++) {
 		if (!strcmp(commit_formats[i].name, name))
 			return 0;
diff --git a/setup.c b/setup.c
index 6c3f85f..debfaab 100644
--- a/setup.c
+++ b/setup.c
@@ -304,14 +304,13 @@ const char *read_gitfile(const char *path)
 	if (len != st.st_size)
 		die("Error reading %s", path);
 	buf[len] = '\0';
-	if (!starts_with(buf, "gitdir: "))
+	if ((dir = (char *)skip_prefix(buf, "gitdir: ")) == NULL)
 		die("Invalid gitfile format: %s", path);
 	while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
 		len--;
 	if (len < 9)
 		die("No path in gitfile: %s", path);
 	buf[len] = '\0';
-	dir = buf + 8;
 
 	if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
 		size_t pathlen = slash+1 - path;
diff --git a/tag.c b/tag.c
index 7b07921..9b63d1b 100644
--- a/tag.c
+++ b/tag.c
@@ -86,9 +86,8 @@ int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
 		return -1;
 	bufptr += 48; /* "object " + sha1 + "\n" */
 
-	if (!starts_with(bufptr, "type "))
+	if ((bufptr = skip_prefix(bufptr, "type ")) == NULL)
 		return -1;
-	bufptr += 5;
 	nl = memchr(bufptr, '\n', tail - bufptr);
 	if (!nl || sizeof(type) <= (nl - bufptr))
 		return -1;
@@ -109,11 +108,11 @@ int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
 		item->tagged = NULL;
 	}
 
-	if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
+	if (bufptr + 4 < tail &&
+	    (bufptr = skip_prefix(bufptr, "tag ")) != NULL)
 		; 		/* good */
 	else
 		return -1;
-	bufptr += 4;
 	nl = memchr(bufptr, '\n', tail - bufptr);
 	if (!nl)
 		return -1;
diff --git a/wt-status.c b/wt-status.c
index 1f65039..185fa81 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1145,9 +1145,8 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
 	struct grab_1st_switch_cbdata *cb = cb_data;
 	const char *target = NULL, *end;
 
-	if (!starts_with(message, "checkout: moving from "))
+	if ((message = skip_prefix(message, "checkout: moving from ")) == NULL)
 		return 0;
-	message += strlen("checkout: moving from ");
 	target = strstr(message, " to ");
 	if (!target)
 		return 0;
-- 
1.8.5.1.208.g019362e
Previous: Junio C HamanoNext: Nguyễn Thái Ngọc Duy
Message 17 of 31 in “Hard coded string length cleanup”
  1. 00/12 Hard coded string length cleanupNguyễn Thái Ngọc Duy, Dec 18, 2013
  2. 01/12 Make starts_with() a wrapper of skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  3. Junio C HamanoDec 18, 2013
  4. Junio C HamanoDec 18, 2013
  5. 02/12 Convert starts_with() to skip_prefix() for option parsingNguyễn Thái Ngọc Duy, Dec 18, 2013
  6. Johannes SixtDec 20, 2013
  7. Jeff KingDec 20, 2013
  8. Christian CouderDec 20, 2013
  9. René ScharfeDec 20, 2013
  10. Junio C HamanoDec 20, 2013
  11. Duy NguyenDec 21, 2013
  12. Junio C HamanoDec 26, 2013
  13. Jeff KingDec 28, 2013
  14. 03/12 Add and use skip_prefix_defval()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  15. Kent R. SpillnerDec 18, 2013
  16. Junio C HamanoDec 18, 2013
  17. 04/12 Replace some use of starts_with() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  18. 05/12 Convert a lot of starts_with() to skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  19. 06/12 fetch.c: replace some use of starts_with() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  20. 07/12 connect.c: replace some use of starts_with() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  21. 08/12 refs.c: replace some use of starts_with() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  22. 09/12 diff.c: reduce code duplication in --stat-xxx parsingNguyễn Thái Ngọc Duy, Dec 18, 2013
  23. 10/12 environment.c: replace starts_with() in strip_namespace() with skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  24. 11/12 diff.c: convert diff_scoreopt_parse to use skip_prefix()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  25. 12/12 refs.c: use skip_prefix() in prune_ref()Nguyễn Thái Ngọc Duy, Dec 18, 2013
  26. Junio C HamanoDec 18, 2013
  27. René ScharfeDec 19, 2013
  28. Duy NguyenDec 19, 2013
  29. René ScharfeDec 20, 2013
  30. Duy NguyenDec 20, 2013
  31. Junio C HamanoDec 20, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.