fixeria has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/35313?usp=email )
Change subject: utils: improve readability of OSMO_STRBUF_CHAR_COUNT ......................................................................
utils: improve readability of OSMO_STRBUF_CHAR_COUNT
Similarly to OSMO_STRBUF_REMAIN, let's improve the code readability by adding a static inline function. We should generally prefer using static inline functions over macros, unless there is something that only the proprocessor can do.
Change-Id: I71f24b87c13fd83952029171a6993f8da5e32e5b --- M include/osmocom/core/utils.h 1 file changed, 28 insertions(+), 4 deletions(-)
Approvals: laforge: Looks good to me, but someone else must approve pespin: Looks good to me, but someone else must approve neels: Looks good to me, approved Jenkins Builder: Verified
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 973a9d0..9a6e6b2 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -297,11 +297,21 @@ #define OSMO_STRBUF_REMAIN(STRBUF) \ _osmo_strbuf_remain(&(STRBUF))
+/*! Get number of actual characters (without terminating nul) in the given struct osmo_strbuf. + * \param[in] sb the string buffer to get the number of characters for. + * \returns number of actual characters (without terminating nul). */ +static inline size_t _osmo_strbuf_char_count(const struct osmo_strbuf *sb) +{ + if (OSMO_UNLIKELY(sb == NULL || sb->buf == NULL)) + return 0; + if (sb->pos == NULL || sb->pos <= sb->buf) + return 0; + return OSMO_MIN(sb->pos - sb->buf, sb->len - 1); +} + /*! Return number of actual characters contained in struct osmo_strbuf (without terminating nul). */ -#define OSMO_STRBUF_CHAR_COUNT(STRBUF) ((STRBUF).buf && ((STRBUF).pos > (STRBUF).buf) ? \ - OSMO_MIN((STRBUF).pos - (STRBUF).buf, \ - (STRBUF).len - 1) \ - : 0) +#define OSMO_STRBUF_CHAR_COUNT(STRBUF) \ + _osmo_strbuf_char_count(&(STRBUF))
/*! Like OSMO_STRBUF_APPEND(), but for function signatures that return the char* buffer instead of a length. * When using this function, the final STRBUF.chars_needed may not reflect the actual number of characters needed, since