[PATCH 1/5] git-compat-util: add xstrdup_or_null helper

2015-01-12 Thread Jeff King
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;

Re: [PATCH 1/5] git-compat-util: add xstrdup_or_null helper

2015-01-12 Thread Jeff King
On Mon, Jan 12, 2015 at 06:21:19PM -0800, Jonathan Nieder wrote: Jeff King wrote: --- a/git-compat-util.h +++ b/git-compat-util.h @@ -675,6 +675,11 @@ extern char *xgetcwd(void); #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) +static inline char

Re: [PATCH 1/5] git-compat-util: add xstrdup_or_null helper

2015-01-12 Thread Jonathan Nieder
Jeff King wrote: --- a/git-compat-util.h +++ b/git-compat-util.h @@ -675,6 +675,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) :