| 7 | namespace devilution { |
| 8 | |
| 9 | std::string FormatInteger(int n) |
| 10 | { |
| 11 | constexpr size_t GroupSize = 3; |
| 12 | |
| 13 | char buf[40]; |
| 14 | char *begin = buf; |
| 15 | const char *end = BufCopy(buf, n); |
| 16 | const size_t len = end - begin; |
| 17 | |
| 18 | std::string out; |
| 19 | const size_t prefixLen = n < 0 ? 1 : 0; |
| 20 | const size_t numLen = len - prefixLen; |
| 21 | if (numLen <= GroupSize) { |
| 22 | out.append(begin, len); |
| 23 | return out; |
| 24 | } |
| 25 | |
| 26 | const string_view separator = _(/* TRANSLATORS: Thousands separator */ ","); |
| 27 | out.reserve(len + separator.size() * (numLen - 1) / GroupSize); |
| 28 | if (n < 0) { |
| 29 | out += '-'; |
| 30 | ++begin; |
| 31 | } |
| 32 | |
| 33 | size_t mlen = numLen % GroupSize; |
| 34 | if (mlen == 0) |
| 35 | mlen = GroupSize; |
| 36 | out.append(begin, mlen); |
| 37 | begin += mlen; |
| 38 | for (; begin != end; begin += GroupSize) { |
| 39 | AppendStrView(out, separator); |
| 40 | out.append(begin, GroupSize); |
| 41 | } |
| 42 | |
| 43 | return out; |
| 44 | } |
| 45 | |
| 46 | } // namespace devilution |