From: Jeff King Date: Tue, 13 Jan 2015 01:57:37 +0000 (-0500) Subject: git-compat-util: add xstrdup_or_null helper X-Git-Tag: v2.3.1~13^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d64ea0f83bd7e676778f833c57f969a94518a28d;p=thirdparty%2Fgit.git 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 Signed-off-by: Junio C Hamano --- diff --git a/git-compat-util.h b/git-compat-util.h index 44890d5b18..98cb78edf7 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)