summaryrefslogtreecommitdiff
path: root/git-compat-util.h
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2015-01-13 01:57:37 (GMT)
committerJunio C Hamano <gitster@pobox.com>2015-01-13 18:03:30 (GMT)
commitd64ea0f83bd7e676778f833c57f969a94518a28d (patch)
treea823ee7016046bdd3f6d2d9454b6a07260f28182 /git-compat-util.h
parent1da1e07c835e900337714cfad6c32a8dc0b36ac3 (diff)
downloadgit-d64ea0f83bd7e676778f833c57f969a94518a28d.zip
git-d64ea0f83bd7e676778f833c57f969a94518a28d.tar.gz
git-d64ea0f83bd7e676778f833c57f969a94518a28d.tar.bz2
git-compat-util: add xstrdup_or_null helper
It's a common idiom to duplicate a string if it is non-NULL, or pass a literal NULL through. This is already a one-liner in C, but you do have to repeat the name of the string twice. So if there's a function call, you must write: const char *x = some_fun(...); return x ? xstrdup(x) : NULL; instead of (with this patch) just: return xstrdup_or_null(some_fun(...)); Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'git-compat-util.h')
-rw-r--r--git-compat-util.h5
1 files changed, 5 insertions, 0 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index 44890d5..98cb78e 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -629,6 +629,11 @@ extern char *xgetcwd(void);
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
+static inline char *xstrdup_or_null(const char *str)
+{
+ return str ? xstrdup(str) : NULL;
+}
+
static inline size_t xsize_t(off_t len)
{
if (len > (size_t) len)