Change common to_string implementations to use snprintf instead of itoa

This commit is contained in:
Myles Busig 2024-03-27 00:59:39 -06:00
parent 002bd5a17a
commit 725fadf22a

View File

@ -172,8 +172,15 @@ public:
num_char = mtl_itostr(x, str.data()); num_char = mtl_itostr(x, str.data());
} }
#else #else
itoa(x, str.data(), hex ? 16 : 10); if (hex) {
num_char = strlen(str.data()); 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 #endif
str.m_size = num_char; str.m_size = num_char;
return str; return str;
@ -198,8 +205,11 @@ public:
num_char = mtl_utostr(x, str.data()); num_char = mtl_utostr(x, str.data());
} }
#else #else
utoa(x, str.data(), hex ? 16 : 10); if (hex) {
num_char = strlen(str.data()); num_char = snprintf(str.data(), str.capacity() + 1, "%#x", x);
} else {
num_char = snprintf(str.data(), str.capacity() + 1, "%u", x);
}
#endif #endif
str.m_size = num_char; str.m_size = num_char;
return str; return str;
@ -243,8 +253,7 @@ public:
#ifdef __ARM_ARCH_4T__ #ifdef __ARM_ARCH_4T__
num_char = mtl_utostrx(reinterpret_cast<uint32_t>(x), str.data()); num_char = mtl_utostrx(reinterpret_cast<uint32_t>(x), str.data());
#else #else
utoa(x, str.data(), 16); num_char = snprintf(str.data(), str.capacity() + 1, "%#x", x);
num_char = strlen(str.data());
#endif #endif
str.m_size = num_char; str.m_size = num_char;
return str; return str;