Snprintf-like formatting function that writes to a buffer.
- Parameters
-
buffer | Output buffer to write formatted string to |
size | Maximum number of characters to write (including null terminator) |
format | Format string with placeholders like "%d", "%s", "%f" etc. |
args | Arguments 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 at line 422 of file printf.h.
422 {
423
424 if (!buffer || size == 0) {
425 return 0;
426 }
427
428
429 StrStream stream;
432
433
434 fl::size formatted_len = result.
size();
435
436
437 fl::size copy_len = (formatted_len < size - 1) ? formatted_len : size - 1;
438
439
440 for (fl::size i = 0; i < copy_len; ++i) {
441 buffer[i] = result[i];
442 }
443
444
445 buffer[copy_len] = '\0';
446
447
448
449 return static_cast<int>(copy_len);
450}
void format_impl(StrStream &stream, const char *format)
References args, fl::printf_detail::format_impl(), and fl::StrStream::str().
Referenced by fl::string_functions::ftoa(), and sprintf().