[asterisk-dev] RFC - add this S_OR macro to strings.h ?
BJ Weschke
bweschke at gmail.com
Thu Mar 23 11:14:37 MST 2006
On 3/23/06, Luigi Rizzo <rizzo at icir.org> wrote:
> In my branch, i have a small patch in strings.h which defines
> a function S_OR(a, b) similar in spirit to the || operator, but working
> on char * instead of booleans. It is used to replace
> constructs that take a string or a default value, as below:
>
> - chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
> + chan = S_OR(cdr->channel, "<unknown>");
>
> or even longer statements involving if/then/else.
> So far I have used it in 25 places in my branch, which means that this is
> a very common construct. Readability of the code improves a lot,
> because lines become shorter, and the chance of mistyping the second
> argument become smaller.
>
> If there are no objections i would like to commit the
> patch below to trunk and start using it.
>
> The implementation can be either a macro, or better a
> static inline function so we are free from side effects
> in evaluating the first argument.
>
> cheers
> luigi
>
> --- include/asterisk/strings.h (revision 14413)
> +++ include/asterisk/strings.h (working copy)
> @@ -35,6 +35,11 @@
> return (!s || (*s == '\0'));
> }
>
> +/*! \brief returns the equivalent of logic or for strings:
> + * first one if not empty, otherwise second one.
> + */
> +#define S_OR(a, b) (!ast_strlen_zero(a) ? (a) : (b))
> +
> /*!
> \brief Gets a pointer to the first non-whitespace character in a string.
> \param ast_skip_blanks function being used
+1 - I think that's a cool idea.
--
Bird's The Word Technologies, Inc.
http://www.btwtech.com/
More information about the asterisk-dev
mailing list