mirror of
https://github.com/dingusdev/dingusppc.git
synced 2025-01-22 14:30:31 +00:00
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:
parent
7fc92e236b
commit
968f503d80
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user