FastLED 3.9.15
Loading...
Searching...
No Matches

◆ snprintf()

template<typename... Args>
int fl::snprintf ( char * buffer,
fl::size size,
const char * format,
const Args &... args )

Snprintf-like formatting function that writes to a buffer.

Parameters
bufferOutput buffer to write formatted string to
sizeMaximum number of characters to write (including null terminator)
formatFormat string with placeholders like "%d", "%s", "%f" etc.
argsArguments to format
Returns
Number of characters that would have been written if buffer was large enough

Supported format specifiers:

  • d, i: integers (all integral types)
  • u: unsigned integers
  • f: floating point numbers
  • s: strings (const char*, fl::string)
  • c: characters
  • x: hexadecimal (lowercase)
  • X: hexadecimal (uppercase)
  • %%: literal % character

Example usage:

char buffer[100];
int len = fl::snprintf(buffer, sizeof(buffer), "Value: %d, Name: %s", 42, "test");
int snprintf(char *buffer, fl::size size, const char *format, const Args &... args)
Snprintf-like formatting function that writes to a buffer.
Definition printf.h:422

Definition at line 422 of file printf.h.

422 {
423 // Handle null buffer or zero size
424 if (!buffer || size == 0) {
425 return 0;
426 }
427
428 // Format to internal string stream
429 StrStream stream;
430 printf_detail::format_impl(stream, format, args...);
431 fl::string result = stream.str();
432
433 // Get the formatted string length
434 fl::size formatted_len = result.size();
435
436 // Copy to buffer, ensuring null termination
437 fl::size copy_len = (formatted_len < size - 1) ? formatted_len : size - 1;
438
439 // Copy characters
440 for (fl::size i = 0; i < copy_len; ++i) {
441 buffer[i] = result[i];
442 }
443
444 // Null terminate
445 buffer[copy_len] = '\0';
446
447 // Return the number of characters actually written (excluding null terminator)
448 // This respects the buffer size limit instead of returning the full formatted length
449 return static_cast<int>(copy_len);
450}
fl::size size() const
Definition str.h:324
void format_impl(StrStream &stream, const char *format)
Definition printf.h:326
corkscrew_args args
Definition old.h:150

References args, fl::printf_detail::format_impl(), and fl::StrStream::str().

Referenced by fl::string_functions::ftoa(), and sprintf().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: