summaryrefslogtreecommitdiff
path: root/src/utils/stringutils.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-03-12 15:38:30 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-03-25 14:34:31 +0000
commitf00022aa66f7b7c6d0825b8a45944e35d8cb0cf6 (patch)
tree26b509507cfa793d422d1ae3d8ac6a3f37ba3172 /src/utils/stringutils.cpp
parenta06db62288bc3fdbf3f25bcfa046c20510036665 (diff)
downloadmana-f00022aa66f7b7c6d0825b8a45944e35d8cb0cf6.tar.gz
mana-f00022aa66f7b7c6d0825b8a45944e35d8cb0cf6.tar.bz2
mana-f00022aa66f7b7c6d0825b8a45944e35d8cb0cf6.tar.xz
mana-f00022aa66f7b7c6d0825b8a45944e35d8cb0cf6.zip
Removed an unnecessary extra allocation in strprintf
We can use vsnprintf to write to the std::string directly.
Diffstat (limited to 'src/utils/stringutils.cpp')
-rw-r--r--src/utils/stringutils.cpp9
1 files changed, 4 insertions, 5 deletions
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index a1637892..7a951ec6 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -94,13 +94,12 @@ std::string strprintf(char const *format, ...)
return buf;
}
// The static size was not big enough, try again with a dynamic allocation.
- ++nb;
- char *buf2 = new char[nb];
+ ++nb; // Add 1 for the null terminator.
+
+ std::string res(nb, char());
va_start(args, format);
- vsnprintf(buf2, nb, format, args);
+ vsnprintf(res.data(), nb, format, args);
va_end(args);
- std::string res(buf2);
- delete [] buf2;
return res;
}