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

[PATCH 13/16] revert: simplify get_oneline() using mempcpy

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Mar 17, 2010, 12:17 UTC
Message-ID
<20100317121746.GM25641@progeny.tock>
In-Reply-To
<20100317113655.GA25470@progeny.tock>

Avoid some pointer arithmetic and make future modifications easier by using mempcpy to advance a pointer as the result is written.

While at it, make the code more self-explanatory by using strlen() on constant strings. GCC will compute the length at compile time; I am not sure about other compilers, but this is not performance-critical anyway. It should be simple to add a conststrlen() macro later if needed.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 builtin/revert.c |   13 ++++++-------
 1 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index 4b2042f..6a47655 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -75,7 +75,7 @@ static void parse_args(int argc, const char **argv)
 
 static char *get_oneline(const char *message, char **body)
 {
-	char *result;
+	char *result, *q;
 	const char *p = message, *abbrev, *eol;
 	int abbrev_len, oneline_len;
 
@@ -94,12 +94,11 @@ static char *get_oneline(const char *message, char **body)
 	abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
 	abbrev_len = strlen(abbrev);
 	oneline_len = eol - p;
-	result = xmalloc(abbrev_len + 5 + oneline_len);
-	memcpy(result, abbrev, abbrev_len);
-	memcpy(result + abbrev_len, "... ", 4);
-	memcpy(result + abbrev_len + 4, p, oneline_len);
-	result[abbrev_len + 4 + oneline_len] = '\0';
-	*body = result + abbrev_len + 4;
+	q = result = xmalloc(abbrev_len + strlen("... ") + oneline_len + 1);
+	q = mempcpy(q, abbrev, abbrev_len);
+	*body = q = mempcpy(q, "... ", strlen("... "));
+	q = mempcpy(q, p, oneline_len);
+	*q = '\0';
 	return result;
 }
 
-- 
1.7.0
Previous: Jonathan NiederNext: Jonathan Nieder
Message 20 of 23 in “Add label for common ancestor to conflictstyle=diff3”
  1. 0/16 Add label for common ancestor to conflictstyle=diff3Jonathan Nieder, Mar 17, 2010
  2. 01/16 xdl_merge(): add optional ancestor label to diff3-style outputJonathan Nieder, Mar 17, 2010
  3. Bert WesargMar 17, 2010
  4. Jonathan NiederMar 20, 2010
  5. 02/16 xdl_merge(): move file1 and file2 labels to xmparam structureJonathan Nieder, Mar 17, 2010
  6. 03/16 merge-file --diff3: add a label for ancestorJonathan Nieder, Mar 17, 2010
  7. 04/16 ll_merge(): add ancestor label parameter for diff3-style outputJonathan Nieder, Mar 17, 2010
  8. 05/16 checkout --conflict=diff3: add a label for ancestorJonathan Nieder, Mar 17, 2010
  9. 06/16 merge_file(): add comment explaining behavior wrt conflict styleJonathan Nieder, Mar 17, 2010
  10. 07/16 merge_trees(): add ancestor label parameter for diff3-style outputJonathan Nieder, Mar 17, 2010
  11. 08/16 tests: document format of conflicts from checkout -mJonathan Nieder, Mar 17, 2010
  12. 09/16 checkout -m --conflict=diff3: add a label for ancestorJonathan Nieder, Mar 17, 2010
  13. 10/16 tests: document cherry-pick behavior in face of conflictsJonathan Nieder, Mar 17, 2010
  14. 11/16 revert_or_cherry_pick(): get oneline_body from get_oneline()Jonathan Nieder, Mar 17, 2010
  15. Stephen BoydMar 17, 2010
  16. Jonathan NiederMar 18, 2010
  17. revert: clarify label on conflict hunksJonathan Nieder, Mar 18, 2010
  18. 15/16 cherry-pick, revert: add a label for ancestorJonathan Nieder, Mar 18, 2010
  19. 12/16 compat: add mempcpy()Jonathan Nieder, Mar 17, 2010
  20. 13/16 revert: simplify get_oneline() using mempcpyJonathan Nieder, Mar 17, 2010
  21. 14/16 revert: clarify label on conflict hunksJonathan Nieder, Mar 17, 2010
  22. 15/16 cherry-pick: add a label for ancestorJonathan Nieder, Mar 17, 2010
  23. 16/16 merge-recursive: add a label for ancestorJonathan Nieder, Mar 17, 2010

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.