| 664 | /// @endcode |
| 665 | template<typename... Args> |
| 666 | int snprintf(char* buffer, fl::size size, const char* format, const Args&... args) FL_NOEXCEPT { |
| 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; |
| 674 | printf_detail::format_impl(stream, format, args...); |
| 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 | } |
| 695 | |
| 696 | /// @brief Sprintf-like formatting function that writes to a buffer |
| 697 | /// @param buffer Output buffer to write formatted string to |