FastLED 3.10.6
Loading...
Searching...
No Matches

◆ snprintf() [2/2]

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
  • o: octal integers
  • f: floating point numbers
  • s: strings (const char*, fl::string)
  • c: characters
  • x: hexadecimal (lowercase)
  • X: hexadecimal (uppercase)
  • p: pointers (formatted as 0x... hex)
  • %%: literal % character

Generic placeholder:

  • {}: formats the next argument by picking the natural specifier for its C++ type, so callers do not have to choose between d / u / ld / s / p or call .c_str(). Supported argument types are the same set the % specifiers support: all integral types (d), bool (as "true"/"false"), char (as a character, c), float/double (f), const char* and fl::string / fl::string_view (s), pointers (p), and unscoped enums (as their underlying integer). Scoped enums (enum class) do not compile through {} or d; cast at the call site. Passing any other type to {} is a compile error - {} deliberately does not fall back to a generic streaming path, to keep code size flat.
  • {{: literal { character
  • }}: literal } character
  • A lone { or } that is not part of the above passes through literally.
  • A {} with no remaining argument emits <missing_arg>, matching the behavior of a % specifier with no remaining argument.

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) FL_NO_EXCEPT

Definition at line 1009 of file stdio.h.

1009 {
1010 // Handle null buffer or zero size
1011 if (!buffer || size == 0) {
1012 return 0;
1013 }
1014
1015 // Format to internal string stream
1016 sstream stream;
1018 fl::string result = stream.str();
1019
1020 // Get the formatted string length
1021 fl::size formatted_len = result.size();
1022
1023 // Copy to buffer, ensuring null termination
1024 fl::size copy_len = (formatted_len < size - 1) ? formatted_len : size - 1;
1025
1026 // Copy characters
1027 for (fl::size i = 0; i < copy_len; ++i) {
1028 buffer[i] = result[i];
1029 }
1030
1031 // Null terminate
1032 buffer[copy_len] = '\0';
1033
1034 // Return the number of characters actually written (excluding null terminator)
1035 // This respects the buffer size limit instead of returning the full formatted length
1036 return static_cast<int>(copy_len);
1037}
fl::size size() const FL_NO_EXCEPT
void format_impl(sstream &stream, const char *format) FL_NO_EXCEPT
Definition stdio.h:855
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
fl::string format(const char *fmt)
Format with no arguments.
Definition format.h:439
corkscrew_args args
Definition old.h:149

References args, FL_NO_EXCEPT, format(), fl::printf_detail::format_impl(), and fl::sstream::str().

+ Here is the call graph for this function: