From e13e1d94d1c3dc186575b438d0bf969999bb4479 Mon Sep 17 00:00:00 2001 From: Eric Robinson Date: Sun, 6 Apr 2025 23:36:27 -0400 Subject: [PATCH] FormatInteger (unsigned) --- Source/utils/format_int.cpp | 32 ++++++++++++++++++++++++++++++++ Source/utils/format_int.hpp | 1 + 2 files changed, 33 insertions(+) diff --git a/Source/utils/format_int.cpp b/Source/utils/format_int.cpp index bb5f4052b..7f09bf493 100644 --- a/Source/utils/format_int.cpp +++ b/Source/utils/format_int.cpp @@ -44,4 +44,36 @@ std::string FormatInteger(int n) return out; } +std::string FormatInteger(uint32_t n) +{ + constexpr size_t GroupSize = 3; + + char buf[40]; + char *begin = buf; + const char *end = BufCopy(buf, n); + const size_t len = end - begin; + + std::string out; + const size_t numLen = len; + if (numLen <= GroupSize) { + out.append(begin, len); + return out; + } + + const std::string_view separator = _(/* TRANSLATORS: Thousands separator */ ","); + out.reserve(len + separator.size() * (numLen - 1) / GroupSize); + + size_t mlen = numLen % GroupSize; + if (mlen == 0) + mlen = GroupSize; + out.append(begin, mlen); + begin += mlen; + for (; begin != end; begin += GroupSize) { + out.append(separator); + out.append(begin, GroupSize); + } + + return out; +} + } // namespace devilution diff --git a/Source/utils/format_int.hpp b/Source/utils/format_int.hpp index 733eb5512..cff0b9348 100644 --- a/Source/utils/format_int.hpp +++ b/Source/utils/format_int.hpp @@ -8,5 +8,6 @@ namespace devilution { * @brief Formats integer with thousands separator. */ std::string FormatInteger(int n); +std::string FormatInteger(uint32_t n); } // namespace devilution