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
  • 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

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) FL_NOEXCEPT
Snprintf-like formatting function that writes to a buffer.
Definition stdio.h:666

Definition at line 666 of file stdio.h.

666 {
667 // Handle null buffer or zero size
668 if (!buffer || size == 0) {
669 return 0;
670 }
671
672 // Format to internal string stream
673 sstream stream;
675 fl::string result = stream.str();
676
677 // Get the formatted string length
678 fl::size formatted_len = result.size();
679
680 // Copy to buffer, ensuring null termination
681 fl::size copy_len = (formatted_len < size - 1) ? formatted_len : size - 1;
682
683 // Copy characters
684 for (fl::size i = 0; i < copy_len; ++i) {
685 buffer[i] = result[i];
686 }
687
688 // Null terminate
689 buffer[copy_len] = '\0';
690
691 // Return the number of characters actually written (excluding null terminator)
692 // This respects the buffer size limit instead of returning the full formatted length
693 return static_cast<int>(copy_len);
694}
fl::size size() const FL_NOEXCEPT
void format_impl(sstream &stream, const char *format) FL_NOEXCEPT
Definition stdio.h:563
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_NOEXCEPT, format(), fl::printf_detail::format_impl(), and fl::sstream::str().

Referenced by fl::net::http::ChunkedWriter::chunkOverhead(), fl::audio::detector::Key::getKeyName(), getPort(), fl::net::http::HttpStreamTransport::idToString(), fl::third_party::TJpgInstanceDecoder::initializeDecoder(), fl::SerialPort::printf(), fl::third_party::TJpgInstanceDecoder::processChunk(), sprintf(), and fl::net::http::ChunkedWriter::writeChunk().

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