From 7164019e2248f74a6b7c2eb2404c3b4c9463845f Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 26 Feb 2017 03:20:50 +0100 Subject: [PATCH] locale digit grouping support for integer format --- ostd/format.hh | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ostd/format.hh b/ostd/format.hh index 2f73380..9b2e164 100644 --- a/ostd/format.hh +++ b/ostd/format.hh @@ -554,7 +554,8 @@ private: template void write_int(R &writer, bool ptr, bool neg, T val) const { - char buf[20]; + /* binary representation is the biggest */ + char buf[sizeof(T) * CHAR_BIT + 1]; size_t n = 0; char isp = spec(); @@ -573,7 +574,25 @@ private: if (zval) { buf[n++] = '0'; } + + auto const &fac = std::use_facet>(p_loc); + auto const &grp = fac.grouping(); + char tsep = fac.thousands_sep(); + auto grpp = reinterpret_cast(grp.data()); + unsigned char grpn = *grpp; for (; val; val /= base) { + if (*grpp) { + if (!grpn) { + buf[n++] = tsep; + if (*(grpp + 1)) { + ++grpp; + } + grpn = *grpp; + } + if (grpn) { + --grpn; + } + } T vb = val % base; buf[n++] = (vb + "70"[vb < 10]) | cmask; }