Thread (23 messages) flat view 23 messages, 3 authors, 2016-06-15
STALE3749d

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

From: Jonathan Nieder <hidden>
Date: 2016-06-15 22:48:26
Subsystem: the rest · Maintainer: Linus Torvalds

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 <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help