debugger: Improve my_sprintf for short strings.

Don't need to repeat snprintf with memory allocation if the string is short.
This commit is contained in:
joevt 2023-08-07 11:44:41 -07:00 committed by dingusdev
parent 7fc92e236b
commit 968f503d80
1 changed files with 4 additions and 3 deletions

View File

@ -36,13 +36,14 @@ using namespace std;
template <typename... Args>
std::string my_sprintf(const char* format, Args... args) {
int length = std::snprintf(nullptr, 0, format, args...);
char buf_small[32];
int length = std::snprintf(buf_small, sizeof(buf_small), format, args...);
if (length <= 0)
return {}; /* empty string in C++11 */
if (length < sizeof(buf_small))
return buf_small;
char* buf = new char[length + 1];
std::snprintf(buf, length + 1, format, args...);
std::string str(buf);
delete[] buf;
return str;