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
- 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
1011 if (!buffer || size == 0) {
1012 return 0;
1013 }
1014
1015
1016 sstream stream;
1019
1020
1022
1023
1024 fl::size copy_len = (formatted_len < size - 1) ? formatted_len : size - 1;
1025
1026
1027 for (fl::size i = 0; i < copy_len; ++i) {
1029 }
1030
1031
1032 buffer[copy_len] = '\0';
1033
1034
1035
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
expected< T, E > result
Alias for expected (Rust-style naming)
fl::string format(const char *fmt)
Format with no arguments.
References args, FL_NO_EXCEPT, format(), fl::printf_detail::format_impl(), and fl::sstream::str().