From 725fadf22ab334f3e88982f4d07f76470d01524d Mon Sep 17 00:00:00 2001 From: Myles Busig Date: Wed, 27 Mar 2024 00:59:39 -0600 Subject: [PATCH] Change common to_string implementations to use snprintf instead of itoa --- include/mtl/string.hpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/include/mtl/string.hpp b/include/mtl/string.hpp index f627048..3284cb0 100644 --- a/include/mtl/string.hpp +++ b/include/mtl/string.hpp @@ -172,8 +172,15 @@ public: num_char = mtl_itostr(x, str.data()); } #else - itoa(x, str.data(), hex ? 16 : 10); - num_char = strlen(str.data()); + if (hex) { + if (x > 0) { + num_char = snprintf(str.data(), str.capacity() + 1, "%#x", x); + } else { + num_char = snprintf(str.data(), str.capacity() + 1, "-%#x", -x); + } + } else { + num_char = snprintf(str.data(), str.capacity() + 1, "%i", x); + } #endif str.m_size = num_char; return str; @@ -198,8 +205,11 @@ public: num_char = mtl_utostr(x, str.data()); } #else - utoa(x, str.data(), hex ? 16 : 10); - num_char = strlen(str.data()); + if (hex) { + num_char = snprintf(str.data(), str.capacity() + 1, "%#x", x); + } else { + num_char = snprintf(str.data(), str.capacity() + 1, "%u", x); + } #endif str.m_size = num_char; return str; @@ -243,8 +253,7 @@ public: #ifdef __ARM_ARCH_4T__ num_char = mtl_utostrx(reinterpret_cast(x), str.data()); #else - utoa(x, str.data(), 16); - num_char = strlen(str.data()); + num_char = snprintf(str.data(), str.capacity() + 1, "%#x", x); #endif str.m_size = num_char; return str;