Browse Source

FormatInteger (unsigned)

pull/7914/head
Eric Robinson 11 months ago committed by Gleb Mazovetskiy
parent
commit
e13e1d94d1
  1. 32
      Source/utils/format_int.cpp
  2. 1
      Source/utils/format_int.hpp

32
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

1
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

Loading…
Cancel
Save