| 1961 | // Writes a decimal integer with digit grouping. |
| 1962 | template <typename OutputIt, typename UInt, typename Char> |
| 1963 | auto write_int(OutputIt out, UInt value, unsigned prefix, |
| 1964 | const format_specs& specs, const digit_grouping<Char>& grouping) |
| 1965 | -> OutputIt { |
| 1966 | static_assert(std::is_same<uint64_or_128_t<UInt>, UInt>::value, ""); |
| 1967 | int num_digits = 0; |
| 1968 | auto buffer = memory_buffer(); |
| 1969 | switch (specs.type()) { |
| 1970 | default: FMT_ASSERT(false, ""); FMT_FALLTHROUGH; |
| 1971 | case presentation_type::none: |
| 1972 | case presentation_type::dec: |
| 1973 | num_digits = count_digits(value); |
| 1974 | format_decimal<char>(appender(buffer), value, num_digits); |
| 1975 | break; |
| 1976 | case presentation_type::hex: |
| 1977 | if (specs.alt()) |
| 1978 | prefix_append(prefix, unsigned(specs.upper() ? 'X' : 'x') << 8 | '0'); |
| 1979 | num_digits = count_digits<4>(value); |
| 1980 | format_base2e<char>(4, appender(buffer), value, num_digits, specs.upper()); |
| 1981 | break; |
| 1982 | case presentation_type::oct: |
| 1983 | num_digits = count_digits<3>(value); |
| 1984 | // Octal prefix '0' is counted as a digit, so only add it if precision |
| 1985 | // is not greater than the number of digits. |
| 1986 | if (specs.alt() && specs.precision <= num_digits && value != 0) |
| 1987 | prefix_append(prefix, '0'); |
| 1988 | format_base2e<char>(3, appender(buffer), value, num_digits); |
| 1989 | break; |
| 1990 | case presentation_type::bin: |
| 1991 | if (specs.alt()) |
| 1992 | prefix_append(prefix, unsigned(specs.upper() ? 'B' : 'b') << 8 | '0'); |
| 1993 | num_digits = count_digits<1>(value); |
| 1994 | format_base2e<char>(1, appender(buffer), value, num_digits); |
| 1995 | break; |
| 1996 | case presentation_type::chr: |
| 1997 | return write_char<Char>(out, static_cast<Char>(value), specs); |
| 1998 | } |
| 1999 | |
| 2000 | unsigned size = (prefix != 0 ? prefix >> 24 : 0) + to_unsigned(num_digits) + |
| 2001 | to_unsigned(grouping.count_separators(num_digits)); |
| 2002 | return write_padded<Char, align::right>( |
| 2003 | out, specs, size, size, [&](reserve_iterator<OutputIt> it) { |
| 2004 | for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) |
| 2005 | *it++ = static_cast<Char>(p & 0xff); |
| 2006 | return grouping.apply(it, string_view(buffer.data(), buffer.size())); |
| 2007 | }); |
| 2008 | } |
| 2009 | |
| 2010 | #if FMT_USE_LOCALE |
| 2011 | // Writes a localized value. |
no test coverage detected