carb::extras::formatString

Defined in carb/extras/StringSafe.h

inline size_t carb::extras::formatString(char *dstBuf, size_t dstBufSize, const char *fmtString, ...)

A snprintf wrapper that clamps the return value.

Remark

This function is intended to be used in code where an index is incremented by snprintf. In the following example, if snprintf() were used, idx can become larger than len, causing wraparound errors, but with formatString(), idx will never become larger than len.

          idx += formatString(buf, len - idx, ...);
          idx += formatString(buf, len - idx, ...);

Parameters
  • dstBuf[out] pointer to a destination buffer (can be nullptr in the case if dstBufSize is zero).

  • dstBufSize[in] size in characters of the destination buffer.

  • fmtString[in] pointer to a format string (passed to the snprintf call).

Returns

a number of characters written to the destination buffer (not including the trailing \0).